| 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 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 | use 5.014_001; | ||
| 22 | |||
| 23 | package Term::CLI::Command 0.051003 { | ||
| 24 | |||
| 25 | use Modern::Perl 1.20140107; | ||
| 26 | use List::Util 1.38 qw( first min ); | ||
| 27 | use Getopt::Long 2.42 qw( GetOptionsFromArray ); | ||
| 28 | use Types::Standard 1.000005 qw( | ||
| 29 | ArrayRef | ||
| 30 | CodeRef | ||
| 31 | InstanceOf | ||
| 32 | Maybe | ||
| 33 | Str | ||
| 34 | ); | ||
| 35 | |||
| 36 | use Term::CLI::L10N; | ||
| 37 | |||
| 38 | use Moo 1.000001; | ||
| 39 | use namespace::clean 0.25; | ||
| 40 | |||
| 41 | extends 'Term::CLI::Element'; | ||
| 42 | |||
| 43 | has options => ( | ||
| 44 | is => 'rw', | ||
| 45 | isa => Maybe[ArrayRef[Str]], | ||
| 46 | predicate => 1 | ||
| 47 | ); | ||
| 48 | |||
| 49 | |||
| 50 | with ('Term::CLI::Role::CommandSet'); | ||
| 51 | with ('Term::CLI::Role::ArgumentSet'); | ||
| 52 | with ('Term::CLI::Role::HelpText'); | ||
| 53 | |||
| 54 | |||
| 55 | sub option_names { | ||
| 56 | my $self = shift; | ||
| 57 | my $opt_specs = $self->options or return (); | ||
| 58 | my @names; | ||
| 59 | for my $spec (@$opt_specs) { | ||
| 60 | for my $optname (split(qr/\|/, $spec =~ s/^([^!+=:]+).*/$1/r)) { | ||
| 61 | push @names, length($optname) == 1 ? "-$optname" : "--$optname"; | ||
| 62 | } | ||
| 63 | } | ||
| 64 | return @names; | ||
| 65 | } | ||
| 66 | |||
| 67 | |||
| 68 | sub complete_line { | ||
| 69 | my ($self, @words) = @_; | ||
| 70 | |||
| 71 | my $partial = $words[$#words] // ''; | ||
| 72 | |||
| 73 | if ($self->has_options) { | ||
| 74 | |||
| 75 | Getopt::Long::Configure(qw(bundling require_order pass_through)); | ||
| 76 | |||
| 77 | my $opt_specs = $self->options; | ||
| 78 | |||
| 79 | my %parsed_opts; | ||
| 80 | |||
| 81 | my $has_terminator = first { $_ eq '--' } @words[0..$#words-1]; | ||
| 82 | |||
| 83 | eval { GetOptionsFromArray(\@words, \%parsed_opts, @$opt_specs) }; | ||
| 84 | |||
| 85 | if (!$has_terminator && @words <= 1 && $partial =~ /^-/) { | ||
| 86 | # We have to complete a command-line option. | ||
| 87 | return grep { rindex($_, $partial, 0) == 0 } $self->option_names; | ||
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 | # If the command has arguments, try to skip over them. | ||
| 92 | if ($self->has_arguments) { | ||
| 93 | my @args = $self->arguments; | ||
| 94 | my $n = 0; | ||
| 95 | while (@words > 1) { | ||
| 96 | last if @args == 0; | ||
| 97 | shift @words; | ||
| 98 | $n++; | ||
| 99 | if ($args[0]->max_occur > 0 and $n >= $args[0]->max_occur) { | ||
| 100 | shift @args; | ||
| 101 | $n = 0; | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | if (@args) { | ||
| 106 | return $args[0]->complete($words[0]); | ||
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 | if ($self->has_commands) { | ||
| 111 | if (@words <= 1) { | ||
| 112 | return grep { rindex($_, $partial, 0) == 0 } $self->command_names; | ||
| 113 | } | ||
| 114 | elsif (my $cmd = $self->find_command($words[0])) { | ||
| 115 | return $cmd->complete_line(@words[1..$#words]); | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | return (); | ||
| 120 | } | ||
| 121 | |||
| 122 | |||
| 123 | sub execute { | ||
| 124 | my ($self, %args) = @_; | ||
| 125 | |||
| 126 | $args{status} = 0; | ||
| 127 | $args{error} = ''; | ||
| 128 | |||
| 129 | # Dereference and copy arguments/unparsed/options to prevent | ||
| 130 | # unwanted side-effects. | ||
| 131 | $args{arguments} = [@{$args{arguments}}]; | ||
| 132 | $args{unparsed} = [@{$args{unparsed}}]; | ||
| 133 | $args{options} = {%{$args{options}}}; | ||
| 134 | $args{command_path} = [@{$args{command_path}}]; | ||
| 135 | |||
| 136 | push @{$args{command_path}}, $self; | ||
| 137 | |||
| 138 | if ($self->has_options) { | ||
| 139 | my $opt_specs = $self->options; | ||
| 140 | |||
| 141 | Getopt::Long::Configure(qw(bundling require_order no_pass_through)); | ||
| 142 | |||
| 143 | my $error = ''; | ||
| 144 | my $ok = do { | ||
| 145 | local( $SIG{__WARN__} ) = sub { chomp($error = join('', @_)) }; | ||
| 146 | GetOptionsFromArray($args{unparsed}, $args{options}, @$opt_specs); | ||
| 147 | }; | ||
| 148 | |||
| 149 | if (!$ok) { | ||
| 150 | $args{status} = -1; | ||
| 151 | $args{error} = $error; | ||
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 | if ($args{status} >= 0) { | ||
| 156 | if ($self->has_arguments or !$self->has_commands) { | ||
| 157 | %args = $self->_check_arguments(%args); | ||
| 158 | } | ||
| 159 | } | ||
| 160 | if ($args{status} >= 0 and $self->has_commands) { | ||
| 161 | %args = $self->_execute_command(%args); | ||
| 162 | } | ||
| 163 | return $self->try_callback( %args ); | ||
| 164 | } | ||
| 165 | |||
| 166 | |||
| 167 | sub _too_few_args_error { | ||
| 168 | my ($self, $arg_spec) = @_; | ||
| 169 | |||
| 170 | if ($arg_spec->max_occur == $arg_spec->min_occur) { | ||
| 171 | if ($arg_spec->min_occur == 1) { | ||
| 172 | return loc("missing '[_1]' argument", $arg_spec->name); | ||
| 173 | } | ||
| 174 | else { | ||
| 175 | return loc("need [_1] '[_2]' [numerate,_1,argument]", | ||
| 176 | $arg_spec->min_occur, $arg_spec->name, | ||
| 177 | ); | ||
| 178 | } | ||
| 179 | } | ||
| 180 | elsif ($arg_spec->max_occur - $arg_spec->min_occur == 1) { | ||
| 181 | return loc("need [_1] or [_2] '[_3]' arguments", | ||
| 182 | $arg_spec->min_occur, | ||
| 183 | $arg_spec->max_occur, | ||
| 184 | $arg_spec->name, | ||
| 185 | ); | ||
| 186 | } | ||
| 187 | elsif ($arg_spec->max_occur > 1) { | ||
| 188 | return loc("need between [_1] and [_2] '[_3]' arguments", | ||
| 189 | $arg_spec->min_occur, | ||
| 190 | $arg_spec->max_occur, | ||
| 191 | $arg_spec->name, | ||
| 192 | ); | ||
| 193 | } | ||
| 194 | else { | ||
| 195 | return loc("need at least [_1] '[_2]' [numerate,_1,argument]", | ||
| 196 | $arg_spec->min_occur, | ||
| 197 | $arg_spec->name, | ||
| 198 | ); | ||
| 199 | } | ||
| 200 | } | ||
| 201 | |||
| 202 | |||
| 203 | sub _check_arguments { | ||
| 204 | my ($self, %args) = @_; | ||
| 205 | |||
| 206 | my $unparsed = $args{unparsed}; | ||
| 207 | |||
| 208 | my @arg_spec = $self->arguments; | ||
| 209 | |||
| 210 | if (@arg_spec == 0 and @$unparsed > 0) { | ||
| 211 | return (%args, | ||
| 212 | status => -1, | ||
| 213 | error => loc('no arguments allowed'), | ||
| 214 | ); | ||
| 215 | } | ||
| 216 | |||
| 217 | my $argno = 0; | ||
| 218 | my @parsed_args; | ||
| 219 | for my $arg_spec (@arg_spec) { | ||
| 220 | if (@$unparsed < $arg_spec->min_occur) { | ||
| 221 | return (%args, | ||
| 222 | status => -1, | ||
| 223 | error => $self->_too_few_args_error($arg_spec), | ||
| 224 | ); | ||
| 225 | } | ||
| 226 | |||
| 227 | my $args_to_check | ||
| 228 | = $arg_spec->max_occur > 0 | ||
| 229 | ? min($arg_spec->max_occur, scalar @$unparsed) | ||
| 230 | : scalar @$unparsed; | ||
| 231 | |||
| 232 | for my $i (1..$args_to_check) { | ||
| 233 | my $arg = $unparsed->[0]; | ||
| 234 | $argno++; | ||
| 235 | my $arg_value = $arg_spec->validate($arg); | ||
| 236 | if (!defined $arg_value) { | ||
| 237 | return (%args, | ||
| 238 | status => -1, | ||
| 239 | error => "arg#$argno, '$arg': " . $arg_spec->error | ||
| 240 | . " ".loc("for")." '" . $arg_spec->name . "'" | ||
| 241 | ); | ||
| 242 | } | ||
| 243 | push @{$args{arguments}}, $arg_value; | ||
| 244 | shift @$unparsed; | ||
| 245 | } | ||
| 246 | } | ||
| 247 | |||
| 248 | # At this point, we have processed all our arg_spec. The only way there | ||
| 249 | # are any elements left in @arguments is for the last arg_spec to have | ||
| 250 | # a max_occur that is exceeded. If the command has no sub-commands that | ||
| 251 | # is surely an error. If it does have sub-commands, we'll leave it to | ||
| 252 | # be parsed further. | ||
| 253 | if (@$unparsed > 0 and !$self->has_commands) { | ||
| 254 | my $last_spec = $arg_spec[$#arg_spec]; | ||
| 255 | return (%args, status => -1, | ||
| 256 | error => loc("too many '[_1]' arguments (max. [_2])", | ||
| 257 | $last_spec->name, | ||
| 258 | $last_spec->max_occur, | ||
| 259 | ), | ||
| 260 | ); | ||
| 261 | } | ||
| 262 | return %args; | ||
| 263 | } | ||
| 264 | |||
| 265 | |||
| 266 | sub _execute_command { | ||
| 267 | my ($self, %args) = @_; | ||
| 268 | |||
| 269 | my $unparsed = $args{unparsed}; | ||
| 270 | |||
| 271 | if (@$unparsed == 0) { | ||
| 272 | if (scalar $self->commands == 1) { | ||
| 273 | my ($cmd) = $self->commands; | ||
| 274 | return (%args, status => -1, | ||
| 275 | error => loc("incomplete command: missing '[_1]'", $cmd->name) | ||
| 276 | ); | ||
| 277 | } | ||
| 278 | return (%args, status => -1, error => loc("missing sub-command")); | ||
| 279 | } | ||
| 280 | |||
| 281 | my $cmd_name = $unparsed->[0]; | ||
| 282 | |||
| 283 | my $cmd = $self->find_command($cmd_name); | ||
| 284 | |||
| 285 | if (!$cmd) { | ||
| 286 | if (scalar $self->commands == 1) { | ||
| 287 | ($cmd) = $self->commands; | ||
| 288 | return (%args, status => -1, | ||
| 289 | error => loc( | ||
| 290 | "expected '[_1]' instead of '[_2]'", | ||
| 291 | $cmd->name, | ||
| 292 | $cmd_name | ||
| 293 | ), | ||
| 294 | ); | ||
| 295 | } | ||
| 296 | return (%args, | ||
| 297 | status => -1, | ||
| 298 | error => loc("unknown sub-command '[_1]'", $cmd_name) | ||
| 299 | ); | ||
| 300 | } | ||
| 301 | |||
| 302 | shift @$unparsed; | ||
| 303 | return $cmd->execute(%args); | ||
| 304 | } | ||
| 305 | |||
| 306 | |||
| 307 | } | ||
| 308 | |||
| 309 | 1; | ||
| 310 | |||
| 311 | __END__ |