| 1 |
package App::Yath::Options; |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
|
| 5 |
our $VERSION = '1.000179'; |
| 6 |
|
| 7 |
use Carp qw/croak confess/; |
| 8 |
use Scalar::Util qw/blessed/; |
| 9 |
use Test2::Harness::Util qw/mod2file/; |
| 10 |
|
| 11 |
use App::Yath::Option(); |
| 12 |
use Test2::Harness::Settings(); |
| 13 |
|
| 14 |
use Test2::Harness::Util::HashBase qw{ |
| 15 |
<all <lookup |
| 16 |
|
| 17 |
<pre_list <cmd_list <post_list |
| 18 |
|
| 19 |
<post_list_sorted |
| 20 |
|
| 21 |
<settings |
| 22 |
|
| 23 |
<args |
| 24 |
<command_class |
| 25 |
|
| 26 |
<pending_pre <pending_cmd <pending_post |
| 27 |
|
| 28 |
<used_plugins |
| 29 |
|
| 30 |
<included |
| 31 |
|
| 32 |
<set_by_cli |
| 33 |
}; |
| 34 |
|
| 35 |
sub import { |
| 36 |
my $class = shift; |
| 37 |
my $caller = caller(); |
| 38 |
|
| 39 |
croak "$caller already has an 'options' method" |
| 40 |
if defined(&{"$caller\::options"}); |
| 41 |
|
| 42 |
my @common; |
| 43 |
my $instance; |
| 44 |
my $options = sub { ($instance //= $class->new()) }; |
| 45 |
my $option = sub { ($instance //= $class->new())->_option([caller()], shift(@_), @common ? (%{$common[-1]}) : (), @_) }; |
| 46 |
my $include = sub { ($instance //= $class->new())->include_from(@_) }; |
| 47 |
|
| 48 |
my $post = sub { |
| 49 |
my $cb = pop; |
| 50 |
my $weight = shift // 0; |
| 51 |
my ($applicable) = @_; |
| 52 |
|
| 53 |
$applicable //= $common[-1]->{applicable} if @common; |
| 54 |
|
| 55 |
croak "You must provide a callback coderef" unless $cb && ref($cb) eq 'CODE'; |
| 56 |
|
| 57 |
($instance //= $class->new())->_post($weight, $applicable, $cb); |
| 58 |
}; |
| 59 |
|
| 60 |
my $group = sub { |
| 61 |
my ($set, $sub) = @_; |
| 62 |
|
| 63 |
my $common = {@common ? (%{$common[-1]}) : (), %$set}; |
| 64 |
|
| 65 |
if (my $class = $common->{builds}) { |
| 66 |
require(mod2file($class)); |
| 67 |
} |
| 68 |
|
| 69 |
push @common => $common; |
| 70 |
my $ok = eval { $sub->(); 1 }; |
| 71 |
my $err = $@; |
| 72 |
pop @common; |
| 73 |
|
| 74 |
die $err unless $ok; |
| 75 |
}; |
| 76 |
|
| 77 |
{ |
| 78 |
no strict 'refs'; |
| 79 |
*{"$caller\::post"} = $post; |
| 80 |
*{"$caller\::option"} = $option; |
| 81 |
*{"$caller\::options"} = $options; |
| 82 |
*{"$caller\::option_group"} = $group; |
| 83 |
*{"$caller\::include_options"} = $include; |
| 84 |
} |
| 85 |
|
| 86 |
return 1; |
| 87 |
} |
| 88 |
|
| 89 |
sub init { |
| 90 |
my $self = shift; |
| 91 |
|
| 92 |
$self->{+ALL} //= []; |
| 93 |
$self->{+LOOKUP} //= {}; |
| 94 |
|
| 95 |
$self->{+USED_PLUGINS} //= []; |
| 96 |
|
| 97 |
$self->{+PRE_LIST} //= []; |
| 98 |
$self->{+CMD_LIST} //= []; |
| 99 |
$self->{+POST_LIST} //= []; |
| 100 |
|
| 101 |
$self->{+SETTINGS} //= Test2::Harness::Settings->new(); |
| 102 |
|
| 103 |
$self->{+INCLUDED} //= {}; |
| 104 |
|
| 105 |
$self->{+SET_BY_CLI} //= {}; |
| 106 |
|
| 107 |
return $self; |
| 108 |
} |
| 109 |
|
| 110 |
sub option { |
| 111 |
my $self = shift; |
| 112 |
$self->_option([caller()], @_); |
| 113 |
} |
| 114 |
|
| 115 |
sub include { |
| 116 |
my $self = shift; |
| 117 |
my ($inc) = @_; |
| 118 |
|
| 119 |
croak "Include must be an instance of ${ \__PACKAGE__ }, got ${ defined($inc) ? \qq['$inc'] : \'undef' }" |
| 120 |
unless $inc && blessed($inc) && $inc->isa(__PACKAGE__); |
| 121 |
|
| 122 |
$self->include_option($_) for @{$inc->all}; |
| 123 |
|
| 124 |
$self->{+POST_LIST_SORTED} = 0; |
| 125 |
push @{$self->{+POST_LIST}} => @{$inc->post_list}; |
| 126 |
|
| 127 |
return; |
| 128 |
} |
| 129 |
|
| 130 |
sub include_from { |
| 131 |
my $self = shift; |
| 132 |
|
| 133 |
for my $pkg (@_) { |
| 134 |
require(mod2file($pkg)) unless $pkg->can('options'); |
| 135 |
|
| 136 |
next unless $pkg->can('options'); |
| 137 |
my $options = $pkg->options or next; |
| 138 |
$self->include($options); |
| 139 |
|
| 140 |
$self->{+INCLUDED}->{$pkg}++; |
| 141 |
$self->{+INCLUDED}->{$_}++ for keys %{$options->included}; |
| 142 |
} |
| 143 |
|
| 144 |
return; |
| 145 |
} |
| 146 |
|
| 147 |
sub populate_pre_defaults { |
| 148 |
my $self = shift; |
| 149 |
|
| 150 |
for my $opt (@{$self->_pre_command_options}) { |
| 151 |
my $slot = $opt->option_slot($self->{+SETTINGS}); |
| 152 |
my $val = $opt->get_default($self->{+SETTINGS}); |
| 153 |
$$slot //= $val; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
sub populate_cmd_defaults { |
| 158 |
my $self = shift; |
| 159 |
|
| 160 |
croak "The 'command_class' attribute has not yet been set" |
| 161 |
unless $self->{+COMMAND_CLASS}; |
| 162 |
|
| 163 |
for my $opt (@{$self->_command_options()}) { |
| 164 |
my $slot = $opt->option_slot($self->{+SETTINGS}); |
| 165 |
my $val = $opt->get_default($self->{+SETTINGS}); |
| 166 |
$$slot //= $val; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
sub grab_pre_command_opts { |
| 171 |
my $self = shift; |
| 172 |
my %config = @_; |
| 173 |
|
| 174 |
$self->populate_pre_defaults(); |
| 175 |
|
| 176 |
unshift @{$self->{+PENDING_PRE} //= []} => $self->_grab_opts( |
| 177 |
'_pre_command_options', |
| 178 |
'pre-command', |
| 179 |
stop_at_non_opt => 1, |
| 180 |
passthrough => 1, |
| 181 |
%config, |
| 182 |
); |
| 183 |
} |
| 184 |
|
| 185 |
sub process_pre_command_opts { |
| 186 |
my $self = shift; |
| 187 |
return unless $self->{+PENDING_PRE}; |
| 188 |
$self->_process_opts(delete $self->{+PENDING_PRE}); |
| 189 |
} |
| 190 |
|
| 191 |
sub set_command_class { |
| 192 |
my $self = shift; |
| 193 |
my ($in) = @_; |
| 194 |
|
| 195 |
croak "Command class has already been set" |
| 196 |
if $self->{+COMMAND_CLASS}; |
| 197 |
|
| 198 |
my $class = blessed($in) || $in; |
| 199 |
|
| 200 |
croak "Invalid command class: $class" |
| 201 |
unless $class->isa('App::Yath::Command'); |
| 202 |
|
| 203 |
$self->include_from($class) if $class->can('options'); |
| 204 |
|
| 205 |
return $self->{+COMMAND_CLASS} = $class; |
| 206 |
} |
| 207 |
|
| 208 |
sub set_args { |
| 209 |
my $self = shift; |
| 210 |
my ($in) = @_; |
| 211 |
|
| 212 |
croak "'args' has already been set" |
| 213 |
if $self->{+ARGS}; |
| 214 |
|
| 215 |
return $self->{+ARGS} = $in; |
| 216 |
} |
| 217 |
|
| 218 |
sub grab_command_opts { |
| 219 |
my $self = shift; |
| 220 |
my %config = @_; |
| 221 |
|
| 222 |
croak "The 'command_class' attribute has not yet been set" |
| 223 |
unless $self->{+COMMAND_CLASS}; |
| 224 |
|
| 225 |
$self->populate_cmd_defaults(); |
| 226 |
|
| 227 |
push @{$self->{+PENDING_CMD} //= []} => $self->_grab_opts( |
| 228 |
'_command_options', |
| 229 |
"command (" . $self->{+COMMAND_CLASS}->name . ")", |
| 230 |
%config, |
| 231 |
); |
| 232 |
} |
| 233 |
|
| 234 |
sub process_command_opts { |
| 235 |
my $self = shift; |
| 236 |
return unless $self->{+PENDING_CMD}; |
| 237 |
$self->_process_opts(delete $self->{+PENDING_CMD}); |
| 238 |
} |
| 239 |
|
| 240 |
sub process_option_post_actions { |
| 241 |
my $self = shift; |
| 242 |
my ($cmd) = @_; |
| 243 |
|
| 244 |
croak "The 'args' attribute has not yet been set" |
| 245 |
unless $self->{+ARGS}; |
| 246 |
|
| 247 |
if ($cmd) { |
| 248 |
croak "The 'command_class' attribute has not yet been set" |
| 249 |
unless $self->{+COMMAND_CLASS}; |
| 250 |
|
| 251 |
croak "The process_option_post_actions requires an App::Yath::Command instance, got: " . ($cmd // "undef") |
| 252 |
unless blessed($cmd) && $cmd->isa('App::Yath::Command'); |
| 253 |
|
| 254 |
croak "The command '$cmd' dos not match the expected class '$self->{+COMMAND_CLASS}'" |
| 255 |
unless blessed($cmd) eq $self->{+COMMAND_CLASS}; |
| 256 |
} |
| 257 |
|
| 258 |
unless ($self->{+POST_LIST_SORTED}++) { |
| 259 |
@{$self->{+POST_LIST}} = sort { $a->[0] <=> $b->[0] } @{$self->{+POST_LIST}}; |
| 260 |
} |
| 261 |
|
| 262 |
for my $post (@{$self->{+POST_LIST}}) { |
| 263 |
next if $post->[1] && !$post->[1]->($post->[2], $self); |
| 264 |
$post->[2]->( |
| 265 |
options => $self, |
| 266 |
args => $self->{+ARGS}, |
| 267 |
settings => $self->{+SETTINGS}, |
| 268 |
$cmd ? (command => $cmd) : (), |
| 269 |
); |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
sub _pre_command_options { $_[0]->{+PRE_LIST} } |
| 274 |
|
| 275 |
sub _command_options { |
| 276 |
my $self = shift; |
| 277 |
|
| 278 |
my $class = $self->{+COMMAND_CLASS} or croak "The 'command_class' attribute has not yet been set"; |
| 279 |
|
| 280 |
my $cmd = $class->name; |
| 281 |
my $cmd_options = $self->{+CMD_LIST} // []; |
| 282 |
my $pre_options = $self->{+PRE_LIST} // []; |
| 283 |
|
| 284 |
return [grep { $_->applicable($self) } @$cmd_options, @$pre_options]; |
| 285 |
} |
| 286 |
|
| 287 |
sub _process_opts { |
| 288 |
my $self = shift; |
| 289 |
my ($list) = @_; |
| 290 |
|
| 291 |
while (my $opt_set = shift @$list) { |
| 292 |
my ($opt, $meth, @args) = @$opt_set; |
| 293 |
$opt->$meth(@args, $self->{+SETTINGS}, $self, $list); |
| 294 |
$self->{+SET_BY_CLI}->{$opt->prefix}->{$opt->field}++; |
| 295 |
push @{$self->{+USED_PLUGINS}} => $opt->from_plugin if $opt->from_plugin; |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
sub _parse_long_option { |
| 300 |
my $self = shift; |
| 301 |
my ($arg) = @_; |
| 302 |
|
| 303 |
$arg =~ m/^--((?:no-)?([^=]+))(=(.*))?$/ or confess "Invalid long option: $arg"; |
| 304 |
|
| 305 |
#return (main, full, val); |
| 306 |
return ($2, $1, $3 ? $4 // '' : undef); |
| 307 |
} |
| 308 |
|
| 309 |
sub _parse_short_option { |
| 310 |
my $self = shift; |
| 311 |
my ($arg) = @_; |
| 312 |
|
| 313 |
$arg =~ m/^-([^-])(=)?(.+)?$/ or confess "Invalid short option: $arg"; |
| 314 |
|
| 315 |
#return (main, remain, assign); |
| 316 |
return ($1, $3, $2); |
| 317 |
} |
| 318 |
|
| 319 |
sub _handle_long_option { |
| 320 |
my $self = shift; |
| 321 |
my ($arg, $lookup, $args) = @_; |
| 322 |
|
| 323 |
my ($main, $full, $val) = $self->_parse_long_option($arg); |
| 324 |
|
| 325 |
my $opt; |
| 326 |
if ($opt = $lookup->{long}->{$full}) { |
| 327 |
if ($opt->requires_arg) { |
| 328 |
$val //= shift(@$args) // die "Option --$full requires an argument.\n"; |
| 329 |
} |
| 330 |
elsif($opt->allows_arg) { |
| 331 |
$val //= $opt->autofill // 1; |
| 332 |
} |
| 333 |
else { |
| 334 |
die "Option --$full does not take an argument\n" if defined $val; |
| 335 |
$val = 1; |
| 336 |
} |
| 337 |
|
| 338 |
return [$opt, 'handle', $val]; |
| 339 |
} |
| 340 |
elsif ($opt = $lookup->{long}->{$main}) { |
| 341 |
die "Option --$full does not take an argument\n" if defined $val; |
| 342 |
return [$opt, 'handle_negation']; |
| 343 |
} |
| 344 |
|
| 345 |
return undef; |
| 346 |
} |
| 347 |
|
| 348 |
sub _handle_short_option { |
| 349 |
my $self = shift; |
| 350 |
my ($arg, $lookup, $args) = @_; |
| 351 |
|
| 352 |
my ($main, $remain, $assign) = $self->_parse_short_option($arg); |
| 353 |
|
| 354 |
if (my $opt = $lookup->{short}->{$main}) { |
| 355 |
if ($opt->allows_arg) { |
| 356 |
my $val = $remain; |
| 357 |
|
| 358 |
$val //= '' if $assign; |
| 359 |
|
| 360 |
if ($opt->requires_arg) { |
| 361 |
$val //= shift(@$args) // die "Option -$main requires an argument.\n"; |
| 362 |
} |
| 363 |
else { |
| 364 |
$val //= $opt->autofill // 1; |
| 365 |
} |
| 366 |
|
| 367 |
$val //= 1; |
| 368 |
return [$opt, 'handle', $val]; |
| 369 |
} |
| 370 |
elsif ($assign) { |
| 371 |
die "Option -$main does not take an argument\n"; |
| 372 |
} |
| 373 |
elsif(defined($remain) && length($remain)) { |
| 374 |
unshift @$args => "-$remain"; |
| 375 |
} |
| 376 |
|
| 377 |
return [$opt, 'handle', 1]; |
| 378 |
} |
| 379 |
|
| 380 |
return undef; |
| 381 |
} |
| 382 |
|
| 383 |
my %ARG_ENDS = ('--' => 1, '::' => 1); |
| 384 |
|
| 385 |
sub _grab_opts { |
| 386 |
my $self = shift; |
| 387 |
my ($opt_fetch, $type, %config) = @_; |
| 388 |
|
| 389 |
croak "The opt_fetch callback is required" unless $opt_fetch; |
| 390 |
croak "The arg type is required" unless $type; |
| 391 |
|
| 392 |
my $args = $config{args} || $self->{+ARGS} or confess "The 'args' attribute has not yet been set"; |
| 393 |
|
| 394 |
my $lookup = $self->_build_lookup($self->$opt_fetch()); |
| 395 |
|
| 396 |
my (@keep_args, @opts); |
| 397 |
while (@$args) { |
| 398 |
my $arg = shift @$args; |
| 399 |
|
| 400 |
if ($ARG_ENDS{$arg}) { |
| 401 |
push @keep_args => $arg; |
| 402 |
last; |
| 403 |
} |
| 404 |
|
| 405 |
if (substr($arg, 0, 1) eq '-') { |
| 406 |
my $handler = (substr($arg, 1, 1) eq '-') ? '_handle_long_option' : '_handle_short_option'; |
| 407 |
if(my $opt_set = $self->$handler($arg, $lookup, $args)) { |
| 408 |
my ($opt, $action, @val) = @$opt_set; |
| 409 |
|
| 410 |
if (my $pre = $opt->pre_process) { |
| 411 |
$pre->( |
| 412 |
opt => $opt, |
| 413 |
options => $self, |
| 414 |
action => $action, |
| 415 |
type => $type, |
| 416 |
|
| 417 |
@val ? (val => $val[0]) : (), |
| 418 |
); |
| 419 |
} |
| 420 |
|
| 421 |
$lookup = $self->_build_lookup($self->$opt_fetch()) |
| 422 |
if $opt->adds_options; |
| 423 |
|
| 424 |
push @opts => $opt_set; |
| 425 |
next; |
| 426 |
} |
| 427 |
elsif (!$config{passthrough}) { |
| 428 |
my $err = "Invalid $type option: $arg"; |
| 429 |
my $handled = $self->{+COMMAND_CLASS} && $self->{+COMMAND_CLASS}->handle_invalid_option($type, $arg, $err); |
| 430 |
die "$err\n" unless $handled; |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
if ($config{die_at_non_opt}) { |
| 435 |
my $err = "Invalid $type option: $arg"; |
| 436 |
my $handled = $self->{+COMMAND_CLASS} && $self->{+COMMAND_CLASS}->handle_invalid_option($type, $arg, $err); |
| 437 |
die "$err\n" unless $handled; |
| 438 |
} |
| 439 |
|
| 440 |
push @keep_args => $arg; |
| 441 |
|
| 442 |
last if $config{stop_at_non_opt}; |
| 443 |
} |
| 444 |
|
| 445 |
unshift @$args => @keep_args; |
| 446 |
|
| 447 |
return @opts; |
| 448 |
} |
| 449 |
|
| 450 |
sub _build_lookup { |
| 451 |
my $self = shift; |
| 452 |
my ($opts) = @_; |
| 453 |
|
| 454 |
my $lookup = {long => {}, short => {}}; |
| 455 |
|
| 456 |
my %seen; |
| 457 |
for my $opt (@$opts) { |
| 458 |
next if $seen{$opt}++; |
| 459 |
|
| 460 |
for my $long ($opt->long_args) { |
| 461 |
$lookup->{long}->{$long} //= $opt; |
| 462 |
} |
| 463 |
|
| 464 |
my $short = $opt->short or next; |
| 465 |
$lookup->{short}->{$short} //= $opt; |
| 466 |
} |
| 467 |
|
| 468 |
return $lookup; |
| 469 |
} |
| 470 |
|
| 471 |
sub _post { |
| 472 |
my $self = shift; |
| 473 |
my ($weight, $applicable, $cb) = @_; |
| 474 |
|
| 475 |
$self->{+POST_LIST_SORTED} = 0; |
| 476 |
|
| 477 |
$weight //= 0; |
| 478 |
|
| 479 |
push @{$self->{+POST_LIST} //= []} => [$weight, $applicable, $cb]; |
| 480 |
} |
| 481 |
|
| 482 |
sub _option { |
| 483 |
my $self = shift; |
| 484 |
my ($trace, @spec) = @_; |
| 485 |
|
| 486 |
my %proto = $self->_parse_option_args(@spec); |
| 487 |
|
| 488 |
my $opt = App::Yath::Option->new( |
| 489 |
trace => $trace, |
| 490 |
$self->_parse_option_caller($trace->[0], \%proto), |
| 491 |
%proto, |
| 492 |
); |
| 493 |
|
| 494 |
$self->include_option($opt); |
| 495 |
} |
| 496 |
|
| 497 |
sub include_option { |
| 498 |
my $self = shift; |
| 499 |
my ($opt) = @_; |
| 500 |
|
| 501 |
my $trace = $opt->trace or confess "Options must have a trace!"; |
| 502 |
|
| 503 |
push @{$self->{+ALL}} => $opt; |
| 504 |
|
| 505 |
my $new = $self->_index_option($opt); |
| 506 |
$self->_list_option($opt) if $new; |
| 507 |
|
| 508 |
return $opt; |
| 509 |
} |
| 510 |
|
| 511 |
sub _parse_option_caller { |
| 512 |
my $self = shift; |
| 513 |
my ($caller, $proto) = @_; |
| 514 |
|
| 515 |
my ($from_plugin, $from_command, $from_prefix, $prefix, $is_top); |
| 516 |
|
| 517 |
$prefix = $proto->{prefix} if exists $proto->{prefix}; |
| 518 |
$prefix //= $caller->option_prefix() if $caller->can('option_prefix'); |
| 519 |
|
| 520 |
if ($caller->isa('App::Yath::Command')) { |
| 521 |
$from_command = $caller->name() unless $caller eq 'App::Yath::Command'; |
| 522 |
$is_top = 1; |
| 523 |
} |
| 524 |
elsif ($caller =~ m/App::Yath::Command::([^:]+)::.*Options(?:::.*)?$/) { |
| 525 |
$from_command = $1; |
| 526 |
$is_top = 1; |
| 527 |
} |
| 528 |
elsif ($caller eq 'App::Yath') { |
| 529 |
$is_top = 1; |
| 530 |
} |
| 531 |
elsif ($caller =~ m/^(App::Yath::Plugin::([^:]+))$/) { |
| 532 |
$from_plugin = $1; |
| 533 |
$from_prefix = $2; |
| 534 |
|
| 535 |
unless (defined $prefix) { |
| 536 |
$prefix = $from_prefix; |
| 537 |
$prefix =~ s/::.*$//g; |
| 538 |
} |
| 539 |
} |
| 540 |
|
| 541 |
$prefix = lc($prefix) if $prefix; |
| 542 |
|
| 543 |
croak "Could not find an option prefix and option is not top-level ($proto->{title})" |
| 544 |
unless $is_top || defined($prefix) || defined($proto->{prefix}); |
| 545 |
|
| 546 |
return ( |
| 547 |
$from_plugin ? (from_plugin => $from_plugin) : (), |
| 548 |
$from_command ? (from_command => $from_command) : (), |
| 549 |
($prefix || !$is_top) ? (prefix => $prefix) : (), |
| 550 |
); |
| 551 |
} |
| 552 |
|
| 553 |
sub _parse_option_args { |
| 554 |
my $self = shift; |
| 555 |
my @spec = @_; |
| 556 |
|
| 557 |
my %args; |
| 558 |
if (@spec == 1) { |
| 559 |
my ($title, $type) = $spec[0] =~ m/^([\w-]+)(?:=(.+))?$/ or croak "Invalid option specification: $spec[0]"; |
| 560 |
return (title => $title, type => $type); |
| 561 |
} |
| 562 |
elsif (@spec == 2) { |
| 563 |
my ($title, $type) = @spec; |
| 564 |
return (title => $title, type => $type); |
| 565 |
} |
| 566 |
|
| 567 |
my $title = shift @spec; |
| 568 |
return (title => $title, @spec); |
| 569 |
} |
| 570 |
|
| 571 |
sub _index_option { |
| 572 |
my $self = shift; |
| 573 |
my ($opt) = @_; |
| 574 |
|
| 575 |
my $index = $self->{+LOOKUP}; |
| 576 |
|
| 577 |
my $out = 0; |
| 578 |
|
| 579 |
for my $n ($opt->name, @{$opt->alt || []}) { |
| 580 |
if (my $existing = $index->{$n}) { |
| 581 |
next if "$existing" eq "$opt"; |
| 582 |
croak "Option '$n' was already defined (" . $existing->trace_string . ")"; |
| 583 |
} |
| 584 |
|
| 585 |
$out++; |
| 586 |
$index->{$n} = $opt; |
| 587 |
} |
| 588 |
|
| 589 |
if (my $short = $opt->short) { |
| 590 |
if (my $existing = $index->{$short}) { |
| 591 |
return $out if "$existing" eq "$opt"; |
| 592 |
croak "Option '$short' was already defined (" . $existing->trace_string . ")"; |
| 593 |
} |
| 594 |
|
| 595 |
$out++; |
| 596 |
$index->{$short} = $opt; |
| 597 |
} |
| 598 |
|
| 599 |
return $out; |
| 600 |
} |
| 601 |
|
| 602 |
sub _list_option { |
| 603 |
my $self = shift; |
| 604 |
my ($opt) = @_; |
| 605 |
|
| 606 |
return push @{$self->{+PRE_LIST}} => $opt |
| 607 |
if $opt->pre_command; |
| 608 |
|
| 609 |
push @{$self->{+CMD_LIST}} => $opt; |
| 610 |
} |
| 611 |
|
| 612 |
sub pre_docs { |
| 613 |
my $self = shift; |
| 614 |
|
| 615 |
return $self->_docs($self->_pre_command_options(), @_); |
| 616 |
} |
| 617 |
|
| 618 |
sub cmd_docs { |
| 619 |
my $self = shift; |
| 620 |
|
| 621 |
return unless $self->{+COMMAND_CLASS}; |
| 622 |
|
| 623 |
return $self->_docs([grep { !$_->pre_command } @{$self->_command_options()}], @_); |
| 624 |
} |
| 625 |
|
| 626 |
my %DOC_FORMATS = ( |
| 627 |
'cli' => [ |
| 628 |
'cli_docs', # Method to call on opt |
| 629 |
"\n", # how to join lines |
| 630 |
sub { "\n$_[1]" }, # how to render the category |
| 631 |
sub { $_[0] =~ s/^/ /mg; "$_[0]\n" }, # transform the value from the opt |
| 632 |
sub { }, # add this at the end |
| 633 |
], |
| 634 |
'pod' => [ |
| 635 |
'pod_docs', # Method to call on opt |
| 636 |
"\n\n", # how to join lines |
| 637 |
sub { ($_[0] ? ("=back") : (), "=head$_[2] $_[1]", "=over 4") }, # how to render the category |
| 638 |
sub { $_[0] }, # transform the value from the opt |
| 639 |
sub { $_[0] ? ("=back") : () }, # add this at the end |
| 640 |
], |
| 641 |
); |
| 642 |
|
| 643 |
sub _docs { |
| 644 |
my $self = shift; |
| 645 |
my ($opts, $format, @args) = @_; |
| 646 |
|
| 647 |
$format //= "UNDEFINED"; |
| 648 |
my $fset = $DOC_FORMATS{$format} or croak "Invalid documentation format '$format'"; |
| 649 |
my ($fmeth, $join, $fcat, $ftrans, $fend) = @$fset; |
| 650 |
|
| 651 |
return unless $opts; |
| 652 |
return unless @$opts; |
| 653 |
|
| 654 |
my @opts = sort _doc_sort_ops @$opts; |
| 655 |
|
| 656 |
my @out; |
| 657 |
|
| 658 |
my $cat; |
| 659 |
for my $opt (@opts) { |
| 660 |
if (!$cat || $opt->category ne $cat) { |
| 661 |
push @out => $fcat->($cat, $opt->category, @args); |
| 662 |
$cat = $opt->category; |
| 663 |
} |
| 664 |
|
| 665 |
my $help = $opt->$fmeth(); |
| 666 |
push @out => $ftrans->($help); |
| 667 |
} |
| 668 |
|
| 669 |
push @out => $fend->($cat); |
| 670 |
|
| 671 |
return join $join => @out; |
| 672 |
} |
| 673 |
|
| 674 |
sub _doc_sort_ops($$) { |
| 675 |
my ($a, $b) = @_; |
| 676 |
|
| 677 |
my $anc = $a->category eq 'NO CATEGORY - FIX ME'; |
| 678 |
my $bnc = $b->category eq 'NO CATEGORY - FIX ME'; |
| 679 |
|
| 680 |
if($anc xor $bnc) { |
| 681 |
return 1 if $anc; |
| 682 |
return -1; |
| 683 |
} |
| 684 |
|
| 685 |
my $ret = $a->category cmp $b->category; |
| 686 |
$ret ||= ($a->prefix || '') cmp ($b->prefix || ''); |
| 687 |
$ret ||= $a->field cmp $b->field; |
| 688 |
$ret ||= $a->name cmp $b->name; |
| 689 |
|
| 690 |
return $ret; |
| 691 |
} |
| 692 |
|
| 693 |
sub clear_env { |
| 694 |
my $self = shift; |
| 695 |
|
| 696 |
for my $opt (@{$self->{+ALL}}) { |
| 697 |
next unless $opt->clear_env_vars; |
| 698 |
my $env = $opt->env_vars or next; |
| 699 |
for my $var (@$env) { |
| 700 |
$var =~ s/^!//; |
| 701 |
delete $ENV{$var}; |
| 702 |
} |
| 703 |
} |
| 704 |
} |
| 705 |
|
| 706 |
1; |
| 707 |
|
| 708 |
__END__ |
| 709 |
|
| 710 |
=pod |
| 711 |
|
| 712 |
=encoding UTF-8 |
| 713 |
|
| 714 |
=head1 NAME |
| 715 |
|
| 716 |
App::Yath::Options - Tools for defining and tracking yath CLI options. |
| 717 |
|
| 718 |
=head1 DESCRIPTION |
| 719 |
|
| 720 |
This class represents a collection of options, and holds the logic for |
| 721 |
processing them. This package also exports sugar to help you define options. |
| 722 |
|
| 723 |
=head1 SYNOPSIS |
| 724 |
|
| 725 |
package My::Options; |
| 726 |
|
| 727 |
use App::Yath::Options; |
| 728 |
|
| 729 |
# This package now has a package instance of options, which can be obtained |
| 730 |
# via the options() method. |
| 731 |
my $options = __PACKAGE__->options; |
| 732 |
|
| 733 |
# We can include options from other packages |
| 734 |
include_options( |
| 735 |
'Package::With::Options::A', |
| 736 |
'Package::With::Options::B', |
| 737 |
..., |
| 738 |
); |
| 739 |
|
| 740 |
# Define an option group with some options |
| 741 |
option_group { %common_fields } => sub { |
| 742 |
|
| 743 |
# Define an option |
| 744 |
option foo => ( |
| 745 |
type => 's', |
| 746 |
default => "FOOOOOOO", |
| 747 |
category => 'foo', |
| 748 |
description => "This is foo" |
| 749 |
long_examples => [' value'], |
| 750 |
... |
| 751 |
); |
| 752 |
|
| 753 |
option bar => ( ... ); |
| 754 |
... |
| 755 |
}; |
| 756 |
|
| 757 |
# Action to call right after options are parsed. |
| 758 |
post sub { |
| 759 |
my %params = @_; |
| 760 |
|
| 761 |
... |
| 762 |
}; |
| 763 |
|
| 764 |
=head1 EXPORTS |
| 765 |
|
| 766 |
=over 4 |
| 767 |
|
| 768 |
=item $opts = options() |
| 769 |
|
| 770 |
=item $opts = $class->options() |
| 771 |
|
| 772 |
This returns the options instance associated with your package. |
| 773 |
|
| 774 |
=item include_options(@CLASSES) |
| 775 |
|
| 776 |
This lets you include options defined in other packages. |
| 777 |
|
| 778 |
=item option_group \%COMMON_FIELDS => sub { ... } |
| 779 |
|
| 780 |
An option group is simply a block where all calls to C<option()> will have |
| 781 |
common fields added automatically, this makes it easier to define multiple |
| 782 |
options that share common fields. Common fields can be overridden inside the |
| 783 |
option definition. |
| 784 |
|
| 785 |
These are both equivalent: |
| 786 |
|
| 787 |
# Using option group |
| 788 |
option_group { category => 'foo', prefix => 'foo' } => sub { |
| 789 |
option a => (type => 'b'); |
| 790 |
option b => (type => 's'); |
| 791 |
}; |
| 792 |
|
| 793 |
# Not using option group |
| 794 |
option a => (type => 'b', category => 'foo', prefix => 'foo'); |
| 795 |
option b => (type => 's', category => 'foo', prefix => 'foo'); |
| 796 |
|
| 797 |
=item option TITLE => %FIELDS |
| 798 |
|
| 799 |
Define an option. The first argument is the C<title> attribute for the new |
| 800 |
option, all other arguments should be attribute/value pairs used to construct |
| 801 |
the option. See L<App::Yath::Option> for the documentation of attributes. |
| 802 |
|
| 803 |
=item post sub { ... } |
| 804 |
|
| 805 |
=item post $weight => sub { ... } |
| 806 |
|
| 807 |
C<post> callbacks are run after all command line arguments have been processed. |
| 808 |
This is a place to verify the result of several options combined, sanity check, |
| 809 |
or even add short-circuit behavior. This is how the C<--help> and |
| 810 |
C<--show-opts> options are implemented. |
| 811 |
|
| 812 |
If no C<$weight> is specified then C<0> is used. C<post> callbacks or sorted |
| 813 |
based on weight with higher values being run later. |
| 814 |
|
| 815 |
=back |
| 816 |
|
| 817 |
=head1 OPTIONS INSTANCES |
| 818 |
|
| 819 |
In general you should not be using the options instance directly. Options |
| 820 |
instances are mostly an implementation detail that should be treated as a black |
| 821 |
box. There are however a few valid reasons to interact with them directly. In |
| 822 |
those cases there are a few public attributes/methods you can work with. This |
| 823 |
section documents the public interface. |
| 824 |
|
| 825 |
=head2 ATTRIBUTES |
| 826 |
|
| 827 |
This section only lists attributes that may be useful to people working with |
| 828 |
options instances. There are a lot of internal (to yath) attributes that are |
| 829 |
implementation details that are not listed here. Attributes not listed here are |
| 830 |
not intended for external use and may change at any time. |
| 831 |
|
| 832 |
=over 4 |
| 833 |
|
| 834 |
=item $arrayref = $options->all |
| 835 |
|
| 836 |
Arrayref containing all the L<App::Yath::Option> instances in the options |
| 837 |
instance. |
| 838 |
|
| 839 |
=item $settings = $options->settings |
| 840 |
|
| 841 |
Get the L<Test2::Harness::Settings> instance. |
| 842 |
|
| 843 |
=item $arrayref = $options->args |
| 844 |
|
| 845 |
Get the reference to the list of command line arguments. This list is modified |
| 846 |
as arguments are processed, there are no guarantees about what is in here at |
| 847 |
any given stage of argument processing. |
| 848 |
|
| 849 |
=item $class_name = $options->command_class |
| 850 |
|
| 851 |
If yath has determined what command is being executed this will be populated |
| 852 |
with that command class. This will be undefined if the class has not been |
| 853 |
determined yet. |
| 854 |
|
| 855 |
=item $arrayref = $options->used_plugins |
| 856 |
|
| 857 |
This is a list of all plugins who's options have been used. Plugins may appear |
| 858 |
more than once. |
| 859 |
|
| 860 |
=item $hashref = $options->included |
| 861 |
|
| 862 |
A hashref where every key is a package who's options have been included into |
| 863 |
this options instance. The values are an implementation detail, do not rely on |
| 864 |
them. |
| 865 |
|
| 866 |
=back |
| 867 |
|
| 868 |
=head2 METHODS |
| 869 |
|
| 870 |
This section only lists methods that may be useful to people working with |
| 871 |
options instances. There are a lot of internal (to yath) methods that are |
| 872 |
implementation details that are not listed here. Methods not listed here are |
| 873 |
not intended for external use and may change at any time. |
| 874 |
|
| 875 |
=over 4 |
| 876 |
|
| 877 |
=item $opt = $options->option(%OPTION_ATTRIBUTES) |
| 878 |
|
| 879 |
This will create a new option with the provided attributes and add it to the |
| 880 |
options instance. A C<trace> attribute will be automatically set for you. |
| 881 |
|
| 882 |
=item $options->include($options_instance) |
| 883 |
|
| 884 |
This method lets you directly include options from a second instance into the |
| 885 |
first. |
| 886 |
|
| 887 |
=item $options->include_from(@CLASSES) |
| 888 |
|
| 889 |
This lets you include options from multiple classes that have options defined. |
| 890 |
|
| 891 |
=item $options->include_option($opt) |
| 892 |
|
| 893 |
This lets you include a single already defined option instance. |
| 894 |
|
| 895 |
=item $options->pre_docs($format, @args) |
| 896 |
|
| 897 |
Get documentation for pre-command options. $format may be 'cli' or 'pod'. |
| 898 |
|
| 899 |
=item $options->cmd_docs($format, @args) |
| 900 |
|
| 901 |
Get documentation for command options. $format may be 'cli' or 'pod'. |
| 902 |
|
| 903 |
=back |
| 904 |
|
| 905 |
=head1 SOURCE |
| 906 |
|
| 907 |
The source code repository for Test2-Harness can be found at |
| 908 |
F<http://github.com/Test-More/Test2-Harness/>. |
| 909 |
|
| 910 |
=head1 MAINTAINERS |
| 911 |
|
| 912 |
=over 4 |
| 913 |
|
| 914 |
=item Chad Granum E<lt>[email protected]<gt> |
| 915 |
|
| 916 |
=back |
| 917 |
|
| 918 |
=head1 AUTHORS |
| 919 |
|
| 920 |
=over 4 |
| 921 |
|
| 922 |
=item Chad Granum E<lt>[email protected]<gt> |
| 923 |
|
| 924 |
=back |
| 925 |
|
| 926 |
=head1 COPYRIGHT |
| 927 |
|
| 928 |
Copyright 2020 Chad Granum E<lt>[email protected]<gt>. |
| 929 |
|
| 930 |
This program is free software; you can redistribute it and/or |
| 931 |
modify it under the same terms as Perl itself. |
| 932 |
|
| 933 |
See F<http://dev.perl.org/licenses/> |
| 934 |
|
| 935 |
=cut |