| 1 | #============================================================================= | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||||||||||||||||
| 3 | # Module: Term::CLI::Command | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||||||||||||||||
| 5 | # Description: Class for (sub-)commands in Term::CLI | ||||||||||||||||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||||||||||||||||
| 7 | # Author: Steven Bakker (SBAKKER), <sbakker@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||
| 8 | # Created: 30/01/18 | ||||||||||||||||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||||||||||||||||
| 10 | # Copyright (c) 2018-2022 Steven Bakker | ||||||||||||||||||||||||||||||||||||||||||
| 11 | # | ||||||||||||||||||||||||||||||||||||||||||
| 12 | # This module is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||||||||||||||||
| 13 | # it under the same terms as Perl itself. See "perldoc perlartistic." | ||||||||||||||||||||||||||||||||||||||||||
| 14 | # | ||||||||||||||||||||||||||||||||||||||||||
| 15 | # This software is distributed in the hope that it will be useful, | ||||||||||||||||||||||||||||||||||||||||||
| 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||||||||||||||||||||||||||||||
| 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||||||||||||||||||||||||||||||||||||||||||
| 18 | # | ||||||||||||||||||||||||||||||||||||||||||
| 19 | #============================================================================= | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | package Term::CLI::Command 0.061000; | ||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 7 | ▶ | use 5.014; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 7 | ▶ | use List::Util 1.23 qw( first min ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 7 | use Types::Standard 1.000005 qw( | |||||||||||||||||||||||||||||||||||||||||
| 28 | ArrayRef | ||||||||||||||||||||||||||||||||||||||||||
| 29 | CodeRef | ||||||||||||||||||||||||||||||||||||||||||
| 30 | InstanceOf | ||||||||||||||||||||||||||||||||||||||||||
| 31 | Maybe | ||||||||||||||||||||||||||||||||||||||||||
| 32 | Str | ||||||||||||||||||||||||||||||||||||||||||
| 33 | 7 | ▶ | ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 7 | ▶ | use Term::CLI::L10N qw( loc ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 7 | ▶ | use Term::CLI::Util qw( is_prefix_str get_options_from_array ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 7 | ▶ | use Moo 1.000001; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 7 | ▶ | use namespace::clean 0.25; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 7 | extends 'Term::CLI::Element'; | |||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 7 | has options => ( | |||||||||||||||||||||||||||||||||||||||||
| 44 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 45 | isa => Maybe [ ArrayRef [Str] ], | ||||||||||||||||||||||||||||||||||||||||||
| 46 | predicate => 1 | ||||||||||||||||||||||||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | 7 | with qw( | |||||||||||||||||||||||||||||||||||||||||
| 50 | Term::CLI::Role::CommandSet | ||||||||||||||||||||||||||||||||||||||||||
| 51 | Term::CLI::Role::ArgumentSet | ||||||||||||||||||||||||||||||||||||||||||
| 52 | Term::CLI::Role::HelpText | ||||||||||||||||||||||||||||||||||||||||||
| 53 | Term::CLI::Role::State | ||||||||||||||||||||||||||||||||||||||||||
| 54 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | sub option_names { | ||||||||||||||||||||||||||||||||||||||||||
| 57 | 10 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
option_names: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 10 | ▶ | my $opt_specs = $self->options or return (); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 9 | my @names; | |||||||||||||||||||||||||||||||||||||||||
| 60 | 9 | for my $spec ( @{$opt_specs} ) { | |||||||||||||||||||||||||||||||||||||||||
| 61 | 24 | my ($name_spec, $opts) = $spec =~ m{^ ([^!+=:]+) (.*) $}xms; | |||||||||||||||||||||||||||||||||||||||||
| 62 | 24 | my $negatable = index($opts, '!') >= 0; | |||||||||||||||||||||||||||||||||||||||||
| 63 | 24 | for my $opt_name ( split( qr{\|}x, $name_spec) ) { | |||||||||||||||||||||||||||||||||||||||||
| 64 | 48 | ▶ | if (length $opt_name > 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 24 | push @names, "--$opt_name"; | |||||||||||||||||||||||||||||||||||||||||
| 66 | 24 | ▶ | push @names, "--no-$opt_name" if $negatable; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 24 | push @names, "-$opt_name"; | |||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | 9 | return @names; | |||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | sub complete { | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 51 | ▶ | my ( $self, $text, $state ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
complete: called CC=19 SCAR=29.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | 51 | ▶ | $text //= q{}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$text //= ""
MC/DC100%1/1
$text //= ""
$text
Truth table100%2/2
A
A= $text
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 51 | ▶ | $state //= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$state //= {}
MC/DC100%1/1
$state //= {}
$state
Truth table100%2/2
A
A= $state
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 51 | ▶ | my $processed = $state->{processed} //= []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$state->{"processed"} //= []
MC/DC100%1/1
$state->{"processed"} //= []
$state->{"processed"}
Truth table100%2/2
A
A= $state->{"processed"}
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 51 | ▶ | my $unprocessed = $state->{unprocessed} //= []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$state->{"unprocessed"} //= []
MC/DC100%1/1
$state->{"unprocessed"} //= []
$state->{"unprocessed"}
Truth table100%2/2
A
A= $state->{"unprocessed"}
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 51 | ▶ | my $parsed_options = $state->{options} //= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$state->{"options"} //= {}
MC/DC100%1/1
$state->{"options"} //= {}
$state->{"options"}
Truth table100%2/2
A
A= $state->{"options"}
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 51 | ▶ | if ( $self->has_options ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | 24 | my %opt_result = get_options_from_array( | |||||||||||||||||||||||||||||||||||||||||
| 89 | args => $unprocessed, | ||||||||||||||||||||||||||||||||||||||||||
| 90 | spec => $self->options, | ||||||||||||||||||||||||||||||||||||||||||
| 91 | result => $parsed_options, | ||||||||||||||||||||||||||||||||||||||||||
| 92 | pass_through => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 93 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 24 | my $double_dash = $opt_result{double_dash}; | |||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | # Check if we have to complete a command-line option. | ||||||||||||||||||||||||||||||||||||||||||
| 98 | 24 | ▶ | if ( !$double_dash && @{$unprocessed} == 0 && $text =~ /^-/x ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
not $double_dash and @{$unprocessed;} == 0 and $text =~ /^-/ux
not $double_dash and @{$unprocessed;} == 0
MC/DC100%3/3
not $double_dash and @{$unprocessed;} == 0 and $text =~ /^-/ux
not $double_dash
@{$unprocessed;} == 0
$text =~ /^-/ux
Truth table100%4/4
not(A) and B and C
A= not $double_dashB= @{$unprocessed;} == 0C= $text =~ /^-/ux
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 5 | return grep { is_prefix_str( $text, $_ ) } $self->option_names; | |||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | # If the command has arguments, try to skip over them. | ||||||||||||||||||||||||||||||||||||||||||
| 104 | 46 | ▶ | if ( $self->has_arguments ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 26 | my @args = $self->arguments; | |||||||||||||||||||||||||||||||||||||||||
| 106 | 26 | my $arg_repeat = 0; | |||||||||||||||||||||||||||||||||||||||||
| 107 | 26 | ▶ | while ( @{$unprocessed} && @args ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@{$unprocessed;} and @args
MC/DC100%2/2
@{$unprocessed;} and @args
@{$unprocessed;}
@args
Truth table100%3/3
A and B
A= @{$unprocessed;}B= @args
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 43 | push @{$processed}, { | |||||||||||||||||||||||||||||||||||||||||
| 109 | element => $args[0], | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 43 | value => shift @{$unprocessed}, | |||||||||||||||||||||||||||||||||||||||||
| 111 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 43 | $arg_repeat++; | |||||||||||||||||||||||||||||||||||||||||
| 113 | 43 | ▶ | if ( $args[0]->max_occur > 0 and $arg_repeat >= $args[0]->max_occur ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$args[0]->max_occur > 0 and $arg_repeat >= $args[0]->max_occur
MC/DC100%2/2
$args[0]->max_occur > 0 and $arg_repeat >= $args[0]->max_occur
$args[0]->max_occur > 0
$arg_repeat >= $args[0]->max_occur
Truth table100%3/3
A and B
A= $args[0]->max_occur > 0B= $arg_repeat >= $args[0]->max_occur
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 22 | shift @args; | |||||||||||||||||||||||||||||||||||||||||
| 115 | 22 | $arg_repeat = 0; | |||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | 26 | ▶ | if (@args) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 22 | return $args[0]->complete( $text, $state ); | |||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | 24 | ▶ | if ( $self->has_commands ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 22 | ▶ | if ( @{$unprocessed} == 0 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 13 | return grep { is_prefix_str( $text, $_ ) } $self->command_names; | |||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||
| 128 | 9 | ▶ | if ( my $cmd = $self->find_command( $unprocessed->[0] ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | 8 | push @{$processed}, { | |||||||||||||||||||||||||||||||||||||||||
| 130 | element => $cmd, | ||||||||||||||||||||||||||||||||||||||||||
| 131 | value => $cmd->name, | ||||||||||||||||||||||||||||||||||||||||||
| 132 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 133 | 8 | shift @{$unprocessed}; | |||||||||||||||||||||||||||||||||||||||||
| 134 | 8 | return $cmd->complete( $text, $state ); | |||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | 3 | return (); | |||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | sub execute_command { | ||||||||||||||||||||||||||||||||||||||||||
| 142 | 49 | ▶ | my ( $self, %args ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
execute_command: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | 49 | $args{status} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 145 | 49 | $args{error} = q{}; | |||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | # Dereference and copy arguments/unparsed/options to prevent | ||||||||||||||||||||||||||||||||||||||||||
| 148 | # unwanted side-effects. | ||||||||||||||||||||||||||||||||||||||||||
| 149 | 49 | $args{arguments} = [ @{ $args{arguments} } ]; | |||||||||||||||||||||||||||||||||||||||||
| 150 | 49 | $args{unprocessed} = [ @{ $args{unprocessed} } ]; | |||||||||||||||||||||||||||||||||||||||||
| 151 | 49 | $args{unparsed} = $args{unprocessed}; | |||||||||||||||||||||||||||||||||||||||||
| 152 | 49 | $args{processed} = [ @{ $args{processed} } ]; | |||||||||||||||||||||||||||||||||||||||||
| 153 | 49 | $args{options} = { %{ $args{options} } }; | |||||||||||||||||||||||||||||||||||||||||
| 154 | 49 | $args{command_path} = [ @{ $args{command_path} } ]; | |||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | 49 | push @{ $args{command_path} }, $self; | |||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | 49 | ▶ | if ( $self->has_options ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | my %opt_result = get_options_from_array( | ||||||||||||||||||||||||||||||||||||||||||
| 160 | args => $args{unprocessed}, | ||||||||||||||||||||||||||||||||||||||||||
| 161 | spec => $self->options, | ||||||||||||||||||||||||||||||||||||||||||
| 162 | result => $args{options}, | ||||||||||||||||||||||||||||||||||||||||||
| 163 | 17 | pass_through => 0, | |||||||||||||||||||||||||||||||||||||||||
| 164 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | 17 | ▶ | if ( !$opt_result{success} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | 2 | $args{status} = -1; | |||||||||||||||||||||||||||||||||||||||||
| 168 | 2 | $args{error} = $opt_result{error_msg}; | |||||||||||||||||||||||||||||||||||||||||
| 169 | 2 | return $self->try_callback(%args); | |||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | 47 | ▶ | if ( $self->has_arguments || !$self->has_commands ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->has_arguments or not $self->has_commands
MC/DC100%2/2
$self->has_arguments or not $self->has_commands
$self->has_arguments
not $self->has_commands
Truth table100%3/3
A or not(B)
A= $self->has_argumentsB= not $self->has_commands
| |||||||||||||||||||||||||||||||||||||||||||
| 174 | 42 | %args = $self->_check_arguments(%args); | |||||||||||||||||||||||||||||||||||||||||
| 175 | 42 | ▶ | return $self->try_callback(%args) if $args{status} < 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | 31 | ▶ | return $self->try_callback(%args) if !$self->has_commands; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | 8 | ▶ | if ( $self->require_sub_command || @{ $args{unprocessed} } > 0 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->require_sub_command or @{$args{"unprocessed"};} > 0
MC/DC0%0/2
$self->require_sub_command or @{$args{"unprocessed"};} > 0
$self->require_sub_command
@{$args{"unprocessed"};} > 0
Truth table33%1/3
A or B
A= $self->require_sub_commandB= @{$args{"unprocessed"};} > 0
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | 8 | %args = $self->_execute_sub_command(%args); | |||||||||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||||||||
| 183 | 8 | return $self->try_callback(%args); | |||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | sub _too_few_args_error { | ||||||||||||||||||||||||||||||||||||||||||
| 187 | 7 | ▶ | my ( $self, $arg_spec ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_too_few_args_error: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | 7 | ▶ | if ( $arg_spec->max_occur == $arg_spec->min_occur ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 190 | 2 | ▶ | if ( $arg_spec->min_occur == 1 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 191 | 1 | return loc( "missing '[_1]' argument", $arg_spec->name ); | |||||||||||||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||||||||||||
| 193 | 1 | return loc( "need [_1] '[_2]' [numerate,_1,argument]", | |||||||||||||||||||||||||||||||||||||||||
| 194 | $arg_spec->min_occur, $arg_spec->name, ); | ||||||||||||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||||||||||||
| 196 | 5 | ▶ | if ( $arg_spec->max_occur - $arg_spec->min_occur == 1 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 197 | 1 | return loc( "need [_1] or [_2] '[_3]' arguments", | |||||||||||||||||||||||||||||||||||||||||
| 198 | $arg_spec->min_occur, $arg_spec->max_occur, $arg_spec->name, ); | ||||||||||||||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||||||||||||
| 200 | 4 | ▶ | if ( $arg_spec->max_occur > 1 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 201 | 1 | return loc( "need between [_1] and [_2] '[_3]' arguments", | |||||||||||||||||||||||||||||||||||||||||
| 202 | $arg_spec->min_occur, $arg_spec->max_occur, $arg_spec->name, ); | ||||||||||||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||||||||||
| 204 | 3 | return loc( "need at least [_1] '[_2]' [numerate,_1,argument]", | |||||||||||||||||||||||||||||||||||||||||
| 205 | $arg_spec->min_occur, $arg_spec->name, ); | ||||||||||||||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||
| 208 | sub _check_arguments { | ||||||||||||||||||||||||||||||||||||||||||
| 209 | 42 | ▶ | my ( $self, %args ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_check_arguments: called CC=10 SCAR=23.0
| |||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||
| 211 | 42 | my $unprocessed = $args{unprocessed}; | |||||||||||||||||||||||||||||||||||||||||
| 212 | 42 | my $processed = $args{processed}; | |||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||
| 214 | 42 | my @arg_spec = $self->arguments; | |||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||
| 216 | 42 | ▶ | if ( @arg_spec == 0 and @{$unprocessed} > 0 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@arg_spec == 0 and @{$unprocessed;} > 0
MC/DC100%2/2
@arg_spec == 0 and @{$unprocessed;} > 0
@arg_spec == 0
@{$unprocessed;} > 0
Truth table100%3/3
A and B
A= @arg_spec == 0B= @{$unprocessed;} > 0
| |||||||||||||||||||||||||||||||||||||||||||
| 217 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 218 | 1 | %args, | |||||||||||||||||||||||||||||||||||||||||
| 219 | status => -1, | ||||||||||||||||||||||||||||||||||||||||||
| 220 | error => loc('no arguments allowed'), | ||||||||||||||||||||||||||||||||||||||||||
| 221 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||
| 224 | 41 | my $argno = 0; | |||||||||||||||||||||||||||||||||||||||||
| 225 | 41 | for my $arg_spec (@arg_spec) { | |||||||||||||||||||||||||||||||||||||||||
| 226 | 54 | ▶ | if ( @{$unprocessed} < $arg_spec->min_occur ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 227 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 228 | 7 | %args, | |||||||||||||||||||||||||||||||||||||||||
| 229 | status => -1, | ||||||||||||||||||||||||||||||||||||||||||
| 230 | error => $self->_too_few_args_error($arg_spec), | ||||||||||||||||||||||||||||||||||||||||||
| 231 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | my $args_to_check = | ||||||||||||||||||||||||||||||||||||||||||
| 235 | $arg_spec->max_occur > 0 | ||||||||||||||||||||||||||||||||||||||||||
| 236 | 29 | ? min( $arg_spec->max_occur, scalar @{$unprocessed} ) | |||||||||||||||||||||||||||||||||||||||||
| 237 | 47 | ▶ | : scalar @{$unprocessed}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | 47 | for my $i ( 1 .. $args_to_check ) { | |||||||||||||||||||||||||||||||||||||||||
| 240 | 63 | my $arg = $unprocessed->[0]; | |||||||||||||||||||||||||||||||||||||||||
| 241 | 63 | $argno++; | |||||||||||||||||||||||||||||||||||||||||
| 242 | 63 | my $arg_value = $arg_spec->validate($arg, \%args); | |||||||||||||||||||||||||||||||||||||||||
| 243 | 63 | ▶ | if ( !defined $arg_value ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 244 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 245 | 4 | %args, | |||||||||||||||||||||||||||||||||||||||||
| 246 | status => -1, | ||||||||||||||||||||||||||||||||||||||||||
| 247 | error => qq{arg#$argno (} . $arg_spec->name . qq{), "$arg": } | ||||||||||||||||||||||||||||||||||||||||||
| 248 | . $arg_spec->error | ||||||||||||||||||||||||||||||||||||||||||
| 249 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||||||||||||
| 251 | 59 | push @{ $args{arguments} }, $arg_value; | |||||||||||||||||||||||||||||||||||||||||
| 252 | 59 | push @{ $processed }, { element => $arg_spec, value => $arg_value }; | |||||||||||||||||||||||||||||||||||||||||
| 253 | 59 | shift @{ $unprocessed }; | |||||||||||||||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||
| 257 | # At this point, we have processed all our arg_spec. The only way there | ||||||||||||||||||||||||||||||||||||||||||
| 258 | # are any elements left in @arguments is for the last arg_spec to have | ||||||||||||||||||||||||||||||||||||||||||
| 259 | # a max_occur that is exceeded. If the command has no sub-commands that | ||||||||||||||||||||||||||||||||||||||||||
| 260 | # is surely an error. If it does have sub-commands, we'll leave it to | ||||||||||||||||||||||||||||||||||||||||||
| 261 | # be parsed further. | ||||||||||||||||||||||||||||||||||||||||||
| 262 | 30 | ▶ | if ( @{$unprocessed} > 0 && !$self->has_commands ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@{$unprocessed;} > 0 and not $self->has_commands
MC/DC100%2/2
@{$unprocessed;} > 0 and not $self->has_commands
@{$unprocessed;} > 0
not $self->has_commands
Truth table100%3/3
A and not(B)
A= @{$unprocessed;} > 0B= not $self->has_commands
| |||||||||||||||||||||||||||||||||||||||||||
| 263 | 4 | my $last_spec = $arg_spec[-1]; | |||||||||||||||||||||||||||||||||||||||||
| 264 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 265 | 4 | %args, | |||||||||||||||||||||||||||||||||||||||||
| 266 | status => -1, | ||||||||||||||||||||||||||||||||||||||||||
| 267 | error => loc( | ||||||||||||||||||||||||||||||||||||||||||
| 268 | "too many '[_1]' arguments (max. [_2])", $last_spec->name, | ||||||||||||||||||||||||||||||||||||||||||
| 269 | $last_spec->max_occur, | ||||||||||||||||||||||||||||||||||||||||||
| 270 | ), | ||||||||||||||||||||||||||||||||||||||||||
| 271 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||||||||||||
| 273 | 26 | return %args; | |||||||||||||||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||
| 276 | sub _execute_sub_command { | ||||||||||||||||||||||||||||||||||||||||||
| 277 | 8 | ▶ | my ( $self, %args ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_execute_sub_command: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||
| 279 | 8 | my $unprocessed = $args{unprocessed}; | |||||||||||||||||||||||||||||||||||||||||
| 280 | 8 | my $processed = $args{processed}; | |||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||
| 282 | 8 | ▶ | if ( @{$unprocessed} == 0 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 283 | 2 | ▶ | if ( scalar $self->commands == 1 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 284 | 1 | my ($cmd) = $self->commands; | |||||||||||||||||||||||||||||||||||||||||
| 285 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 286 | 1 | %args, | |||||||||||||||||||||||||||||||||||||||||
| 287 | status => -1, | ||||||||||||||||||||||||||||||||||||||||||
| 288 | error => loc( "incomplete command: missing '[_1]'", $cmd->name ) | ||||||||||||||||||||||||||||||||||||||||||
| 289 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||||||||||||||||
| 291 | 1 | return ( %args, status => -1, error => loc("missing sub-command") ); | |||||||||||||||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||
| 294 | 6 | my $cmd_name = $unprocessed->[0]; | |||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||
| 296 | 6 | my $cmd = $self->find_command($cmd_name); | |||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||
| 298 | 6 | ▶ | if ( !$cmd ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 299 | 2 | ▶ | if ( scalar $self->commands == 1 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 300 | 1 | ($cmd) = $self->commands; | |||||||||||||||||||||||||||||||||||||||||
| 301 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 302 | 1 | %args, | |||||||||||||||||||||||||||||||||||||||||
| 303 | status => -1, | ||||||||||||||||||||||||||||||||||||||||||
| 304 | error => loc( | ||||||||||||||||||||||||||||||||||||||||||
| 305 | "expected '[_1]' instead of '[_2]'", $cmd->name, | ||||||||||||||||||||||||||||||||||||||||||
| 306 | $cmd_name | ||||||||||||||||||||||||||||||||||||||||||
| 307 | ), | ||||||||||||||||||||||||||||||||||||||||||
| 308 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||||||||||||||||
| 310 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 311 | 1 | %args, | |||||||||||||||||||||||||||||||||||||||||
| 312 | status => -1, | ||||||||||||||||||||||||||||||||||||||||||
| 313 | error => loc( "unknown sub-command '[_1]'", $cmd_name ) | ||||||||||||||||||||||||||||||||||||||||||
| 314 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||
| 317 | 4 | shift @{ $unprocessed }; | |||||||||||||||||||||||||||||||||||||||||
| 318 | 4 | push @{ $processed }, { element => $cmd, value => $cmd->name }; | |||||||||||||||||||||||||||||||||||||||||
| 319 | 4 | return $cmd->execute_command(%args); | |||||||||||||||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||
| 322 | 7 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||
| 324 | __END__ | ||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||
| 326 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||
| 328 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||
| 330 | Term::CLI::Command - Class for (sub-)commands in Term::CLI | ||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||
| 332 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||
| 334 | version 0.061000 | ||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||
| 336 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||
| 338 | use Term::CLI::Command; | ||||||||||||||||||||||||||||||||||||||||||
| 339 | use Term::CLI::Argument::Filename; | ||||||||||||||||||||||||||||||||||||||||||
| 340 | use Data::Dumper; | ||||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||||
| 342 | my $copy_cmd = Term::CLI::Command->new( | ||||||||||||||||||||||||||||||||||||||||||
| 343 | name => 'copy', | ||||||||||||||||||||||||||||||||||||||||||
| 344 | options => [ 'verbose!' ], | ||||||||||||||||||||||||||||||||||||||||||
| 345 | arguments => [ | ||||||||||||||||||||||||||||||||||||||||||
| 346 | Term::CLI::Argument::Filename->new(name => 'src'), | ||||||||||||||||||||||||||||||||||||||||||
| 347 | Term::CLI::Argument::Filename->new(name => 'dst'), | ||||||||||||||||||||||||||||||||||||||||||
| 348 | ], | ||||||||||||||||||||||||||||||||||||||||||
| 349 | callback => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 350 | my ($self, %args) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 351 | print Data::Dumper->Dump([\%args], ['args']); | ||||||||||||||||||||||||||||||||||||||||||
| 352 | return (%args, status => 0); | ||||||||||||||||||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||||||||||||||||
| 354 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||
| 356 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||||||
| 358 | Class for command elements in L<Term::CLI|Term::CLI>(3p). | ||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||
| 360 | =head1 CLASS STRUCTURE | ||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||
| 362 | =head2 Inherits from: | ||||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||
| 364 | L<Term::CLI::Element|Term::CLI::Element>(3p). | ||||||||||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||||||||||
| 366 | =head2 Consumes: | ||||||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||||||
| 368 | L<Term::CLI::Role::ArgumentSet|Term::CLI::Role::ArgumentSet>(3p), | ||||||||||||||||||||||||||||||||||||||||||
| 369 | L<Term::CLI::Role::CommandSet|Term::CLI::Role::CommandSet>(3p), | ||||||||||||||||||||||||||||||||||||||||||
| 370 | L<Term::CLI::Role::HelpText|Term::CLI::Role::HelpText>(3p), | ||||||||||||||||||||||||||||||||||||||||||
| 371 | L<Term::CLI::Role::State|Term::CLI::Role::State>(3p). | ||||||||||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||||||||||
| 373 | =head1 CONSTRUCTORS | ||||||||||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||||||||
| 375 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||||||||
| 377 | =item B<new> ( B<name> =E<gt> I<VARNAME> ... ) | ||||||||||||||||||||||||||||||||||||||||||
| 378 | X<new> | ||||||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||||||
| 380 | Create a new C<Term::CLI::Command> object and return a reference | ||||||||||||||||||||||||||||||||||||||||||
| 381 | to it. | ||||||||||||||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||||||||||||||
| 383 | The B<name> attribute is required. | ||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||
| 385 | Other attributes are: | ||||||||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||||||||
| 387 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||||||||
| 389 | =item B<arguments> =E<gt> I<ArrayRef> | ||||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||||
| 391 | Reference to an array containing | ||||||||||||||||||||||||||||||||||||||||||
| 392 | L<Term::CLI::Argument|Term::CLI::Argument>(3p) object | ||||||||||||||||||||||||||||||||||||||||||
| 393 | instances that describe the parameters that the command takes, | ||||||||||||||||||||||||||||||||||||||||||
| 394 | or C<undef>. | ||||||||||||||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||||||||||||||
| 396 | See also | ||||||||||||||||||||||||||||||||||||||||||
| 397 | L<Term::CLI::Role::ArgumentSet|Term::CLI::Role::ArgumentSet/ATTRIBUTES>. | ||||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||||
| 399 | =item B<callback> =E<gt> I<CodeRef> | ||||||||||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||||||||||
| 401 | Reference to a subroutine that should be called when the command | ||||||||||||||||||||||||||||||||||||||||||
| 402 | is executed, or C<undef>. | ||||||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||||||
| 404 | =item B<commands> =E<gt> I<ArrayRef> | ||||||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||||||
| 406 | Reference to an array containing C<Term::CLI::Command> object | ||||||||||||||||||||||||||||||||||||||||||
| 407 | instances that describe the sub-commands that the command takes, | ||||||||||||||||||||||||||||||||||||||||||
| 408 | or C<undef>. | ||||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||
| 410 | See also | ||||||||||||||||||||||||||||||||||||||||||
| 411 | L<Term::CLI::Role::CommandSet|Term::CLI::Role::CommandSet/ATTRIBUTES>. | ||||||||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||||||||
| 413 | =item B<require_sub_command> =E<gt> I<Bool> | ||||||||||||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||||||||||||
| 415 | If the command has sub-commands, it is normally required that the input | ||||||||||||||||||||||||||||||||||||||||||
| 416 | contains one of the sub-commands after this command. However, it may be | ||||||||||||||||||||||||||||||||||||||||||
| 417 | desirable to allow the command to appear "naked", i.e. without a sub-command. | ||||||||||||||||||||||||||||||||||||||||||
| 418 | For such cases, set the C<require_sub_command> to a false value. | ||||||||||||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||||||||||||
| 420 | See also | ||||||||||||||||||||||||||||||||||||||||||
| 421 | L<Term::CLI::Role::CommandSet|Term::CLI::Role::CommandSet/ATTRIBUTES>. | ||||||||||||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||||||||||||
| 423 | =item B<options> =E<gt> I<ArrayRef> | ||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||
| 425 | Reference to an array containing command options in | ||||||||||||||||||||||||||||||||||||||||||
| 426 | L<Getopt::Long|Getopt::Long>(3p) style, or C<undef>. | ||||||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||||||
| 428 | =item B<description> =E<gt> I<Str> | ||||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||
| 430 | Extended description of the command. | ||||||||||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||||||||||
| 432 | See also L<Term::CLI::Role::HelpText|Term::CLI::Role::HelpText/ATTRIBUTES>. | ||||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||
| 434 | =item B<summary> =E<gt> I<Str> | ||||||||||||||||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||||||||||||||||
| 436 | Short description of the command. | ||||||||||||||||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||||||||||||||||
| 438 | See also L<Term::CLI::Role::HelpText|Term::CLI::Role::HelpText/ATTRIBUTES>. | ||||||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||||||
| 440 | =item B<usage> =E<gt> I<Str> | ||||||||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||||||||
| 442 | Static usage summary of the command. | ||||||||||||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||||||||||||
| 444 | See also L<Term::CLI::Role::HelpText|Term::CLI::Role::HelpText/ATTRIBUTES>. | ||||||||||||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||||||||||||
| 446 | (B<NOTE:> You will rarely have to specify this, as it can be determined | ||||||||||||||||||||||||||||||||||||||||||
| 447 | automatically.) | ||||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||
| 449 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||||||||||||
| 451 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||
| 453 | =head1 INHERITED METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||||||||||||
| 455 | This class inherits all the attributes and accessors of | ||||||||||||||||||||||||||||||||||||||||||
| 456 | L<Term::CLI::Element|Term::CLI::Element>(3p), | ||||||||||||||||||||||||||||||||||||||||||
| 457 | L<Term::CLI::Role::CommandSet|Term::CLI::Role::CommandSet>(3p), | ||||||||||||||||||||||||||||||||||||||||||
| 458 | L<Term::CLI::Role::HelpText|Term::CLI::Role::HelpText>(3p), | ||||||||||||||||||||||||||||||||||||||||||
| 459 | and | ||||||||||||||||||||||||||||||||||||||||||
| 460 | L<Term::CLI::Role::ArgumentSet|Term::CLI::Role::ArgumentSet>(3p), | ||||||||||||||||||||||||||||||||||||||||||
| 461 | most notably: | ||||||||||||||||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||||||||||||||||
| 463 | =head2 Accessors | ||||||||||||||||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||||||||||||||||
| 465 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||||||||||||||||
| 467 | =item B<has_arguments> | ||||||||||||||||||||||||||||||||||||||||||
| 468 | X<has_arguments> | ||||||||||||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||||||||||||
| 470 | See | ||||||||||||||||||||||||||||||||||||||||||
| 471 | L<has_arguments in Term::CLI::Role::ArgumentSet|Term::CLI::Role::ArgumentSet/has_arguments>. | ||||||||||||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||||||||||||
| 473 | =item B<has_callback> | ||||||||||||||||||||||||||||||||||||||||||
| 474 | X<has_callback> | ||||||||||||||||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||||||||||||||||
| 476 | See | ||||||||||||||||||||||||||||||||||||||||||
| 477 | L<has_callback in Term::CLI::Role::CommandSet|Term::CLI::Role::CommandSet/has_callback>. | ||||||||||||||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||||||||||||
| 479 | =item B<has_commands> | ||||||||||||||||||||||||||||||||||||||||||
| 480 | X<has_commands> | ||||||||||||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||||||||||||
| 482 | See | ||||||||||||||||||||||||||||||||||||||||||
| 483 | L<has_commands in Term::CLI::Role::CommandSet|Term::CLI::Role::CommandSet/has_commands>. | ||||||||||||||||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||||||||||||||||
| 485 | =item B<arguments> | ||||||||||||||||||||||||||||||||||||||||||
| 486 | X<arguments> | ||||||||||||||||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||||||||||||||||
| 488 | See | ||||||||||||||||||||||||||||||||||||||||||
| 489 | L<arguments in Term::CLI::Role::ArgumentSet|Term::CLI::Role::ArgumentSet/arguments>. | ||||||||||||||||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||||||||||||||||
| 491 | Returns a list of C<Term::CLI::Argument> object instances. | ||||||||||||||||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||||||||||||||||
| 493 | =item B<commands> | ||||||||||||||||||||||||||||||||||||||||||
| 494 | X<commands> | ||||||||||||||||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||||||||||||||||
| 496 | See | ||||||||||||||||||||||||||||||||||||||||||
| 497 | L<commands in Term::CLI::Role::CommandSet|Term::CLI::Role::CommandSet/commands>. | ||||||||||||||||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||||||||||||||||
| 499 | Returns a list of C<Term::CLI::Command> object instances. | ||||||||||||||||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||||||||||||||||
| 501 | =item B<callback> ( [ I<CodeRef> ] ) | ||||||||||||||||||||||||||||||||||||||||||
| 502 | X<callback> | ||||||||||||||||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||||||||||||||||
| 504 | See | ||||||||||||||||||||||||||||||||||||||||||
| 505 | L<callback in Term::CLI::Role::CommandSet|Term::CLI::Role::CommandSet/callback>. | ||||||||||||||||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||||||||||||||||
| 507 | =item B<description> ( [ I<Str> ] ) | ||||||||||||||||||||||||||||||||||||||||||
| 508 | X<description> | ||||||||||||||||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||||||||||||||||
| 510 | See | ||||||||||||||||||||||||||||||||||||||||||
| 511 | L<description in Term::CLI::Role::HelpText|Term::CLI::Role::HelpText/description>. | ||||||||||||||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||||||||||||
| 513 | =item B<summary> ( [ I<Str> ] ) | ||||||||||||||||||||||||||||||||||||||||||
| 514 | X<summary> | ||||||||||||||||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||||||||||||||||
| 516 | See | ||||||||||||||||||||||||||||||||||||||||||
| 517 | L<summary in Term::CLI::Role::HelpText|Term::CLI::Role::HelpText/summary>. | ||||||||||||||||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||||||||||||||||
| 519 | =item B<usage> ( [ I<Str> ] ) | ||||||||||||||||||||||||||||||||||||||||||
| 520 | X<usage> | ||||||||||||||||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||||||||||||||||
| 522 | See | ||||||||||||||||||||||||||||||||||||||||||
| 523 | L<description in Term::CLI::Role::HelpText|Term::CLI::Role::HelpText/description>. | ||||||||||||||||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||||||||||||||||
| 525 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||||||||||||||||
| 527 | =head2 Others | ||||||||||||||||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||||||||||||||||
| 529 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||||||||||||||||
| 531 | =item B<argument_names> | ||||||||||||||||||||||||||||||||||||||||||
| 532 | X<argument_names> | ||||||||||||||||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||||||||||||||||
| 534 | Return the list of argument names, in the original order. | ||||||||||||||||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||||||||||||||||
| 536 | =item B<command_names> | ||||||||||||||||||||||||||||||||||||||||||
| 537 | X<command_names> | ||||||||||||||||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||||||||||||||||
| 539 | Return the list of (sub-)command names, sorted alphabetically. | ||||||||||||||||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||||||||||||||||
| 541 | =item B<find_command> ( I<CMD> ) | ||||||||||||||||||||||||||||||||||||||||||
| 542 | X<find_command> | ||||||||||||||||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||||||||||||||||
| 544 | Check whether I<CMD> is a sub-command of this command. If so, | ||||||||||||||||||||||||||||||||||||||||||
| 545 | return the appropriate C<Term::CLI::Command> reference; otherwise, | ||||||||||||||||||||||||||||||||||||||||||
| 546 | return C<undef>. | ||||||||||||||||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||||||||||||||||
| 548 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||||||||||||
| 550 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||||||||||||||||
| 552 | =head2 Accessors | ||||||||||||||||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||||||||||||||||
| 554 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||||||||||||||||
| 556 | =item B<has_options> | ||||||||||||||||||||||||||||||||||||||||||
| 557 | X<has_options> | ||||||||||||||||||||||||||||||||||||||||||
| 558 | |||||||||||||||||||||||||||||||||||||||||||
| 559 | Predicate functions that return whether or not the associated | ||||||||||||||||||||||||||||||||||||||||||
| 560 | attribute has been set. | ||||||||||||||||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||||||||||||||||
| 562 | =item B<options> ( [ I<ArrayRef> ] ) | ||||||||||||||||||||||||||||||||||||||||||
| 563 | X<options> | ||||||||||||||||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||||||||||||||||
| 565 | I<ArrayRef> with command-line options in L<Getopt::Long|Getopt::Long>(3p) | ||||||||||||||||||||||||||||||||||||||||||
| 566 | format. | ||||||||||||||||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||||||||||||||||
| 568 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||||||||||||||||
| 570 | =head2 Others | ||||||||||||||||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||||||||||||||||
| 572 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||||||||||||||||
| 574 | =item B<complete> ( I<TEXT>, I<STATE> ) | ||||||||||||||||||||||||||||||||||||||||||
| 575 | X<complete> | ||||||||||||||||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||||||||||||||||
| 577 | Called by L<Term::CLI::Role::CommandSet|Term::CLI::Role::CommandSet>'s | ||||||||||||||||||||||||||||||||||||||||||
| 578 | L<complete_line|Term::CLI::Role::CommandSet/complete_line> method, | ||||||||||||||||||||||||||||||||||||||||||
| 579 | or another C<Term::CLI::Command>'s C<complete> function. | ||||||||||||||||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||||||||||||||||
| 581 | The method can complete options, sub-commands, and arguments. | ||||||||||||||||||||||||||||||||||||||||||
| 582 | Completions of commands and arguments is delegated to the appropriate | ||||||||||||||||||||||||||||||||||||||||||
| 583 | C<Term::CLI::Command> and L<Term::CLI::Argument|Term::CLI::Argument> | ||||||||||||||||||||||||||||||||||||||||||
| 584 | instances, resp. | ||||||||||||||||||||||||||||||||||||||||||
| 585 | |||||||||||||||||||||||||||||||||||||||||||
| 586 | =item B<option_names> | ||||||||||||||||||||||||||||||||||||||||||
| 587 | X<option_names> | ||||||||||||||||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||||||||||||||||
| 589 | Return a list of all command line options for this command. | ||||||||||||||||||||||||||||||||||||||||||
| 590 | Long options are prefixed with C<-->, and one-letter options | ||||||||||||||||||||||||||||||||||||||||||
| 591 | are prefixed with C<->. | ||||||||||||||||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||||||||||||||||
| 593 | Example: | ||||||||||||||||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||||||||||||||||
| 595 | $cmd->options( [ 'verbose|v+', 'debug|d', 'help|h|?' ] ); | ||||||||||||||||||||||||||||||||||||||||||
| 596 | say join(' ', $cmd->option_names); | ||||||||||||||||||||||||||||||||||||||||||
| 597 | # output: --debug --help --verbose -? -d -h -v | ||||||||||||||||||||||||||||||||||||||||||
| 598 | |||||||||||||||||||||||||||||||||||||||||||
| 599 | =item B<execute_command> ( I<KEY> =E<gt> I<VAL>, ... ) | ||||||||||||||||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||||||||||||||||
| 601 | This method is called by C<Term::CLI::Role::CommandSet>'s | ||||||||||||||||||||||||||||||||||||||||||
| 602 | L<execute_line|Term::CLI::Role::CommandSet/execute_line> method. | ||||||||||||||||||||||||||||||||||||||||||
| 603 | It should not be called directly. | ||||||||||||||||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||||||||||||||||
| 605 | It accepts the same list of parameters as the | ||||||||||||||||||||||||||||||||||||||||||
| 606 | L<command callback|Term::CLI::Role::CommandSet/callback> | ||||||||||||||||||||||||||||||||||||||||||
| 607 | function | ||||||||||||||||||||||||||||||||||||||||||
| 608 | (see L<Term::CLI::Role::CommandSet|Term::CLI::Role::CommandSet>), | ||||||||||||||||||||||||||||||||||||||||||
| 609 | and returns the same structure. | ||||||||||||||||||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||||||||||||||||
| 611 | The C<arguments> parameter (an I<ArrayRef>) should contain the words | ||||||||||||||||||||||||||||||||||||||||||
| 612 | on the command line that have not been parsed yet. | ||||||||||||||||||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||||||||||||||||
| 614 | Depending on whether the object has sub-commands or arguments, the rest of | ||||||||||||||||||||||||||||||||||||||||||
| 615 | the line is parsed (possibly handing off to another sub-command), and the | ||||||||||||||||||||||||||||||||||||||||||
| 616 | results are passed to the | ||||||||||||||||||||||||||||||||||||||||||
| 617 | L<command's callback|Term::CLI::Role::CommandSet/callback> | ||||||||||||||||||||||||||||||||||||||||||
| 618 | function. | ||||||||||||||||||||||||||||||||||||||||||
| 619 | |||||||||||||||||||||||||||||||||||||||||||
| 620 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 621 | |||||||||||||||||||||||||||||||||||||||||||
| 622 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 623 | |||||||||||||||||||||||||||||||||||||||||||
| 624 | L<Term::CLI::Argument|Term::CLI::Argument>(3p), | ||||||||||||||||||||||||||||||||||||||||||
| 625 | L<Term::CLI::Element|Term::CLI::Element>(3p), | ||||||||||||||||||||||||||||||||||||||||||
| 626 | L<Term::CLI::Role::ArgumentSet|Term::CLI::Role::ArgumentSet>(3p), | ||||||||||||||||||||||||||||||||||||||||||
| 627 | L<Term::CLI::Role::CommandSet|Term::CLI::Role::CommandSet>(3p), | ||||||||||||||||||||||||||||||||||||||||||
| 628 | L<Term::CLI::Role::HelpText|Term::CLI::Role::HelpText>(3p), | ||||||||||||||||||||||||||||||||||||||||||
| 629 | L<Term::CLI|Term::CLI>(3p), | ||||||||||||||||||||||||||||||||||||||||||
| 630 | L<Term::CLI::Util|Term::CLI::Util>(3p), | ||||||||||||||||||||||||||||||||||||||||||
| 631 | L<Getopt::Long|Getopt::Long>(3p). | ||||||||||||||||||||||||||||||||||||||||||
| 632 | |||||||||||||||||||||||||||||||||||||||||||
| 633 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 634 | |||||||||||||||||||||||||||||||||||||||||||
| 635 | Steven Bakker E<lt>sbakker@cpan.orgE<gt>, 2018. | ||||||||||||||||||||||||||||||||||||||||||
| 636 | |||||||||||||||||||||||||||||||||||||||||||
| 637 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 638 | |||||||||||||||||||||||||||||||||||||||||||
| 639 | Copyright (c) 2018 Steven Bakker | ||||||||||||||||||||||||||||||||||||||||||
| 640 | |||||||||||||||||||||||||||||||||||||||||||
| 641 | This module is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||||||||||||||||
| 642 | it under the same terms as Perl itself. See "perldoc perlartistic." | ||||||||||||||||||||||||||||||||||||||||||
| 643 | |||||||||||||||||||||||||||||||||||||||||||
| 644 | This software is distributed in the hope that it will be useful, | ||||||||||||||||||||||||||||||||||||||||||
| 645 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||||||||||||||||||||||||||||||
| 646 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||||||||||||||||||||||||||||||||||||||||||
| 647 | |||||||||||||||||||||||||||||||||||||||||||
| 648 | =begin __PODCOVERAGE | ||||||||||||||||||||||||||||||||||||||||||
| 649 | |||||||||||||||||||||||||||||||||||||||||||
| 650 | =head1 THIS SECTION SHOULD BE HIDDEN | ||||||||||||||||||||||||||||||||||||||||||
| 651 | |||||||||||||||||||||||||||||||||||||||||||
| 652 | This section is meant for methods that should not be considered | ||||||||||||||||||||||||||||||||||||||||||
| 653 | for coverage. This typically includes things like BUILD and DEMOLISH from | ||||||||||||||||||||||||||||||||||||||||||
| 654 | Moo/Moose. It is possible to skip these when using the Pod::Coverage class | ||||||||||||||||||||||||||||||||||||||||||
| 655 | (using C<also_private>), but this is not an option when running C<cover> | ||||||||||||||||||||||||||||||||||||||||||
| 656 | from the command line. | ||||||||||||||||||||||||||||||||||||||||||
| 657 | |||||||||||||||||||||||||||||||||||||||||||
| 658 | The simplest trick is to add a hidden section with an item list containing | ||||||||||||||||||||||||||||||||||||||||||
| 659 | these methods. | ||||||||||||||||||||||||||||||||||||||||||
| 660 | |||||||||||||||||||||||||||||||||||||||||||
| 661 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 662 | |||||||||||||||||||||||||||||||||||||||||||
| 663 | =item BUILD | ||||||||||||||||||||||||||||||||||||||||||
| 664 | |||||||||||||||||||||||||||||||||||||||||||
| 665 | =item DEMOLISH | ||||||||||||||||||||||||||||||||||||||||||
| 666 | |||||||||||||||||||||||||||||||||||||||||||
| 667 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 668 | |||||||||||||||||||||||||||||||||||||||||||
| 669 | =end __PODCOVERAGE | ||||||||||||||||||||||||||||||||||||||||||
| 670 | |||||||||||||||||||||||||||||||||||||||||||
| 671 | =cut | ||||||||||||||||||||||||||||||||||||||||||