| 1 | package Getopt::Chain::v005; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | Getopt::Chain::v005 - Option and subcommand processing in the style svn and git | ||
| 9 | |||
| 10 | =head1 VERSION | ||
| 11 | |||
| 12 | Version 0.005 | ||
| 13 | |||
| 14 | =cut | ||
| 15 | |||
| 16 | our $VERSION = '0.005'; | ||
| 17 | |||
| 18 | =head1 SYNPOSIS | ||
| 19 | |||
| 20 | #!/usr/bin/perl -w | ||
| 21 | |||
| 22 | use strict; | ||
| 23 | use Getopt::Chain::v005; | ||
| 24 | |||
| 25 | # A partial, pseudo-reimplementation of git(1): | ||
| 26 | |||
| 27 | Getopt::Chain::v005->process( | ||
| 28 | |||
| 29 | options => [qw/ version bare git-dir=s /] | ||
| 30 | |||
| 31 | run => sub { | ||
| 32 | my $context = shift; | ||
| 33 | my @arguments = @_; # Remaining, unparsed arguments | ||
| 34 | |||
| 35 | # ... do stuff before any subcommand ... | ||
| 36 | |||
| 37 | } | ||
| 38 | |||
| 39 | commands => { | ||
| 40 | |||
| 41 | init => { | ||
| 42 | options => [qw/ quiet|q --template=s /] | ||
| 43 | run => sub { | ||
| 44 | my $context = shift; | ||
| 45 | my @arguments = @_; # Again, remaining unparsed arguments | ||
| 46 | |||
| 47 | # ... do init stuff ... | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | commit => { | ||
| 52 | options => [qw/ all|a message|m=s /] | ||
| 53 | run => sub { | ||
| 54 | my $context = shift; | ||
| 55 | |||
| 56 | # ... do commit stuff .. | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | add => ... | ||
| 61 | |||
| 62 | help => ... | ||
| 63 | |||
| 64 | ... | ||
| 65 | } | ||
| 66 | ) | ||
| 67 | |||
| 68 | # The above will allow the following (example) usage: | ||
| 69 | # | ||
| 70 | # ./script --version | ||
| 71 | # ./script --git-dir path/to/repository init | ||
| 72 | # ./script --git-dir path/to/repository commit -a --message '...' | ||
| 73 | # ./script commit -m '~' | ||
| 74 | |||
| 75 | |||
| 76 | =head1 DESCRIPTION | ||
| 77 | |||
| 78 | Getopt::Chain::v005 can be used to provide C<svn(1)>- and C<git(1)>-style option and subcommand processing. Any option specification | ||
| 79 | covered by L<Getopt::Long> is fair game. | ||
| 80 | |||
| 81 | CAVEAT: Unfortunately, Getopt::Long slurps up the entire arguments array at once. Usually, this isn't a problem (as Getopt::Chain::v005 uses | ||
| 82 | pass_through). However, if a subcommand has an option with the same name or alias as an option for a parent, then that option won't be available | ||
| 83 | for the subcommand. For example: | ||
| 84 | |||
| 85 | ./script --verbose --revision 36 edit --revision 48 --file xyzzy.c | ||
| 86 | # Getopt::Chain::v005 will not associate the second --revision with "edit" | ||
| 87 | |||
| 88 | So, for now, try to use distinct option names/aliases :) | ||
| 89 | |||
| 90 | Finally, this code fairly new so aspects of the API *might* change (particularly abort/error-handling). Let me know if you're using it and have any suggestions. | ||
| 91 | |||
| 92 | TODO: Default values, option descriptions (like L<Getopt::Long::Descriptive>) and constraints (validation). | ||
| 93 | |||
| 94 | =head1 Basic configuration | ||
| 95 | |||
| 96 | A Getopt::Chain::v005 configuration is pretty straightforward | ||
| 97 | |||
| 98 | Essentially you declare a command, which consists of (optional) <options> and an (optional) <run> subroutine (a CODE reference) | ||
| 99 | |||
| 100 | <options> should be an ARRAY reference consisting of a L<Getopt::Long> specification | ||
| 101 | |||
| 102 | <run> should be a CODE reference which is a subroutine that accepts a L<Getopt::Chain::v005::Context> as the first argument and any remaining command-line | ||
| 103 | arguments (left after option parsing) as the rest of @_ | ||
| 104 | |||
| 105 | A third parameter exists, <commands> in which you associate a <name> with a command, consisting (recursively) of <options>, <run>, and <commands> | ||
| 106 | |||
| 107 | The <name> indicates what should be typed on the command-line to "trigger" the command. All-in-all, a configuration | ||
| 108 | looks something like this: | ||
| 109 | |||
| 110 | options => [ ... ] | ||
| 111 | |||
| 112 | run => sub {} | ||
| 113 | |||
| 114 | commands => { | ||
| 115 | |||
| 116 | <name1> => { | ||
| 117 | |||
| 118 | options => [ ... ] | ||
| 119 | |||
| 120 | run => sub {} | ||
| 121 | |||
| 122 | commands => ... | ||
| 123 | }, | ||
| 124 | |||
| 125 | <name2> => { | ||
| 126 | ... | ||
| 127 | }, | ||
| 128 | |||
| 129 | ... Rinse, repeat, etc. | ||
| 130 | } | ||
| 131 | |||
| 132 | See SYNPOSIS for an example | ||
| 133 | |||
| 134 | =head1 Error-handling configuration | ||
| 135 | |||
| 136 | Alongside <options>, <run>, and <commands>, you can designate a third parameter, <error> which is a either a CODE or HASH reference | ||
| 137 | |||
| 138 | This is an error handler for dealing with the following situations: | ||
| 139 | |||
| 140 | option_processing_error A Getopt::Long parsing error | ||
| 141 | |||
| 142 | have_remainder When a dash or dash-dash string remains on the argument stack without being processed | ||
| 143 | |||
| 144 | unknown_command When you've indicated that you want to accept a command but the user entered an unknown one | ||
| 145 | |||
| 146 | You can either give a single subroutine to deal with all three, or give a HASH with: | ||
| 147 | |||
| 148 | =over | ||
| 149 | |||
| 150 | =item A subroutine for dealing with that specific error | ||
| 151 | |||
| 152 | =item A value of '0' for disabling/ignoring that error | ||
| 153 | |||
| 154 | =back | ||
| 155 | |||
| 156 | For more detail (for now), look at the source: | ||
| 157 | |||
| 158 | perldoc -m Getopt::Chain::v005 | ||
| 159 | |||
| 160 | =head1 METHODS | ||
| 161 | |||
| 162 | =head2 Getopt::Chain::v005->process( <arguments>, ... ) | ||
| 163 | |||
| 164 | <arguments> should be an ARRAY reference | ||
| 165 | |||
| 166 | ... should consist of a Getopt::Chain::v005 configuration | ||
| 167 | |||
| 168 | =head2 Getopt::Chain::v005->process( ... ) | ||
| 169 | |||
| 170 | @ARGV will be used for <arguments> | ||
| 171 | |||
| 172 | ... should consist of a Getopt::Chain::v005 configuration | ||
| 173 | |||
| 174 | =cut | ||
| 175 | |||
| 176 | use Moose; | ||
| 177 | use Getopt::Chain::v005::Carp; | ||
| 178 | |||
| 179 | use Getopt::Chain::v005::Context; | ||
| 180 | |||
| 181 | use Getopt::Long qw/GetOptionsFromArray/; | ||
| 182 | |||
| 183 | has options => qw/is ro/; | ||
| 184 | |||
| 185 | has schema => qw/is ro isa Maybe[HashRef]/; | ||
| 186 | has _getopt_long_options => qw/is rw isa ArrayRef/; | ||
| 187 | |||
| 188 | has commands => qw/is rw isa Maybe[HashRef]/; | ||
| 189 | |||
| 190 | has run => qw/is ro isa Maybe[CodeRef]/; | ||
| 191 | |||
| 192 | has validate => qw/is ro isa Maybe[CodeRef]/; | ||
| 193 | |||
| 194 | has error => qw/is ro/; | ||
| 195 | |||
| 196 | sub BUILD { | ||
| 197 | my $self = shift; | ||
| 198 | my $given = shift; | ||
| 199 | |||
| 200 | my $commands = $self->_parse_commands($self->commands); | ||
| 201 | my ($schema, $getopt_long_options) = $self->_parse_schema($self->options); | ||
| 202 | |||
| 203 | $self->{commands} = $commands; | ||
| 204 | $self->{schema} = $schema; | ||
| 205 | $self->_getopt_long_options($getopt_long_options); | ||
| 206 | } | ||
| 207 | |||
| 208 | sub _parse_commands { | ||
| 209 | my $self = shift; | ||
| 210 | my $commands = shift; | ||
| 211 | |||
| 212 | return unless $commands; | ||
| 213 | |||
| 214 | my $class = ref $self; | ||
| 215 | |||
| 216 | my %commands; | ||
| 217 | while (my ($name, $command) = each %$commands) { | ||
| 218 | $commands{$name} = $class->new(inherit => $self, ref $command eq "CODE" ? (run => $command) : %$command); | ||
| 219 | } | ||
| 220 | |||
| 221 | return \%commands; | ||
| 222 | } | ||
| 223 | |||
| 224 | sub _parse_schema { | ||
| 225 | my $self = shift; | ||
| 226 | my $schema = shift; | ||
| 227 | |||
| 228 | my %schema; | ||
| 229 | my @getopt_long_options; | ||
| 230 | $schema = { map { $_ => undef } @$schema } if ref $schema eq "ARRAY"; | ||
| 231 | |||
| 232 | while (my ($specification, $more) = each %$schema) { | ||
| 233 | |||
| 234 | my (%option, %ParseOptionSpec); | ||
| 235 | |||
| 236 | my ($key, $name) = Getopt::Long::ParseOptionSpec($specification, \%ParseOptionSpec); | ||
| 237 | |||
| 238 | $option{key} = $key; | ||
| 239 | $option{name} = $name; | ||
| 240 | $option{aliases} = [ keys %ParseOptionSpec ]; | ||
| 241 | |||
| 242 | $schema{$key} = \%option; | ||
| 243 | push @getopt_long_options, $specification; | ||
| 244 | } | ||
| 245 | |||
| 246 | return (\%schema, \@getopt_long_options); | ||
| 247 | } | ||
| 248 | |||
| 249 | sub process { | ||
| 250 | my $self = shift; | ||
| 251 | unless (ref $self) { | ||
| 252 | my @process; | ||
| 253 | push @process, shift if ref $_[0] eq "ARRAY"; | ||
| 254 | return $self->new(@_)->process(@process); | ||
| 255 | } | ||
| 256 | my $arguments = shift; | ||
| 257 | my %given = 1 == @_ && ref $_[0] eq "HASH" ? %{ $_[0] } : @_; | ||
| 258 | |||
| 259 | my %options; | ||
| 260 | $arguments = [ @ARGV ] unless $arguments; | ||
| 261 | my $remaining_arguments = [ @$arguments ]; # This array will eventually contain leftover arguments | ||
| 262 | |||
| 263 | my $context = $given{context} ||= Getopt::Chain::v005::Context->new; | ||
| 264 | $context->push(processor => $self, command => $given{command}, | ||
| 265 | arguments => $arguments, remaining_arguments => $remaining_arguments, options => \%options); | ||
| 266 | |||
| 267 | eval { | ||
| 268 | if (my $getopt_long_options = $self->_getopt_long_options) { | ||
| 269 | Getopt::Long::Configure(qw/pass_through/); | ||
| 270 | GetOptionsFromArray($remaining_arguments, \%options, @$getopt_long_options); | ||
| 271 | } | ||
| 272 | }; | ||
| 273 | $self->_handle_option_processing_error($@, $context) if $@; | ||
| 274 | |||
| 275 | $context->update; | ||
| 276 | |||
| 277 | if (@$remaining_arguments && $remaining_arguments->[0] =~ m/^--\w/) { | ||
| 278 | $self->_handle_have_remainder('Have remainder "' . $remaining_arguments->[0] . '"', $context); | ||
| 279 | } | ||
| 280 | |||
| 281 | $context->valid($self->validate->($context)) if $self->validate; | ||
| 282 | |||
| 283 | $self->run->($context, @$remaining_arguments) if $self->run; | ||
| 284 | |||
| 285 | if (my $commands = $self->commands) { | ||
| 286 | my @arguments = @$remaining_arguments; | ||
| 287 | my $command = shift @arguments; | ||
| 288 | |||
| 289 | my $processor = $commands->{defined $command ? $command : 'DEFAULT'} || $commands->{DEFAULT}; | ||
| 290 | |||
| 291 | if ($processor) { | ||
| 292 | return $processor->process(\@arguments, command => $command, context => $context); | ||
| 293 | } | ||
| 294 | elsif (defined $command) { | ||
| 295 | $self->_handle_unknown_command("Unknown command \"$command\"", $context); | ||
| 296 | } | ||
| 297 | } | ||
| 298 | |||
| 299 | return $context->options; | ||
| 300 | } | ||
| 301 | |||
| 302 | sub _handle_option_processing_error { | ||
| 303 | my $self = shift; | ||
| 304 | return $self->_handle_error(option_processing_error => @_); | ||
| 305 | } | ||
| 306 | |||
| 307 | sub _handle_have_remainder { | ||
| 308 | my $self = shift; | ||
| 309 | return $self->_handle_error(have_remainder => @_); | ||
| 310 | } | ||
| 311 | |||
| 312 | sub _handle_unknown_command { | ||
| 313 | my $self = shift; | ||
| 314 | return $self->_handle_error(unknown_command => @_); | ||
| 315 | } | ||
| 316 | |||
| 317 | sub _handle_error { | ||
| 318 | my $self = shift; | ||
| 319 | my $event = shift; | ||
| 320 | my $description = shift; | ||
| 321 | |||
| 322 | my $error = $self->error; | ||
| 323 | |||
| 324 | if (ref $error eq "CODE") { | ||
| 325 | return $error->($event, $description, @_); | ||
| 326 | } | ||
| 327 | elsif (ref $error eq "HASH") { | ||
| 328 | goto _handle_error_croak unless defined (my $response = $error->{$event}); | ||
| 329 | |||
| 330 | if (ref $response eq "CODE") { | ||
| 331 | return $response->($event, $description, @_); | ||
| 332 | } | ||
| 333 | elsif ($response) { | ||
| 334 | goto _handle_error_croak; | ||
| 335 | } | ||
| 336 | else { | ||
| 337 | # Ignore the error | ||
| 338 | return; | ||
| 339 | } | ||
| 340 | } | ||
| 341 | |||
| 342 | croak "Don't understand error handler ($error)" if $error; | ||
| 343 | |||
| 344 | _handle_error_croak: | ||
| 345 | croak "$description ($event)"; | ||
| 346 | } | ||
| 347 | |||
| 348 | use MooseX::MakeImmutable; | ||
| 349 | MooseX::MakeImmutable->lock_down; | ||
| 350 | |||
| 351 | =head1 SEE ALSO | ||
| 352 | |||
| 353 | L<Getopt::Chain::v005::Context> | ||
| 354 | |||
| 355 | L<Getopt::Long> | ||
| 356 | |||
| 357 | L<App::Cmd> | ||
| 358 | |||
| 359 | L<MooseX::App::Cmd> | ||
| 360 | |||
| 361 | =head1 AUTHOR | ||
| 362 | |||
| 363 | Robert Krimen, C<< <rkrimen at cpan.org> >> | ||
| 364 | |||
| 365 | =head1 SOURCE | ||
| 366 | |||
| 367 | You can contribute or fork this project via GitHub: | ||
| 368 | |||
| 369 | L<http://github.com/robertkrimen/Getopt-Chain/tree/master> | ||
| 370 | |||
| 371 | git clone git://github.com/robertkrimen/Getopt-Chain.git Getopt-Chain | ||
| 372 | |||
| 373 | =head1 BUGS | ||
| 374 | |||
| 375 | Please report any bugs or feature requests to C<bug-Getopt-Chain at rt.cpan.org>, or through | ||
| 376 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Getopt-Chain>. I will be notified, and then you'll | ||
| 377 | automatically be notified of progress on your bug as I make changes. | ||
| 378 | |||
| 379 | |||
| 380 | |||
| 381 | |||
| 382 | =head1 SUPPORT | ||
| 383 | |||
| 384 | You can find documentation for this module with the perldoc command. | ||
| 385 | |||
| 386 | perldoc Getopt::Chain::v005 | ||
| 387 | |||
| 388 | |||
| 389 | You can also look for information at: | ||
| 390 | |||
| 391 | =over 4 | ||
| 392 | |||
| 393 | =item * RT: CPAN's request tracker | ||
| 394 | |||
| 395 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Getopt-Chain> | ||
| 396 | |||
| 397 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 398 | |||
| 399 | L<http://annocpan.org/dist/Getopt-Chain> | ||
| 400 | |||
| 401 | =item * CPAN Ratings | ||
| 402 | |||
| 403 | L<http://cpanratings.perl.org/d/Getopt-Chain> | ||
| 404 | |||
| 405 | =item * Search CPAN | ||
| 406 | |||
| 407 | L<http://search.cpan.org/dist/Getopt-Chain> | ||
| 408 | |||
| 409 | =back | ||
| 410 | |||
| 411 | |||
| 412 | =head1 ACKNOWLEDGEMENTS | ||
| 413 | |||
| 414 | |||
| 415 | =head1 COPYRIGHT & LICENSE | ||
| 416 | |||
| 417 | Copyright 2008 Robert Krimen, all rights reserved. | ||
| 418 | |||
| 419 | This program is free software; you can redistribute it and/or modify it | ||
| 420 | under the same terms as Perl itself. | ||
| 421 | |||
| 422 | |||
| 423 | =cut | ||
| 424 | |||
| 425 | 1; # End of Getopt::Chain::v005 |