| 1 | package Getopt::Args; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Carp qw/croak carp/; | ||
| 5 | use Encode qw/decode/; | ||
| 6 | use Exporter::Tidy | ||
| 7 | default => [qw/opt arg optargs usage subcmd/], | ||
| 8 | other => [qw/dispatch class_optargs/]; | ||
| 9 | use Getopt::Long qw/GetOptionsFromArray/; | ||
| 10 | use List::Util qw/max/; | ||
| 11 | |||
| 12 | our $VERSION = '0.1.21'; | ||
| 13 | our $COLOUR = 0; | ||
| 14 | our $ABBREV = 0; | ||
| 15 | our $SORT = 0; | ||
| 16 | our $PRINT_DEFAULT = 0; | ||
| 17 | our $PRINT_ISA = 0; | ||
| 18 | |||
| 19 | my %seen; # hash of hashes keyed by 'caller', then opt/arg name | ||
| 20 | my %opts; # option configuration keyed by 'caller' | ||
| 21 | my %args; # argument configuration keyed by 'caller' | ||
| 22 | my %caller; # current 'caller' keyed by real caller | ||
| 23 | my %desc; # sub-command descriptions | ||
| 24 | my %dispatching; # track optargs() calls from dispatch classes | ||
| 25 | my %hidden; # subcmd hiding by default | ||
| 26 | |||
| 27 | # internal function for App::optargs | ||
| 28 | sub _cmdlist { | ||
| 29 | return sort grep { $_ ne 'App::optargs' } keys %seen; | ||
| 30 | } | ||
| 31 | |||
| 32 | # ------------------------------------------------------------------------ | ||
| 33 | # Sub-command definition | ||
| 34 | # | ||
| 35 | # This works by faking caller context in opt() and arg() | ||
| 36 | # ------------------------------------------------------------------------ | ||
| 37 | my %subcmd_params = ( | ||
| 38 | cmd => undef, | ||
| 39 | comment => undef, | ||
| 40 | hidden => undef, | ||
| 41 | |||
| 42 | # alias => '', | ||
| 43 | # ishelp => undef, | ||
| 44 | ); | ||
| 45 | |||
| 46 | my @subcmd_required = (qw/cmd comment/); | ||
| 47 | |||
| 48 | sub subcmd { | ||
| 49 | my $params = {@_}; | ||
| 50 | my $caller = caller; | ||
| 51 | |||
| 52 | if ( my @missing = grep { !exists $params->{$_} } @subcmd_required ) { | ||
| 53 | croak "missing required parameter(s): @missing"; | ||
| 54 | } | ||
| 55 | |||
| 56 | if ( my @invalid = grep { !exists $subcmd_params{$_} } keys %$params ) { | ||
| 57 | my @valid = keys %subcmd_params; | ||
| 58 | croak "invalid parameter(s): @invalid (valid: @valid)"; | ||
| 59 | } | ||
| 60 | |||
| 61 | # croak "'ishelp' can only be applied to Bool opts" | ||
| 62 | # if $params->{ishelp} and $params->{isa} ne 'Bool'; | ||
| 63 | |||
| 64 | my @cmd = | ||
| 65 | ref $params->{cmd} eq 'ARRAY' | ||
| 66 | ? @{ $params->{cmd} } | ||
| 67 | : ( $params->{cmd} ); | ||
| 68 | croak 'missing cmd elements' unless @cmd; | ||
| 69 | |||
| 70 | my $name = pop @cmd; | ||
| 71 | my $parent = join( '::', $caller, @cmd ); | ||
| 72 | $parent =~ s/-/_/g; | ||
| 73 | |||
| 74 | croak "parent command not found: @cmd" unless $seen{$parent}; | ||
| 75 | |||
| 76 | my $package = $parent . '::' . $name; | ||
| 77 | $package =~ s/-/_/g; | ||
| 78 | |||
| 79 | croak "sub command already defined: @cmd $name" if $seen{$package}; | ||
| 80 | |||
| 81 | $caller{$caller} = $package; | ||
| 82 | $desc{$package} = $params->{comment}; | ||
| 83 | $seen{$package} = {}; | ||
| 84 | $opts{$package} = []; | ||
| 85 | $args{$package} = []; | ||
| 86 | $hidden{$package} = $params->{hidden}; | ||
| 87 | |||
| 88 | my $parent_arg = ( grep { $_->{isa} eq 'SubCmd' } @{ $args{$parent} } )[0]; | ||
| 89 | push( @{ $parent_arg->{subcommands} }, $name ); | ||
| 90 | |||
| 91 | return; | ||
| 92 | } | ||
| 93 | |||
| 94 | # ------------------------------------------------------------------------ | ||
| 95 | # Option definition | ||
| 96 | # ------------------------------------------------------------------------ | ||
| 97 | my %opt_params = ( | ||
| 98 | isa => undef, | ||
| 99 | isa_name => undef, | ||
| 100 | comment => undef, | ||
| 101 | default => undef, | ||
| 102 | alias => '', | ||
| 103 | ishelp => undef, | ||
| 104 | hidden => undef, | ||
| 105 | ); | ||
| 106 | |||
| 107 | my @opt_required = (qw/isa comment/); | ||
| 108 | |||
| 109 | my %opt_isa = ( | ||
| 110 | 'Bool' => '!', | ||
| 111 | 'Counter' => '+', | ||
| 112 | 'Str' => '=s', | ||
| 113 | 'Int' => '=i', | ||
| 114 | 'Num' => '=f', | ||
| 115 | 'ArrayRef' => '=s@', | ||
| 116 | 'HashRef' => '=s%', | ||
| 117 | ); | ||
| 118 | |||
| 119 | sub opt { | ||
| 120 | my $name = shift; | ||
| 121 | my $params = {@_}; | ||
| 122 | my $caller = caller; | ||
| 123 | my $package = $caller{$caller} || $caller; | ||
| 124 | |||
| 125 | croak 'usage: opt $name => (%parameters)' unless $name; | ||
| 126 | croak "'$name' already defined" if $seen{$package}->{$name}; | ||
| 127 | |||
| 128 | if ( my @missing = grep { !exists $params->{$_} } @opt_required ) { | ||
| 129 | croak "missing required parameter(s): @missing"; | ||
| 130 | } | ||
| 131 | |||
| 132 | if ( my @invalid = grep { !exists $opt_params{$_} } keys %$params ) { | ||
| 133 | my @valid = keys %opt_params; | ||
| 134 | croak "invalid parameter(s): @invalid (valid: @valid)"; | ||
| 135 | } | ||
| 136 | |||
| 137 | croak "'ishelp' can only be applied to Bool opts" | ||
| 138 | if $params->{ishelp} and $params->{isa} ne 'Bool'; | ||
| 139 | |||
| 140 | croak "unknown type: $params->{isa}" | ||
| 141 | unless exists $opt_isa{ $params->{isa} }; | ||
| 142 | |||
| 143 | $params = { %opt_params, %$params }; | ||
| 144 | $params->{package} = $package; | ||
| 145 | $params->{name} = $name; | ||
| 146 | $params->{length} = length $name; | ||
| 147 | $params->{acount} = do { my @tmp = split( '|', $params->{alias} ) }; | ||
| 148 | $params->{type} = 'opt'; | ||
| 149 | $params->{ISA} = $params->{name}; | ||
| 150 | |||
| 151 | if ( ( my $dashed = $params->{name} ) =~ s/_/-/g ) { | ||
| 152 | $params->{dashed} = $dashed; | ||
| 153 | $params->{ISA} .= '|' . $dashed; | ||
| 154 | } | ||
| 155 | |||
| 156 | $params->{ISA} .= '|' . $params->{alias} if $params->{alias}; | ||
| 157 | $params->{ISA} .= $opt_isa{ $params->{isa} }; | ||
| 158 | |||
| 159 | push( @{ $opts{$package} }, $params ); | ||
| 160 | $args{$package} ||= []; | ||
| 161 | $seen{$package}->{$name}++; | ||
| 162 | |||
| 163 | return; | ||
| 164 | } | ||
| 165 | |||
| 166 | # ------------------------------------------------------------------------ | ||
| 167 | # Argument definition | ||
| 168 | # ------------------------------------------------------------------------ | ||
| 169 | my %arg_params = ( | ||
| 170 | isa => undef, | ||
| 171 | comment => undef, | ||
| 172 | required => undef, | ||
| 173 | default => undef, | ||
| 174 | greedy => undef, | ||
| 175 | fallback => undef, | ||
| 176 | ); | ||
| 177 | |||
| 178 | my @arg_required = (qw/isa comment/); | ||
| 179 | |||
| 180 | my %arg_isa = ( | ||
| 181 | 'Str' => '=s', | ||
| 182 | 'Int' => '=i', | ||
| 183 | 'Num' => '=f', | ||
| 184 | 'ArrayRef' => '=s@', | ||
| 185 | 'HashRef' => '=s%', | ||
| 186 | 'SubCmd' => '=s', | ||
| 187 | ); | ||
| 188 | |||
| 189 | sub arg { | ||
| 190 | my $name = shift; | ||
| 191 | my $params = {@_}; | ||
| 192 | my $caller = caller; | ||
| 193 | my $package = $caller{$caller} || $caller; | ||
| 194 | |||
| 195 | croak 'usage: arg $name => (%parameters)' unless $name; | ||
| 196 | croak "'$name' already defined" if $seen{$package}->{$name}; | ||
| 197 | |||
| 198 | if ( my @missing = grep { !exists $params->{$_} } @arg_required ) { | ||
| 199 | croak "missing required parameter(s): @missing"; | ||
| 200 | } | ||
| 201 | |||
| 202 | if ( my @invalid = grep { !exists $arg_params{$_} } keys %$params ) { | ||
| 203 | my @valid = keys %arg_params; | ||
| 204 | croak "invalid parameter(s): @invalid (valid: @valid)"; | ||
| 205 | } | ||
| 206 | |||
| 207 | croak "unknown type: $params->{isa}" | ||
| 208 | unless exists $arg_isa{ $params->{isa} }; | ||
| 209 | |||
| 210 | croak "'default' and 'required' cannot be used together" | ||
| 211 | if defined $params->{default} and defined $params->{required}; | ||
| 212 | |||
| 213 | croak "'fallback' only valid with isa 'SubCmd'" | ||
| 214 | if $params->{fallback} and $params->{isa} ne 'SubCmd'; | ||
| 215 | |||
| 216 | croak "fallback must be a hashref" | ||
| 217 | if defined $params->{fallback} && ref $params->{fallback} ne 'HASH'; | ||
| 218 | |||
| 219 | $params->{package} = $package; | ||
| 220 | $params->{name} = $name; | ||
| 221 | $params->{length} = length $name; | ||
| 222 | $params->{acount} = 0; | ||
| 223 | $params->{type} = 'arg'; | ||
| 224 | $params->{ISA} = $params->{name} . $arg_isa{ $params->{isa} }; | ||
| 225 | |||
| 226 | push( @{ $args{$package} }, $params ); | ||
| 227 | $opts{$package} ||= []; | ||
| 228 | $seen{$package}->{$name}++; | ||
| 229 | |||
| 230 | if ( $params->{fallback} ) { | ||
| 231 | my $p = $package . '::' . uc $params->{fallback}->{name}; | ||
| 232 | $p =~ s/-/_/g; | ||
| 233 | $opts{$p} = []; | ||
| 234 | $args{$p} = []; | ||
| 235 | $desc{$p} = $params->{fallback}->{comment}; | ||
| 236 | } | ||
| 237 | |||
| 238 | return; | ||
| 239 | } | ||
| 240 | |||
| 241 | # ------------------------------------------------------------------------ | ||
| 242 | # Usage message generation | ||
| 243 | # ------------------------------------------------------------------------ | ||
| 244 | |||
| 245 | sub _usage { | ||
| 246 | my $caller = shift; | ||
| 247 | my $error = shift; | ||
| 248 | my $ishelp = shift; | ||
| 249 | my $terminal = -t STDOUT; | ||
| 250 | my $red = ( $COLOUR && $terminal ) ? "\e[0;31m" : ''; | ||
| 251 | my $yellow = ''; #( $COLOUR && $terminal ) ? "\e[0;33m" : ''; | ||
| 252 | my $grey = ''; #( $COLOUR && $terminal ) ? "\e[1;30m" : ''; | ||
| 253 | my $reset = ( $COLOUR && $terminal ) ? "\e[0m" : ''; | ||
| 254 | my $parent = $caller; | ||
| 255 | my @args = @{ $args{$caller} }; | ||
| 256 | my @opts = @{ $opts{$caller} }; | ||
| 257 | my @parents; | ||
| 258 | my @usage; | ||
| 259 | my @uargs; | ||
| 260 | my @uopts; | ||
| 261 | my $usage; | ||
| 262 | |||
| 263 | require File::Basename; | ||
| 264 | my $me = File::Basename::basename( defined &static::list ? $^X : $0 ); | ||
| 265 | |||
| 266 | if ($error) { | ||
| 267 | $usage .= "${red}error:$reset $error\n\n"; | ||
| 268 | } | ||
| 269 | |||
| 270 | $usage .= $yellow . ( $ishelp ? 'help:' : 'usage:' ) . $reset . ' ' . $me; | ||
| 271 | |||
| 272 | while ( $parent =~ s/(.*)::(.*)/$1/ ) { | ||
| 273 | last unless $seen{$parent}; | ||
| 274 | ( my $name = $2 ) =~ s/_/-/g; | ||
| 275 | unshift( @parents, $name ); | ||
| 276 | unshift( @opts, @{ $opts{$parent} } ); | ||
| 277 | } | ||
| 278 | |||
| 279 | $usage .= ' ' . join( ' ', @parents ) if @parents; | ||
| 280 | |||
| 281 | my $last = $args[$#args]; | ||
| 282 | |||
| 283 | if ($last) { | ||
| 284 | foreach my $def (@args) { | ||
| 285 | $usage .= ' '; | ||
| 286 | $usage .= '[' unless $def->{required}; | ||
| 287 | $usage .= uc $def->{name}; | ||
| 288 | $usage .= '...' if $def->{greedy}; | ||
| 289 | $usage .= ']' unless $def->{required}; | ||
| 290 | push( @uargs, [ uc $def->{name}, $def->{comment} ] ); | ||
| 291 | } | ||
| 292 | } | ||
| 293 | |||
| 294 | $usage .= ' [OPTIONS...]' if @opts; | ||
| 295 | |||
| 296 | $usage .= "\n"; | ||
| 297 | |||
| 298 | $usage .= "\n ${grey}Synopsis:$reset\n $desc{$caller}\n" | ||
| 299 | if $ishelp and $desc{$caller}; | ||
| 300 | |||
| 301 | if ( $ishelp and my $version = $caller->VERSION ) { | ||
| 302 | $usage .= "\n ${grey}Version:$reset\n $version\n"; | ||
| 303 | } | ||
| 304 | |||
| 305 | if ( $last && $last->{isa} eq 'SubCmd' ) { | ||
| 306 | $usage .= "\n ${grey}" . ucfirst( $last->{name} ) . ":$reset\n"; | ||
| 307 | |||
| 308 | my @subcommands = @{ $last->{subcommands} }; | ||
| 309 | |||
| 310 | push( @subcommands, uc $last->{fallback}->{name} ) | ||
| 311 | if ( | ||
| 312 | exists $last->{fallback} | ||
| 313 | && ( $ishelp | ||
| 314 | or !$last->{fallback}->{hidden} ) | ||
| 315 | ); | ||
| 316 | |||
| 317 | @subcommands = sort @subcommands if $SORT; | ||
| 318 | |||
| 319 | foreach my $subcommand (@subcommands) { | ||
| 320 | my $pkg = $last->{package} . '::' . $subcommand; | ||
| 321 | $pkg =~ s/-/_/g; | ||
| 322 | next if $hidden{$pkg} and !$ishelp; | ||
| 323 | push( @usage, [ $subcommand, $desc{$pkg} ] ); | ||
| 324 | } | ||
| 325 | |||
| 326 | } | ||
| 327 | |||
| 328 | @opts = sort { $a->{name} cmp $b->{name} } @opts if $SORT; | ||
| 329 | |||
| 330 | foreach my $opt (@opts) { | ||
| 331 | next if $opt->{hidden} and !$ishelp; | ||
| 332 | |||
| 333 | ( my $name = $opt->{name} ) =~ s/_/-/g; | ||
| 334 | |||
| 335 | if ( $opt->{isa} eq 'Bool' and $opt->{default} ) { | ||
| 336 | $name = 'no-' . $name; | ||
| 337 | } | ||
| 338 | |||
| 339 | my $default = ''; | ||
| 340 | if ( $PRINT_DEFAULT && defined $opt->{default} and !$opt->{ishelp} ) { | ||
| 341 | my $value = | ||
| 342 | ref $opt->{default} eq 'CODE' | ||
| 343 | ? $opt->{default}->( {%$opt} ) | ||
| 344 | : $opt->{default}; | ||
| 345 | if ( $opt->{isa} eq 'Bool' ) { | ||
| 346 | $value = $value ? 'true' : 'false'; | ||
| 347 | } | ||
| 348 | $default = " [default: $value]"; | ||
| 349 | } | ||
| 350 | |||
| 351 | if ($PRINT_ISA) { | ||
| 352 | if ( $opt->{isa_name} ) { | ||
| 353 | $name .= '=' . uc $opt->{isa_name}; | ||
| 354 | } | ||
| 355 | elsif ($opt->{isa} eq 'Str' | ||
| 356 | || $opt->{isa} eq 'HashRef' | ||
| 357 | || $opt->{isa} eq 'ArrayRef' ) | ||
| 358 | { | ||
| 359 | $name .= '=STR'; | ||
| 360 | } | ||
| 361 | elsif ( $opt->{isa} eq 'Int' ) { | ||
| 362 | $name .= '=INT'; | ||
| 363 | } | ||
| 364 | elsif ( $opt->{isa} eq 'Num' ) { | ||
| 365 | $name .= '=NUM'; | ||
| 366 | } | ||
| 367 | } | ||
| 368 | |||
| 369 | $name .= ',' if $opt->{alias}; | ||
| 370 | push( | ||
| 371 | @uopts, | ||
| 372 | [ | ||
| 373 | '--' . $name, | ||
| 374 | $opt->{alias} | ||
| 375 | ? '-' . $opt->{alias} | ||
| 376 | : '', | ||
| 377 | $opt->{comment} . $default | ||
| 378 | ] | ||
| 379 | ); | ||
| 380 | } | ||
| 381 | |||
| 382 | if (@uopts) { | ||
| 383 | my $w1 = max( map { length $_->[0] } @uopts ); | ||
| 384 | my $fmt = '%-' . $w1 . "s %s"; | ||
| 385 | |||
| 386 | @uopts = map { [ sprintf( $fmt, $_->[0], $_->[1] ), $_->[2] ] } @uopts; | ||
| 387 | } | ||
| 388 | |||
| 389 | my $w1 = max( map { length $_->[0] } @usage, @uargs, @uopts ); | ||
| 390 | my $format = ' %-' . $w1 . "s %s\n"; | ||
| 391 | |||
| 392 | if (@usage) { | ||
| 393 | foreach my $row (@usage) { | ||
| 394 | $usage .= sprintf( $format, @$row ); | ||
| 395 | } | ||
| 396 | } | ||
| 397 | if ( @uargs and $last->{isa} ne 'SubCmd' ) { | ||
| 398 | $usage .= "\n ${grey}Arguments:$reset\n"; | ||
| 399 | foreach my $row (@uargs) { | ||
| 400 | $usage .= sprintf( $format, @$row ); | ||
| 401 | } | ||
| 402 | } | ||
| 403 | if (@uopts) { | ||
| 404 | $usage .= "\n ${grey}Options:$reset\n"; | ||
| 405 | foreach my $row (@uopts) { | ||
| 406 | $usage .= sprintf( $format, @$row ); | ||
| 407 | } | ||
| 408 | } | ||
| 409 | |||
| 410 | $usage .= "\n"; | ||
| 411 | return bless( \$usage, 'Getopt::Args::Usage' ); | ||
| 412 | } | ||
| 413 | |||
| 414 | sub _synopsis { | ||
| 415 | my $caller = shift; | ||
| 416 | my $parent = $caller; | ||
| 417 | my @args = @{ $args{$caller} }; | ||
| 418 | my @parents; | ||
| 419 | |||
| 420 | require File::Basename; | ||
| 421 | my $usage = File::Basename::basename($0); | ||
| 422 | |||
| 423 | while ( $parent =~ s/(.*)::(.*)/$1/ ) { | ||
| 424 | last unless $seen{$parent}; | ||
| 425 | ( my $name = $2 ) =~ s/_/-/g; | ||
| 426 | unshift( @parents, $name ); | ||
| 427 | } | ||
| 428 | |||
| 429 | $usage .= ' ' . join( ' ', @parents ) if @parents; | ||
| 430 | |||
| 431 | if ( my $last = $args[$#args] ) { | ||
| 432 | foreach my $def (@args) { | ||
| 433 | $usage .= ' '; | ||
| 434 | $usage .= '[' unless $def->{required}; | ||
| 435 | $usage .= uc $def->{name}; | ||
| 436 | $usage .= '...' if $def->{greedy}; | ||
| 437 | $usage .= ']' unless $def->{required}; | ||
| 438 | } | ||
| 439 | } | ||
| 440 | |||
| 441 | return 'usage: ' . $usage . "\n"; | ||
| 442 | } | ||
| 443 | |||
| 444 | sub usage { | ||
| 445 | my $caller = caller; | ||
| 446 | return _usage( $caller, @_ ); | ||
| 447 | } | ||
| 448 | |||
| 449 | # ------------------------------------------------------------------------ | ||
| 450 | # Option/Argument processing | ||
| 451 | # ------------------------------------------------------------------------ | ||
| 452 | sub _optargs { | ||
| 453 | my $caller = shift; | ||
| 454 | my $source = \@_; | ||
| 455 | my $source_hash = {}; | ||
| 456 | my $package = $caller; | ||
| 457 | |||
| 458 | if ( !@_ and @ARGV ) { | ||
| 459 | my $CODESET = | ||
| 460 | eval { require I18N::Langinfo; I18N::Langinfo::CODESET() }; | ||
| 461 | |||
| 462 | if ($CODESET) { | ||
| 463 | my $codeset = I18N::Langinfo::langinfo($CODESET); | ||
| 464 | $_ = decode( $codeset, $_ ) for @ARGV; | ||
| 465 | } | ||
| 466 | |||
| 467 | $source = \@ARGV; | ||
| 468 | } | ||
| 469 | else { | ||
| 470 | $source_hash = { map { %$_ } grep { ref $_ eq 'HASH' } @$source }; | ||
| 471 | $source = [ grep { ref $_ ne 'HASH' } @$source ]; | ||
| 472 | } | ||
| 473 | |||
| 474 | map { Carp::croak('_optargs argument undefined!') if !defined $_ } @$source; | ||
| 475 | |||
| 476 | croak "no option or argument defined for $caller" | ||
| 477 | unless exists $opts{$package} | ||
| 478 | or exists $args{$package}; | ||
| 479 | |||
| 480 | Getopt::Long::Configure(qw/pass_through no_auto_abbrev no_ignore_case/); | ||
| 481 | |||
| 482 | my @config = ( @{ $opts{$package} }, @{ $args{$package} } ); | ||
| 483 | |||
| 484 | my $ishelp; | ||
| 485 | my $missing_required; | ||
| 486 | my $optargs = {}; | ||
| 487 | my @coderef_default_keys; | ||
| 488 | |||
| 489 | while ( my $try = shift @config ) { | ||
| 490 | my $result; | ||
| 491 | |||
| 492 | if ( $try->{type} eq 'opt' ) { | ||
| 493 | if ( exists $source_hash->{ $try->{name} } ) { | ||
| 494 | $result = delete $source_hash->{ $try->{name} }; | ||
| 495 | } | ||
| 496 | else { | ||
| 497 | GetOptionsFromArray( $source, $try->{ISA} => \$result ); | ||
| 498 | } | ||
| 499 | } | ||
| 500 | elsif ( $try->{type} eq 'arg' ) { | ||
| 501 | if (@$source) { | ||
| 502 | die _usage( $package, qq{Unknown option "$source->[0]"} ) | ||
| 503 | if $source->[0] =~ m/^--\S/; | ||
| 504 | |||
| 505 | die _usage( $package, qq{Unknown option "$source->[0]"} ) | ||
| 506 | if $source->[0] =~ m/^-\S/ | ||
| 507 | and !( | ||
| 508 | $source->[0] =~ m/^-\d/ and ( $try->{isa} ne 'Num' | ||
| 509 | or $try->{isa} ne 'Int' ) | ||
| 510 | ); | ||
| 511 | |||
| 512 | if ( $try->{greedy} ) { | ||
| 513 | my @later; | ||
| 514 | if ( @config and @$source > @config ) { | ||
| 515 | push( @later, pop @$source ) for @config; | ||
| 516 | } | ||
| 517 | |||
| 518 | if ( $try->{isa} eq 'ArrayRef' ) { | ||
| 519 | $result = [@$source]; | ||
| 520 | } | ||
| 521 | elsif ( $try->{isa} eq 'HashRef' ) { | ||
| 522 | $result = { map { split /=/, $_ } @$source }; | ||
| 523 | } | ||
| 524 | else { | ||
| 525 | $result = "@$source"; | ||
| 526 | } | ||
| 527 | |||
| 528 | shift @$source while @$source; | ||
| 529 | push( @$source, @later ); | ||
| 530 | } | ||
| 531 | else { | ||
| 532 | if ( $try->{isa} eq 'ArrayRef' ) { | ||
| 533 | $result = [ shift @$source ]; | ||
| 534 | } | ||
| 535 | elsif ( $try->{isa} eq 'HashRef' ) { | ||
| 536 | $result = { split /=/, shift @$source }; | ||
| 537 | } | ||
| 538 | else { | ||
| 539 | $result = shift @$source; | ||
| 540 | } | ||
| 541 | } | ||
| 542 | |||
| 543 | # TODO: type check using Param::Utils? | ||
| 544 | } | ||
| 545 | elsif ( exists $source_hash->{ $try->{name} } ) { | ||
| 546 | $result = delete $source_hash->{ $try->{name} }; | ||
| 547 | } | ||
| 548 | elsif ( $try->{required} and !$ishelp ) { | ||
| 549 | $missing_required++; | ||
| 550 | next; | ||
| 551 | } | ||
| 552 | |||
| 553 | if ( $try->{isa} eq 'SubCmd' and $result ) { | ||
| 554 | |||
| 555 | # look up abbreviated words | ||
| 556 | if ($ABBREV) { | ||
| 557 | require Text::Abbrev; | ||
| 558 | my %words = | ||
| 559 | map { m/^$package\:\:(\w+)$/; $1 => 1 } | ||
| 560 | grep { m/^$package\:\:(\w+)$/ } | ||
| 561 | keys %seen; | ||
| 562 | my %abbrev = Text::Abbrev::abbrev( keys %words ); | ||
| 563 | $result = $abbrev{$result} if defined $abbrev{$result}; | ||
| 564 | } | ||
| 565 | |||
| 566 | my $newpackage = $package . '::' . $result; | ||
| 567 | $newpackage =~ s/-/_/g; | ||
| 568 | |||
| 569 | if ( exists $seen{$newpackage} ) { | ||
| 570 | $package = $newpackage; | ||
| 571 | @config = grep { $_->{type} eq 'opt' } @config; | ||
| 572 | push( @config, @{ $opts{$package} }, @{ $args{$package} } ); | ||
| 573 | } | ||
| 574 | elsif ( !$ishelp ) { | ||
| 575 | if ( $try->{fallback} ) { | ||
| 576 | unshift @$source, $result; | ||
| 577 | $try->{fallback}->{type} = 'arg'; | ||
| 578 | unshift( @config, $try->{fallback} ); | ||
| 579 | next; | ||
| 580 | } | ||
| 581 | else { | ||
| 582 | die _usage( $package, | ||
| 583 | "Unknown " . uc( $try->{name} ) . qq{ "$result"} ); | ||
| 584 | } | ||
| 585 | } | ||
| 586 | |||
| 587 | $result = undef; | ||
| 588 | } | ||
| 589 | |||
| 590 | } | ||
| 591 | |||
| 592 | if ( defined $result ) { | ||
| 593 | $optargs->{ $try->{name} } = $result; | ||
| 594 | } | ||
| 595 | elsif ( defined $try->{default} ) { | ||
| 596 | push( @coderef_default_keys, $try->{name} ) | ||
| 597 | if ref $try->{default} eq 'CODE'; | ||
| 598 | $optargs->{ $try->{name} } = $result = $try->{default}; | ||
| 599 | } | ||
| 600 | |||
| 601 | $ishelp = 1 if $result and $try->{ishelp}; | ||
| 602 | |||
| 603 | } | ||
| 604 | |||
| 605 | if ($ishelp) { | ||
| 606 | die _usage( $package, undef, 1 ); | ||
| 607 | } | ||
| 608 | elsif ($missing_required) { | ||
| 609 | die _usage($package); | ||
| 610 | } | ||
| 611 | elsif (@$source) { | ||
| 612 | die _usage( $package, "Unexpected options or arguments: @$source" ); | ||
| 613 | } | ||
| 614 | elsif ( my @unexpected = keys %$source_hash ) { | ||
| 615 | die _usage( $package, | ||
| 616 | "Unexpected HASH options or arguments: @unexpected" ); | ||
| 617 | } | ||
| 618 | |||
| 619 | # Re-calculate the default if it was a subref | ||
| 620 | foreach my $key (@coderef_default_keys) { | ||
| 621 | $optargs->{$key} = $optargs->{$key}->( {%$optargs} ); | ||
| 622 | } | ||
| 623 | |||
| 624 | return ( $package, $optargs ); | ||
| 625 | } | ||
| 626 | |||
| 627 | sub optargs { | ||
| 628 | my $caller = caller; | ||
| 629 | |||
| 630 | carp "optargs() called from dispatch handler" | ||
| 631 | if $dispatching{$caller}; | ||
| 632 | |||
| 633 | my ( $package, $optargs ) = _optargs( $caller, @_ ); | ||
| 634 | return $optargs; | ||
| 635 | } | ||
| 636 | |||
| 637 | sub class_optargs { | ||
| 638 | my $caller = shift; | ||
| 639 | |||
| 640 | croak 'dispatch($class, [@argv])' unless $caller; | ||
| 641 | carp "optargs_class() called from dispatch handler" | ||
| 642 | if $dispatching{$caller}; | ||
| 643 | |||
| 644 | die $@ unless eval "require $caller;"; | ||
| 645 | |||
| 646 | my ( $class, $optargs ) = _optargs( $caller, @_ ); | ||
| 647 | |||
| 648 | croak $@ unless eval "require $class;1;"; | ||
| 649 | return ( $class, $optargs ); | ||
| 650 | } | ||
| 651 | |||
| 652 | sub dispatch { | ||
| 653 | my $method = shift; | ||
| 654 | my $class = shift; | ||
| 655 | |||
| 656 | croak 'dispatch($method, $class, [@argv])' unless $method and $class; | ||
| 657 | croak $@ unless eval "require $class;1;"; | ||
| 658 | |||
| 659 | my ( $package, $optargs ) = class_optargs( $class, @_ ); | ||
| 660 | |||
| 661 | my $sub = $package->can($method); | ||
| 662 | die "Can't find method $method via package $package" unless $sub; | ||
| 663 | |||
| 664 | $dispatching{$class}++; | ||
| 665 | my @results = $sub->($optargs); | ||
| 666 | $dispatching{$class}--; | ||
| 667 | return @results if wantarray; | ||
| 668 | return $results[0]; | ||
| 669 | } | ||
| 670 | |||
| 671 | package Getopt::Args::Usage; | ||
| 672 | use overload | ||
| 673 | bool => sub { 1 }, | ||
| 674 | '""' => sub { ${ $_[0] } }, | ||
| 675 | fallback => 1; | ||
| 676 | |||
| 677 | 1; | ||
| 678 | |||
| 679 | __END__ |