| 1 | package Getopt::Chain::v005::Context; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Moose; | ||
| 7 | use Getopt::Chain::v005::Carp; | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Getopt::Chain::v005::Context - Per-command context | ||
| 12 | |||
| 13 | =head1 DESCRIPTION | ||
| 14 | |||
| 15 | A context encapsulates the current state of execution, including: | ||
| 16 | |||
| 17 | The name of the current command (or undef if at the "root") | ||
| 18 | Every option parsed so far | ||
| 19 | Options local to the current command | ||
| 20 | The arguments as they were BEFORE parsing options for this command | ||
| 21 | The arguments remaining AFTER parsing options for this command | ||
| 22 | |||
| 23 | =head1 METHODS | ||
| 24 | |||
| 25 | =head2 $context->command | ||
| 26 | |||
| 27 | Returns the name of the current command (or undef in a special case) | ||
| 28 | |||
| 29 | ./script --verbose edit --file xyzzy.c | ||
| 30 | # The command name is "edit" in the edit subroutine | ||
| 31 | |||
| 32 | ./script --help | ||
| 33 | # The command name is undef in the root subroutine | ||
| 34 | |||
| 35 | =head2 $context->option( <name> ) | ||
| 36 | |||
| 37 | Returns the value of the option for <name> | ||
| 38 | |||
| 39 | <name> should be primary name of the option (see L<Getopt::Long> for more information | ||
| 40 | on primary/alias naming) | ||
| 41 | |||
| 42 | If called in list context and the value of option is an ARRAY reference, | ||
| 43 | then this method returns a list: | ||
| 44 | |||
| 45 | ./script --exclude apple --exclude banana --exclude --cherry | ||
| 46 | ... | ||
| 47 | my @exclude = $context->option( exclude ) | ||
| 48 | |||
| 49 | See L<Hash::Param> for more usage information | ||
| 50 | |||
| 51 | =head2 $context->options( <name>, <name>, ... ) | ||
| 52 | |||
| 53 | Similar to ->option( <name> ) except for many-at-once | ||
| 54 | |||
| 55 | Returns a list in list context, and an ARRAY reference otherwise (you could | ||
| 56 | end up with a LoL situation in that case) | ||
| 57 | |||
| 58 | See L<Hash::Param> for more usage information | ||
| 59 | |||
| 60 | =head2 $context->options | ||
| 61 | |||
| 62 | Returns the keys of the option hash in list context | ||
| 63 | |||
| 64 | Returns the option HASH reference in scalar context | ||
| 65 | |||
| 66 | ./script --verbose | ||
| 67 | ... | ||
| 68 | if ( $context->options->{verbose} ) { ... } | ||
| 69 | |||
| 70 | See L<Hash::Param> for more usage information | ||
| 71 | |||
| 72 | =head2 $context->local_option | ||
| 73 | |||
| 74 | =head2 $context->local_options | ||
| 75 | |||
| 76 | Behave similarly to ->option and ->options, except only cover options local to the current command | ||
| 77 | |||
| 78 | ./script --verbose edit --file xyzzy.c | ||
| 79 | $context->local_option( file ) # Returns 'xyzzy.c' | ||
| 80 | $context->local_option( verbose ) # Doesn't return anything | ||
| 81 | $context->option( verbose ) # Returns 1 | ||
| 82 | |||
| 83 | =head2 $context->stash | ||
| 84 | |||
| 85 | An initially empty HASH reference that can be used for sharing inter-command information | ||
| 86 | |||
| 87 | Similar to the stash in L<Catalyst> | ||
| 88 | |||
| 89 | =head2 $context->arguments | ||
| 90 | |||
| 91 | Returns a copy of the arguments (@ARGV) for the current command BEFORE option parsing | ||
| 92 | |||
| 93 | Returns an ARRAY reference (still a copy) when called in scalar context | ||
| 94 | |||
| 95 | ./script --verbose edit --file xyzzy.c | ||
| 96 | |||
| 97 | # At the very beginning: | ||
| 98 | $context->arguments # Returns ( --verbose edit --file xyzzy.c ) | ||
| 99 | |||
| 100 | # In the "edit" subroutine: | ||
| 101 | $context->arguments # Returns ( edit --file xyzzy.c ) | ||
| 102 | |||
| 103 | =head2 $context->remaining_arguments | ||
| 104 | |||
| 105 | Returns a copy of the remaining arguments (@ARGV) for the current command AFTER option parsing | ||
| 106 | |||
| 107 | Returns an ARRAY reference (still a copy) when called in scalar context | ||
| 108 | |||
| 109 | ./script --verbose edit --file xyzzy.c | ||
| 110 | |||
| 111 | # At the very beginning: | ||
| 112 | $context->remaining_arguments # Returns ( edit --file xyzzy.c ) | ||
| 113 | |||
| 114 | # In the "edit" subroutine: | ||
| 115 | $context->remaining_arguments # Returns ( ) | ||
| 116 | |||
| 117 | =head2 $context->abort( [ ... ] ) | ||
| 118 | |||
| 119 | Immediately exit the process with exit code of -1 | ||
| 120 | |||
| 121 | If the optional ... (message) is given, then print that out to STDERR first | ||
| 122 | |||
| 123 | =head1 SEE ALSO | ||
| 124 | |||
| 125 | L<Getopt::Chain::v005> | ||
| 126 | |||
| 127 | =cut | ||
| 128 | |||
| 129 | use Hash::Param; | ||
| 130 | |||
| 131 | has options => qw/reader _options lazy_build 1 isa HashRef/; | ||
| 132 | sub _build_options { | ||
| 133 | my $self = shift; | ||
| 134 | return {}; | ||
| 135 | } | ||
| 136 | |||
| 137 | has options_ => qw/is ro isa Hash::Param lazy_build 1/, handles => {qw/option param options params/}; | ||
| 138 | sub _build_options_ { | ||
| 139 | my $self = shift; | ||
| 140 | return Hash::Param->new(params => $self->_options); | ||
| 141 | } | ||
| 142 | |||
| 143 | has chain => qw/is ro isa ArrayRef/, default => sub { [] }; | ||
| 144 | |||
| 145 | has stash => qw/is ro isa HashRef/, default => sub { {} }; | ||
| 146 | |||
| 147 | sub BUILD { | ||
| 148 | my $self = shift; | ||
| 149 | my $given = shift; | ||
| 150 | } | ||
| 151 | |||
| 152 | sub push { | ||
| 153 | my $self = shift; | ||
| 154 | my $link = Getopt::Chain::v005::Context::Link->new(context => $self, @_); | ||
| 155 | push @{ $self->chain }, $link; | ||
| 156 | return $link; | ||
| 157 | } | ||
| 158 | |||
| 159 | sub pop { | ||
| 160 | my $self = shift; | ||
| 161 | pop @{ $self->chain }; | ||
| 162 | } | ||
| 163 | |||
| 164 | sub run { | ||
| 165 | my $self = shift; | ||
| 166 | my $path = shift || ""; | ||
| 167 | |||
| 168 | my @path = grep { length $_ } split m/[ \/]+/, $path; | ||
| 169 | |||
| 170 | my $link = $self->link; | ||
| 171 | my $processor = $self->link(0)->processor; | ||
| 172 | for (@path) { | ||
| 173 | # TODO Probably call this 'resolve' | ||
| 174 | $processor = $processor->commands->{$_} or croak "Couldn't traverse $path: $_ not found"; | ||
| 175 | } | ||
| 176 | |||
| 177 | $self->push(processor => $processor, command => $path[-1], | ||
| 178 | arguments => $link->_arguments, remaining_arguments => $link->_remaining_arguments, options => scalar $link->options); | ||
| 179 | |||
| 180 | $processor->run->($self, @_); | ||
| 181 | |||
| 182 | $self->pop; | ||
| 183 | } | ||
| 184 | |||
| 185 | sub update { | ||
| 186 | my $self = shift; | ||
| 187 | |||
| 188 | my $link = $self->link; | ||
| 189 | |||
| 190 | my $local_options = $self->local_options_; | ||
| 191 | my $options = $self->options_; | ||
| 192 | |||
| 193 | for my $key ($local_options->params) { | ||
| 194 | $options->param($key => scalar $local_options->param($key)); | ||
| 195 | } | ||
| 196 | } | ||
| 197 | |||
| 198 | sub link { | ||
| 199 | my $self = shift; | ||
| 200 | my $at = shift; | ||
| 201 | |||
| 202 | $at = -1 unless defined $at; | ||
| 203 | return $self->chain->[$at]; | ||
| 204 | } | ||
| 205 | |||
| 206 | sub local_option { | ||
| 207 | my $self = shift; | ||
| 208 | return $self->link->option(@_); | ||
| 209 | } | ||
| 210 | |||
| 211 | sub local_options { | ||
| 212 | my $self = shift; | ||
| 213 | return $self->link->options(@_); | ||
| 214 | } | ||
| 215 | |||
| 216 | sub local_options_ { | ||
| 217 | my $self = shift; | ||
| 218 | return $self->link->options_(@_); | ||
| 219 | } | ||
| 220 | |||
| 221 | for my $method (qw/processor command arguments remaining_arguments remainder valid/) { | ||
| 222 | no strict 'refs'; | ||
| 223 | *$method = sub { | ||
| 224 | my $self = shift; | ||
| 225 | return $self->link->$method(@_); | ||
| 226 | }; | ||
| 227 | } | ||
| 228 | |||
| 229 | sub abort { | ||
| 230 | my $self = shift; | ||
| 231 | print STDERR "$0: "; | ||
| 232 | if (@_) { | ||
| 233 | my @__ = @_; # Modification of read-only value ... | ||
| 234 | chomp $__[-1]; | ||
| 235 | print STDERR join "", @__, "\n"; | ||
| 236 | } | ||
| 237 | else { | ||
| 238 | print STDERR "Unknown error: aborting"; | ||
| 239 | } | ||
| 240 | exit -1; | ||
| 241 | } | ||
| 242 | |||
| 243 | package Getopt::Chain::v005::Context::Link; | ||
| 244 | |||
| 245 | use Moose; | ||
| 246 | use Getopt::Chain::v005::Carp; | ||
| 247 | |||
| 248 | use Hash::Param; | ||
| 249 | |||
| 250 | has context => qw/is ro required 1 isa Getopt::Chain::v005::Context/, handles => [qw/all_options/]; | ||
| 251 | |||
| 252 | has processor => qw/is ro required 1 isa Getopt::Chain::v005/; | ||
| 253 | |||
| 254 | has command => qw/is ro required 1 isa Maybe[Str]/; | ||
| 255 | |||
| 256 | has options => qw/reader _options required 1 isa HashRef/; | ||
| 257 | |||
| 258 | has options_ => qw/is ro isa Hash::Param lazy_build 1/, handles => {qw/option param options params/}; | ||
| 259 | sub _build_options_ { | ||
| 260 | my $self = shift; | ||
| 261 | return Hash::Param->new(params => $self->_options); | ||
| 262 | } | ||
| 263 | |||
| 264 | has arguments => qw/is ro reader _arguments required 1 isa ArrayRef/; | ||
| 265 | sub arguments { | ||
| 266 | my @arguments = @{ shift->_arguments }; | ||
| 267 | return wantarray ? @arguments : \@arguments; | ||
| 268 | } | ||
| 269 | |||
| 270 | has remaining_arguments => qw/is ro reader _remaining_arguments required 1 isa ArrayRef/; | ||
| 271 | sub remaining_arguments { | ||
| 272 | my @arguments = @{ shift->_remaining_arguments }; | ||
| 273 | return wantarray ? @arguments : \@arguments; | ||
| 274 | } | ||
| 275 | |||
| 276 | sub remainder { | ||
| 277 | return scalar shift->remaining_arguments; | ||
| 278 | } | ||
| 279 | |||
| 280 | has valid => qw/is rw/; | ||
| 281 | |||
| 282 | 1; | ||
| 283 | |||
| 284 | __END__ | ||
| 285 | |||
| 286 | sub options { | ||
| 287 | my $self = shift; | ||
| 288 | |||
| 289 | if (@_) { | ||
| 290 | return $self->option(@_); | ||
| 291 | } | ||
| 292 | else { | ||
| 293 | return wantarray ? keys %{ $self->_options } : $self->_options; | ||
| 294 | } | ||
| 295 | } | ||
| 296 | |||
| 297 | sub option { | ||
| 298 | my $self = shift; | ||
| 299 | |||
| 300 | if (@_ == 0) { | ||
| 301 | return $self->options; | ||
| 302 | } | ||
| 303 | |||
| 304 | if (@_ == 1) { | ||
| 305 | |||
| 306 | my $option = shift; | ||
| 307 | |||
| 308 | unless (exists $self->_options->{$option}) { | ||
| 309 | return wantarray ? () : undef; | ||
| 310 | } | ||
| 311 | |||
| 312 | if (ref $self->_options->{$option} eq 'ARRAY') { | ||
| 313 | return (wantarray) | ||
| 314 | ? @{ $self->_options->{$option} } | ||
| 315 | : $self->_options->{$option}->[0]; | ||
| 316 | } | ||
| 317 | else { | ||
| 318 | return (wantarray) | ||
| 319 | ? ($self->_options->{$option}) | ||
| 320 | : $self->_options->{$option}; | ||
| 321 | } | ||
| 322 | } | ||
| 323 | elsif (@_ > 1) { | ||
| 324 | my @options = map { scalar $self->option($_) } @_; | ||
| 325 | return wantarray ? @options : \@options; | ||
| 326 | } | ||
| 327 | } | ||
| 328 | |||
| 329 | 1; |