| 1 | package Getopt::Chain::Context; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Moose; | ||
| 7 | use MooseX::AttributeHelpers; | ||
| 8 | use Getopt::Chain::Carp; | ||
| 9 | |||
| 10 | use Getopt::Chain; | ||
| 11 | |||
| 12 | use Getopt::Long qw/GetOptionsFromArray/; | ||
| 13 | use Hash::Param; | ||
| 14 | |||
| 15 | use constant DEBUG => Getopt::Chain->DEBUG; | ||
| 16 | our $DEBUG = DEBUG; | ||
| 17 | |||
| 18 | =head1 NAME | ||
| 19 | |||
| 20 | Getopt::Chain::Context - Per-command context | ||
| 21 | |||
| 22 | =head1 WARNING | ||
| 23 | |||
| 24 | This documentation is out of date and needs an update. For the real documentation: | ||
| 25 | |||
| 26 | perldoc -m Getopt::Chain::Context | ||
| 27 | |||
| 28 | =head1 DESCRIPTION | ||
| 29 | |||
| 30 | A context encapsulates the current state of execution, including: | ||
| 31 | |||
| 32 | The name of the current command (or undef if at the "root") | ||
| 33 | Every option parsed so far | ||
| 34 | Options local to the current command | ||
| 35 | The arguments as they were BEFORE parsing options for this command | ||
| 36 | The arguments remaining AFTER parsing options for this command | ||
| 37 | |||
| 38 | =head1 METHODS | ||
| 39 | |||
| 40 | =head2 $context->command | ||
| 41 | |||
| 42 | Returns the name of the current command (or undef in a special case) | ||
| 43 | |||
| 44 | ./script --verbose edit --file xyzzy.c | ||
| 45 | # The command name is "edit" in the edit subroutine | ||
| 46 | |||
| 47 | ./script --help | ||
| 48 | # The command name is undef in the root subroutine | ||
| 49 | |||
| 50 | =head2 $context->option( <name> ) | ||
| 51 | |||
| 52 | Returns the value of the option for <name> | ||
| 53 | |||
| 54 | <name> should be primary name of the option (see L<Getopt::Long> for more information | ||
| 55 | on primary/alias naming) | ||
| 56 | |||
| 57 | If called in list context and the value of option is an ARRAY reference, | ||
| 58 | then this method returns a list: | ||
| 59 | |||
| 60 | ./script --exclude apple --exclude banana --exclude --cherry | ||
| 61 | ... | ||
| 62 | my @exclude = $context->option( exclude ) | ||
| 63 | |||
| 64 | See L<Hash::Param> for more usage information | ||
| 65 | |||
| 66 | =head2 $context->options( <name>, <name>, ... ) | ||
| 67 | |||
| 68 | Similar to ->option( <name> ) except for many-at-once | ||
| 69 | |||
| 70 | Returns a list in list context, and an ARRAY reference otherwise (you could | ||
| 71 | end up with a LoL situation in that case) | ||
| 72 | |||
| 73 | See L<Hash::Param> for more usage information | ||
| 74 | |||
| 75 | =head2 $context->options | ||
| 76 | |||
| 77 | Returns the keys of the option hash in list context | ||
| 78 | |||
| 79 | Returns the option HASH reference in scalar context | ||
| 80 | |||
| 81 | ./script --verbose | ||
| 82 | ... | ||
| 83 | if ( $context->options->{verbose} ) { ... } | ||
| 84 | |||
| 85 | See L<Hash::Param> for more usage information | ||
| 86 | |||
| 87 | =head2 $context->local_option | ||
| 88 | |||
| 89 | =head2 $context->local_options | ||
| 90 | |||
| 91 | Behave similarly to ->option and ->options, except only cover options local to the current command | ||
| 92 | |||
| 93 | ./script --verbose edit --file xyzzy.c | ||
| 94 | $context->local_option( file ) # Returns 'xyzzy.c' | ||
| 95 | $context->local_option( verbose ) # Doesn't return anything | ||
| 96 | $context->option( verbose ) # Returns 1 | ||
| 97 | |||
| 98 | =head2 $context->stash | ||
| 99 | |||
| 100 | An initially empty HASH reference that can be used for sharing inter-command information | ||
| 101 | |||
| 102 | Similar to the stash in L<Catalyst> | ||
| 103 | |||
| 104 | =head2 $context->arguments | ||
| 105 | |||
| 106 | Returns a copy of the arguments (@ARGV) for the current command BEFORE option parsing | ||
| 107 | |||
| 108 | Returns an ARRAY reference (still a copy) when called in scalar context | ||
| 109 | |||
| 110 | ./script --verbose edit --file xyzzy.c | ||
| 111 | |||
| 112 | # At the very beginning: | ||
| 113 | $context->arguments # Returns ( --verbose edit --file xyzzy.c ) | ||
| 114 | |||
| 115 | # In the "edit" subroutine: | ||
| 116 | $context->arguments # Returns ( edit --file xyzzy.c ) | ||
| 117 | |||
| 118 | =head2 $context->remaining_arguments | ||
| 119 | |||
| 120 | Returns a copy of the remaining arguments (@ARGV) for the current command AFTER option parsing | ||
| 121 | |||
| 122 | Returns an ARRAY reference (still a copy) when called in scalar context | ||
| 123 | |||
| 124 | ./script --verbose edit --file xyzzy.c | ||
| 125 | |||
| 126 | # At the very beginning: | ||
| 127 | $context->remaining_arguments # Returns ( edit --file xyzzy.c ) | ||
| 128 | |||
| 129 | # In the "edit" subroutine: | ||
| 130 | $context->remaining_arguments # Returns ( ) | ||
| 131 | |||
| 132 | =cut | ||
| 133 | |||
| 134 | # Should probably move these into Getopt::Chain | ||
| 135 | # ...or even... Getopt::Longer :) | ||
| 136 | sub is_option_like($) { | ||
| 137 | return $_[0] =~ m/^-/; | ||
| 138 | } | ||
| 139 | |||
| 140 | sub consume_arguments($$) { # Will modify arguments, reflecting consumption | ||
| 141 | my $argument_schema = shift; | ||
| 142 | my $arguments = shift; | ||
| 143 | |||
| 144 | my %options; | ||
| 145 | eval { | ||
| 146 | if ($argument_schema && @$argument_schema) { | ||
| 147 | Getopt::Long::Configure(qw/pass_through/); | ||
| 148 | GetOptionsFromArray($arguments, \%options, @$argument_schema); | ||
| 149 | } | ||
| 150 | }; | ||
| 151 | croak "There was an error option-processing arguments: $@" if $@; | ||
| 152 | |||
| 153 | if (@$arguments && is_option_like $arguments->[0]) { | ||
| 154 | croak "Have remainder arguments after option-processing: ", $arguments->[0]; | ||
| 155 | } | ||
| 156 | |||
| 157 | return ( \%options ); | ||
| 158 | } | ||
| 159 | |||
| 160 | has dispatcher => qw/is ro required 1/; | ||
| 161 | |||
| 162 | has _options => qw/is ro isa Hash::Param lazy_build 1/, handles => {qw/option param options params/}; | ||
| 163 | sub _build__options { | ||
| 164 | my $self = shift; | ||
| 165 | return Hash::Param->new(params => {}); | ||
| 166 | } | ||
| 167 | |||
| 168 | has _stash => qw/is ro isa HashRef/, default => sub { {} }; | ||
| 169 | sub stash { | ||
| 170 | my $self = shift; | ||
| 171 | return $self->_stash unless @_; | ||
| 172 | my $stash = $self->_stash; | ||
| 173 | while ( @_ ) { my ($k, $v) = (shift @_, shift @_); $stash->{$k} = $v } | ||
| 174 | return $stash; | ||
| 175 | } | ||
| 176 | |||
| 177 | # The original arguments from the commandline (or wherever)... read only! | ||
| 178 | has arguments => qw/metaclass Collection::Array reader _arguments required 1 lazy 1 isa ArrayRef/, default => sub { [] }, provides => {qw/ | ||
| 179 | elements arguments | ||
| 180 | /}; | ||
| 181 | |||
| 182 | # The arguments remaining after each step does argument consuming... written by the step! | ||
| 183 | has remaining_arguments => qw/metaclass Collection::Array accessor _remaining_arguments isa ArrayRef/, provides => {qw/ | ||
| 184 | elements remaining_arguments | ||
| 185 | shift shift_remaining_argument | ||
| 186 | first first_remaining_argument | ||
| 187 | /}; | ||
| 188 | |||
| 189 | has steps => qw/metaclass Collection::Array reader _steps required 1 lazy 1 isa ArrayRef/, default => sub { [] }, provides => {qw/ | ||
| 190 | elements steps | ||
| 191 | first first_step | ||
| 192 | last last_step | ||
| 193 | push push_step | ||
| 194 | pop pop_step | ||
| 195 | /}; | ||
| 196 | |||
| 197 | has _path => qw/metaclass Collection::Array is ro required 1 lazy 1 isa ArrayRef/, default => sub { [] }, provides => {qw/ | ||
| 198 | elements path | ||
| 199 | push push_path | ||
| 200 | /}; | ||
| 201 | |||
| 202 | sub initialize_run { | ||
| 203 | my $self = shift; | ||
| 204 | $self->_remaining_arguments( [ $self->arguments ] ); | ||
| 205 | } | ||
| 206 | |||
| 207 | sub run { | ||
| 208 | my $self = shift; | ||
| 209 | |||
| 210 | $self->initialize_run; | ||
| 211 | 1 while $self->next; | ||
| 212 | } | ||
| 213 | |||
| 214 | sub next { | ||
| 215 | my $self = shift; | ||
| 216 | |||
| 217 | unless (defined $self->_remaining_arguments) { # Haven't been run yet | ||
| 218 | $self->initialize_run; | ||
| 219 | } | ||
| 220 | |||
| 221 | my $run_path = join ' ', $self->path; | ||
| 222 | warn "Context::next ", $self->path_as_string, " ($run_path)\n" if $DEBUG; | ||
| 223 | |||
| 224 | |||
| 225 | { | ||
| 226 | # $self->dispatcher->run( $run_path, $self ); # This will (indirectly) call ->run_step( ... ) below | ||
| 227 | my $dispatch = $self->dispatcher->dispatch( $run_path ); | ||
| 228 | if ( my @matches = $dispatch->matches ) { | ||
| 229 | for my $match ($dispatch->matches) { | ||
| 230 | my $result = $match->result; | ||
| 231 | last if $match->run( $self ); # ->run_step returned true | ||
| 232 | } | ||
| 233 | } | ||
| 234 | } | ||
| 235 | my $next_path_part; | ||
| 236 | $self->push_path( $next_path_part ) if $next_path_part = $self->next_path_part; | ||
| 237 | return $next_path_part; | ||
| 238 | } | ||
| 239 | |||
| 240 | sub next_path_part { | ||
| 241 | my $self = shift; | ||
| 242 | |||
| 243 | return unless defined (my $argument = $self->first_remaining_argument); | ||
| 244 | croak "Had remainder arguments after option-processing: ", $argument, " @ ", $self->path_as_string, " [", join ' ', $self->remaining_arguments, "]" if is_option_like $argument; | ||
| 245 | return $self->shift_remaining_argument; # Same as $argument, really | ||
| 246 | } | ||
| 247 | |||
| 248 | sub last { | ||
| 249 | my $self = shift; | ||
| 250 | return ! defined $self->first_remaining_argument; | ||
| 251 | } | ||
| 252 | |||
| 253 | sub path_as_string { | ||
| 254 | my $self = shift; | ||
| 255 | return join '/', '^START', $self->path, ($self->last ? '$' : ()); | ||
| 256 | } | ||
| 257 | |||
| 258 | sub run_step { # Called from within the Path::Dispatcher rule | ||
| 259 | my $self = shift; | ||
| 260 | my $argument_schema = shift; | ||
| 261 | my $run = shift; | ||
| 262 | my $control = shift; | ||
| 263 | |||
| 264 | $argument_schema = [] unless defined $argument_schema; | ||
| 265 | |||
| 266 | my $step = $self->add_step( argument_schema => $argument_schema, run => $run ); | ||
| 267 | return 1 if $step->run( $control ); # We consumed and ran, so we need to rollback | ||
| 268 | $self->pop_step; # Rollback, since we didn't actually run | ||
| 269 | return 0; | ||
| 270 | } | ||
| 271 | |||
| 272 | sub add_step { | ||
| 273 | my $self = shift; | ||
| 274 | my %given = @_; # Should be: argument_schema, run | ||
| 275 | |||
| 276 | my $parent = $self->last_step; # Could be undef | ||
| 277 | my $step = Getopt::Chain::Context::Step->new( context => $self, parent => $parent, path => [ $self->path ], arguments => [ $self->remaining_arguments ], %given ); | ||
| 278 | $self->push_step( $step ); | ||
| 279 | return $step; | ||
| 280 | } | ||
| 281 | |||
| 282 | sub command { | ||
| 283 | my $self = shift; | ||
| 284 | return $self->last_step->last_path_part; | ||
| 285 | } | ||
| 286 | |||
| 287 | sub local_option { | ||
| 288 | my $self = shift; | ||
| 289 | return $self->last_step->option( @_ ); | ||
| 290 | } | ||
| 291 | |||
| 292 | sub local_options { | ||
| 293 | my $self = shift; | ||
| 294 | return $self->last_step->options( @_ ); | ||
| 295 | } | ||
| 296 | |||
| 297 | sub local_path { | ||
| 298 | my $self = shift; | ||
| 299 | return $self->last_step->path; | ||
| 300 | } | ||
| 301 | |||
| 302 | package Getopt::Chain::Context::Step; | ||
| 303 | |||
| 304 | use Moose; | ||
| 305 | use Getopt::Chain::Carp; | ||
| 306 | |||
| 307 | use Hash::Param; | ||
| 308 | |||
| 309 | use constant DEBUG => Getopt::Chain->DEBUG; | ||
| 310 | our $DEBUG = DEBUG; | ||
| 311 | |||
| 312 | has context => qw/is ro required 1 isa Getopt::Chain::Context/; | ||
| 313 | |||
| 314 | has _options => qw/is ro isa Hash::Param lazy_build 1/, handles => {qw/option param options params/}; | ||
| 315 | sub _build__options { | ||
| 316 | my $self = shift; | ||
| 317 | return Hash::Param->new( params => {} ); | ||
| 318 | } | ||
| 319 | |||
| 320 | has arguments => qw/metaclass Collection::Array accessor _arguments required 1 isa ArrayRef/, provides => {qw/ | ||
| 321 | elements arguments | ||
| 322 | /}; | ||
| 323 | |||
| 324 | has argument_schema => qw/metaclass Collection::Array accessor _argument_schema required 1 isa ArrayRef/, provides => {qw/ | ||
| 325 | elements argument_schema | ||
| 326 | /}; | ||
| 327 | |||
| 328 | has run => qw/is ro reader _run isa Maybe[CodeRef]/; | ||
| 329 | |||
| 330 | has _path => qw/metaclass Collection::Array is ro required 1 lazy 1 isa ArrayRef init_arg path/, default => sub { [] }, provides => {qw/ | ||
| 331 | elements path | ||
| 332 | last last_path_part | ||
| 333 | push push_path | ||
| 334 | /}; | ||
| 335 | |||
| 336 | has parent => qw/is ro isa Maybe[Getopt::Chain::Context::Step]/; | ||
| 337 | |||
| 338 | sub run { | ||
| 339 | my $self = shift; | ||
| 340 | my $control = shift; | ||
| 341 | |||
| 342 | my @arguments; | ||
| 343 | if ($control->{arguments_from_1} && defined $1) { | ||
| 344 | push @arguments, grep { length } split m/\s+/, $1; | ||
| 345 | } | ||
| 346 | |||
| 347 | my $options = {}; | ||
| 348 | my $arguments = [ $self->arguments ]; | ||
| 349 | my $argument_schema = [ $self->argument_schema ]; | ||
| 350 | |||
| 351 | warn "Context::Step::run ", $self->context->path_as_string, " [@$arguments] {@$argument_schema}\n" if $DEBUG; | ||
| 352 | |||
| 353 | eval { | ||
| 354 | $options = Getopt::Chain::Context::consume_arguments $argument_schema, $arguments; | ||
| 355 | }; | ||
| 356 | if ($@) { | ||
| 357 | chomp( my $error = $@ ); | ||
| 358 | croak "At ", join( '/', $self->path ), " with arguments [@$arguments]: $@"; | ||
| 359 | } | ||
| 360 | |||
| 361 | $self->context->_remaining_arguments( $arguments ); | ||
| 362 | |||
| 363 | while (my ($key, $value) = each %$options) { | ||
| 364 | $self->option( $key => $value ); | ||
| 365 | $self->context->option( $key => $value ); # TODO Better way to do this... | ||
| 366 | } | ||
| 367 | |||
| 368 | my $last = ! @$arguments; | ||
| 369 | |||
| 370 | unless ($last || $control->{always_run}) { | ||
| 371 | warn "Context::Step::run ", $self->context->path_as_string, " SKIP\n" if DEBUG; | ||
| 372 | return; | ||
| 373 | } | ||
| 374 | |||
| 375 | push @arguments, @$arguments; # TODO: Test this | ||
| 376 | |||
| 377 | my $run = $self->_run; | ||
| 378 | $run->( $self->context, @arguments ) if $run; | ||
| 379 | |||
| 380 | return 1; | ||
| 381 | } | ||
| 382 | |||
| 383 | 1; | ||
| 384 |