| 1 | #============================================================================= | ||
| 2 | # | ||
| 3 | # Module: Term::CLI | ||
| 4 | # | ||
| 5 | # Description: Class for CLI parsing | ||
| 6 | # | ||
| 7 | # Author: Steven Bakker (SBAKKER), <sbakker@cpan.org> | ||
| 8 | # Created: 31/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 | 4 | ▶ | use 5.014_001; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 22 | |||
| 23 | package Term::CLI 0.051003 { | ||
| 24 | |||
| 25 | 4 | ▶ | use Modern::Perl 1.20140107; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 26 | 4 | ▶ | use Text::ParseWords 3.29 qw( parse_line ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 27 | 4 | ▶ | use Term::CLI::ReadLine; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 28 | use FindBin 1.50; | ||
| 29 | |||
| 30 | use Term::CLI::L10N; | ||
| 31 | |||
| 32 | # Load all Term::CLI classes so the user doesn't have to. | ||
| 33 | |||
| 34 | use Term::CLI::Argument::Bool; | ||
| 35 | use Term::CLI::Argument::Enum; | ||
| 36 | use Term::CLI::Argument::Filename; | ||
| 37 | use Term::CLI::Argument::Number; | ||
| 38 | use Term::CLI::Argument::Number::Float; | ||
| 39 | use Term::CLI::Argument::Number::Int; | ||
| 40 | use Term::CLI::Argument::String; | ||
| 41 | |||
| 42 | use Term::CLI::Command; | ||
| 43 | use Term::CLI::Command::Help; | ||
| 44 | |||
| 45 | use Types::Standard 1.000005 qw( | ||
| 46 | ArrayRef | ||
| 47 | CodeRef | ||
| 48 | InstanceOf | ||
| 49 | Maybe | ||
| 50 | RegexpRef | ||
| 51 | Str | ||
| 52 | Int | ||
| 53 | ); | ||
| 54 | |||
| 55 | use Moo 1.000001; | ||
| 56 | use namespace::clean 0.25; | ||
| 57 | |||
| 58 | extends 'Term::CLI::Base'; | ||
| 59 | |||
| 60 | with('Term::CLI::Role::CommandSet'); | ||
| 61 | |||
| 62 | # Provide a default for 'name'. | ||
| 63 | has '+name' => ( | ||
| 64 | default => sub { $FindBin::Script } | ||
| 65 | ); | ||
| 66 | |||
| 67 | has prompt => ( | ||
| 68 | is => 'rw', | ||
| 69 | isa => Str, | ||
| 70 | default => sub { '~> ' } | ||
| 71 | ); | ||
| 72 | |||
| 73 | has split_function => ( | ||
| 74 | is => 'rw', | ||
| 75 | isa => CodeRef, | ||
| 76 | default => sub { \&_default_split } | ||
| 77 | ); | ||
| 78 | |||
| 79 | has skip => ( | ||
| 80 | is => 'rw', | ||
| 81 | isa => RegexpRef, | ||
| 82 | ); | ||
| 83 | |||
| 84 | has history_file => ( | ||
| 85 | is => 'rw', | ||
| 86 | isa => Str | ||
| 87 | ); | ||
| 88 | |||
| 89 | has history_lines => ( | ||
| 90 | is => 'rw', | ||
| 91 | isa => Int, | ||
| 92 | default => sub { 1000 }, | ||
| 93 | trigger => 1, | ||
| 94 | ); | ||
| 95 | |||
| 96 | has word_delimiters => ( is => 'rw', isa => Str, default => sub {" \n\t"} ); | ||
| 97 | has quote_characters => ( is => 'rw', isa => Str, default => sub {q("')} ); | ||
| 98 | |||
| 99 | sub BUILD { | ||
| 100 | my ($self, $args) = @_; | ||
| 101 | |||
| 102 | my $term = Term::CLI::ReadLine->new($self->name)->term; | ||
| 103 | $term->Attribs->{completion_function} = sub { $self->complete_line(@_) }; | ||
| 104 | $term->Attribs->{char_is_quoted_p} = sub { $self->_is_escaped(@_) }; | ||
| 105 | |||
| 106 | $self->_set_completion_attribs; | ||
| 107 | |||
| 108 | if (! exists $args->{callback} ) { | ||
| 109 | $self->callback(\&_default_callback); | ||
| 110 | } | ||
| 111 | |||
| 112 | if (!exists $args->{history_file}) { | ||
| 113 | my $hist_file = $self->name; | ||
| 114 | $hist_file =~ s{^/}{}g; | ||
| 115 | $hist_file =~ s{/$}{}g; | ||
| 116 | $hist_file =~ s{/+}{-}g; | ||
| 117 | $self->history_file("$::ENV{HOME}/.${hist_file}_history"); | ||
| 118 | } | ||
| 119 | |||
| 120 | # Set ReadLine history size... | ||
| 121 | $self->term->StifleHistory($self->history_lines); | ||
| 122 | } | ||
| 123 | |||
| 124 | sub _trigger_history_lines { | ||
| 125 | my ($self, $arg) = @_; | ||
| 126 | |||
| 127 | # the ReadLine object may not have been initialised yet... | ||
| 128 | $self->term->StifleHistory($arg) if $self->term; | ||
| 129 | } | ||
| 130 | |||
| 131 | # %args = $self->_default_callback(%args); | ||
| 132 | # | ||
| 133 | # Default top-level callback if none is given. | ||
| 134 | # Simply check the status and print an error | ||
| 135 | # message if status < 0. | ||
| 136 | sub _default_callback { | ||
| 137 | my ($self, %args) = @_; | ||
| 138 | |||
| 139 | if ($args{status} < 0) { | ||
| 140 | say STDERR loc("ERROR"), ": ", $args{error}; | ||
| 141 | } | ||
| 142 | return %args; | ||
| 143 | } | ||
| 144 | |||
| 145 | |||
| 146 | # ($error, @words) = $self->_default_split($text); | ||
| 147 | # | ||
| 148 | # Default function to split a string into words. | ||
| 149 | # Similar to "shellwords()", except that we use | ||
| 150 | # "parse_line" to support custom delimiters. | ||
| 151 | # | ||
| 152 | # Unfortunately, there's no way to specify custom | ||
| 153 | # quote characters. | ||
| 154 | # | ||
| 155 | sub _default_split { | ||
| 156 | my ($self, $text) = @_; | ||
| 157 | |||
| 158 | if ($text =~ /\S/) { | ||
| 159 | my $delim = $self->word_delimiters; | ||
| 160 | $text =~ s/^[$delim]+//; | ||
| 161 | my @words = parse_line(qr{[$delim]+}, 0, $text); | ||
| 162 | pop @words if @words and not defined $words[-1]; | ||
| 163 | my $error = @words ? '' : loc('unbalanced quotes in input'); | ||
| 164 | return ($error, @words); | ||
| 165 | } | ||
| 166 | else { | ||
| 167 | return (''); | ||
| 168 | } | ||
| 169 | } | ||
| 170 | |||
| 171 | |||
| 172 | # BOOL = CLI->_is_escaped($line, $index); | ||
| 173 | # | ||
| 174 | # The character at $index in $line is a possible word break | ||
| 175 | # character. Check if it is perhaps escaped. | ||
| 176 | # | ||
| 177 | sub _is_escaped { | ||
| 178 | my ($self,$line, $index) = @_; | ||
| 179 | return 0 if $index <= 0; | ||
| 180 | return 0 if substr($line, $index-1, 1) ne '\\'; | ||
| 181 | return !$self->_is_escaped($line, $index-1); | ||
| 182 | } | ||
| 183 | |||
| 184 | |||
| 185 | # CLI->_set_completion_attribs(); | ||
| 186 | # | ||
| 187 | # Set some attributes in the Term::ReadLine object related to | ||
| 188 | # custom completion. | ||
| 189 | # | ||
| 190 | sub _set_completion_attribs { | ||
| 191 | my $self = shift; | ||
| 192 | my $term = $self->term; | ||
| 193 | |||
| 194 | # Default: '" | ||
| 195 | $term->Attribs->{completer_quote_characters} = $self->quote_characters; | ||
| 196 | |||
| 197 | # Default: \n\t\\"'`@$><=;|&{( and <space> | ||
| 198 | $term->Attribs->{completer_word_break_characters} = $self->word_delimiters; | ||
| 199 | |||
| 200 | # Default: <space> | ||
| 201 | $term->Attribs->{completion_append_character} | ||
| 202 | = substr($self->word_delimiters, 0, 1); | ||
| 203 | } | ||
| 204 | |||
| 205 | |||
| 206 | # CLI->_split_line( $text ); | ||
| 207 | # | ||
| 208 | # Attempt to split $text into words. Use a custom split function if | ||
| 209 | # necessary. | ||
| 210 | # | ||
| 211 | sub _split_line { | ||
| 212 | my ($self, $text) = @_; | ||
| 213 | return $self->split_function->($self, $text); | ||
| 214 | } | ||
| 215 | |||
| 216 | |||
| 217 | # See POD X<complete_line> | ||
| 218 | sub complete_line { | ||
| 219 | my ($self, $text, $line, $start) = @_; | ||
| 220 | |||
| 221 | $self->_set_completion_attribs; | ||
| 222 | |||
| 223 | my $quote_char | ||
| 224 | = $self->term->Attribs->{completion_quote_character} =~ s/\000//gr; | ||
| 225 | |||
| 226 | my @words; | ||
| 227 | |||
| 228 | if ($start > 0) { | ||
| 229 | if (length $quote_char) { | ||
| 230 | # ReadLine thinks the $text to be completed is quoted. | ||
| 231 | # The quote character will precede the $start of $text. | ||
| 232 | # Make sure we do not include it in the text to break | ||
| 233 | # into words... | ||
| 234 | (my $err, @words) = $self->_split_line(substr($line, 0, $start-1)); | ||
| 235 | } | ||
| 236 | else { | ||
| 237 | (my $err, @words) = $self->_split_line(substr($line, 0, $start)); | ||
| 238 | } | ||
| 239 | } | ||
| 240 | |||
| 241 | push @words, $text; | ||
| 242 | |||
| 243 | my @list; | ||
| 244 | |||
| 245 | if (@words == 1) { | ||
| 246 | @list = grep { rindex($_, $words[0], 0) == 0 } $self->command_names; | ||
| 247 | } | ||
| 248 | elsif (my $cmd = $self->find_command($words[0])) { | ||
| 249 | @list = $cmd->complete_line(@words[1..$#words]); | ||
| 250 | } | ||
| 251 | |||
| 252 | # Escape spaces in reply if necessary. | ||
| 253 | if (length $quote_char) { | ||
| 254 | return @list; | ||
| 255 | } | ||
| 256 | else { | ||
| 257 | my $delim = $self->word_delimiters; | ||
| 258 | return map { s/([$delim])/\\$1/gr } @list; | ||
| 259 | } | ||
| 260 | } | ||
| 261 | |||
| 262 | |||
| 263 | # %old_sig = CLI->_set_signal_handlers(); | ||
| 264 | # | ||
| 265 | # Set signal handlers to ensure proper terminal/CLI handling in the | ||
| 266 | # face of various signals (^C ^\ ^Z). | ||
| 267 | # | ||
| 268 | sub _set_signal_handlers { | ||
| 269 | my $self = shift; | ||
| 270 | |||
| 271 | my %old_sig = %SIG; | ||
| 272 | |||
| 273 | # $last_signal is set by the signal handlers and is used | ||
| 274 | # in the term's "Attrib{signal_event_hook}" to determine | ||
| 275 | # what action to take. | ||
| 276 | my $last_signal = 'NONE'; | ||
| 277 | |||
| 278 | # The generic signal handler will attempt to re-throw the signal, after | ||
| 279 | # putting the terminal in the correct state. Any previously set signal | ||
| 280 | # handlers should then be triggered. | ||
| 281 | my $generic_handler = sub { | ||
| 282 | my $signal = shift; | ||
| 283 | |||
| 284 | $last_signal = $signal; | ||
| 285 | |||
| 286 | if (defined $old_sig{$signal} && $old_sig{$signal} ne 'DEFAULT') { | ||
| 287 | $SIG{$signal} = $old_sig{$signal}; | ||
| 288 | } | ||
| 289 | else { | ||
| 290 | $SIG{$signal} = 'DEFAULT'; | ||
| 291 | } | ||
| 292 | |||
| 293 | $self->term->Attribs->{catch_signals} = 0; | ||
| 294 | |||
| 295 | $self->term->free_line_state(); | ||
| 296 | $self->term->cleanup_after_signal(); | ||
| 297 | kill $signal, $$; | ||
| 298 | $self->term->Attribs->{catch_signals} = 1; | ||
| 299 | return 1; | ||
| 300 | }; | ||
| 301 | |||
| 302 | # The WINCH signal handler. | ||
| 303 | # Tell ReadLine to resize the terminal. | ||
| 304 | my $winch_handler = sub { | ||
| 305 | $self->term->resize_terminal; | ||
| 306 | $last_signal = $_[0]; | ||
| 307 | $old_sig{$_[0]}->(@_) if ref $old_sig{$_[0]}; | ||
| 308 | return 1; | ||
| 309 | }; | ||
| 310 | |||
| 311 | # The CONT signal handler. | ||
| 312 | # In case we get suspended, make sure we redraw the CLI on wake-up. | ||
| 313 | my $cont_handler = sub { | ||
| 314 | $last_signal = $_[0]; | ||
| 315 | |||
| 316 | $self->term->free_line_state(); | ||
| 317 | $self->term->cleanup_after_signal(); | ||
| 318 | |||
| 319 | $old_sig{$_[0]}->(@_) if ref $old_sig{$_[0]}; | ||
| 320 | |||
| 321 | $self->term->Attribs->{line_buffer} = ''; | ||
| 322 | $self->term->reset_after_signal(); | ||
| 323 | $self->term->forced_update_display(); | ||
| 324 | return 1; | ||
| 325 | }; | ||
| 326 | |||
| 327 | # Install signal handler(s). | ||
| 328 | my $install_handlers = sub { | ||
| 329 | $self->term->Attribs->{catch_signals} = 1; | ||
| 330 | |||
| 331 | $SIG{WINCH} = $winch_handler; | ||
| 332 | $SIG{CONT} = $cont_handler; | ||
| 333 | $SIG{HUP} | ||
| 334 | = $SIG{INT} | ||
| 335 | = $SIG{QUIT} | ||
| 336 | = $SIG{ALRM} | ||
| 337 | #= $SIG{STOP} | ||
| 338 | = $SIG{TERM} | ||
| 339 | = $SIG{TTIN} | ||
| 340 | = $SIG{TTOU} | ||
| 341 | = $SIG{TSTP} | ||
| 342 | = $generic_handler; | ||
| 343 | |||
| 344 | }; | ||
| 345 | |||
| 346 | $install_handlers->(); | ||
| 347 | |||
| 348 | # Post-signal hook, called by ReadLine. | ||
| 349 | # | ||
| 350 | # Abort the current input line, except when the | ||
| 351 | # WINCH signal was received. | ||
| 352 | # | ||
| 353 | $self->term->Attribs->{signal_event_hook} = sub { | ||
| 354 | return 1 if $last_signal eq 'WINCH'; # Nothing on WINCH. | ||
| 355 | |||
| 356 | # Move to a new line and clear input buffer. | ||
| 357 | $self->term->crlf(); | ||
| 358 | $self->term->Attribs->{line_buffer} = ''; | ||
| 359 | $self->term->forced_update_display(); | ||
| 360 | |||
| 361 | $install_handlers->(); # Re-install handlers, if necessary. | ||
| 362 | $self->term->reset_after_signal(); | ||
| 363 | return 1; | ||
| 364 | }; | ||
| 365 | |||
| 366 | return %old_sig; | ||
| 367 | } | ||
| 368 | |||
| 369 | |||
| 370 | # See POD X<readline> | ||
| 371 | sub readline { | ||
| 372 | my ($self, %args) = @_; | ||
| 373 | |||
| 374 | my $prompt = $args{prompt} // $self->prompt; | ||
| 375 | my $skip = exists $args{skip} ? $args{skip} : $self->skip; | ||
| 376 | |||
| 377 | $self->_set_completion_attribs; | ||
| 378 | my %old_sig = $self->_set_signal_handlers; | ||
| 379 | |||
| 380 | my $input; | ||
| 381 | while (defined ($input = $self->term->readline($prompt))) { | ||
| 382 | next if defined $skip && $input =~ $skip; | ||
| 383 | last; | ||
| 384 | } | ||
| 385 | |||
| 386 | %SIG = %old_sig; # Restore signal handlers. | ||
| 387 | return $input; | ||
| 388 | } | ||
| 389 | |||
| 390 | |||
| 391 | sub read_history { | ||
| 392 | my $self = shift; | ||
| 393 | |||
| 394 | my $hist_file = @_ ? shift @_ : $self->history_file; | ||
| 395 | |||
| 396 | if ($self->term->ReadHistory($hist_file)) { | ||
| 397 | $self->history_file($hist_file); | ||
| 398 | $self->set_error(''); | ||
| 399 | return 1; | ||
| 400 | } | ||
| 401 | else { | ||
| 402 | return $self->set_error("$hist_file: $!"); | ||
| 403 | } | ||
| 404 | } | ||
| 405 | |||
| 406 | |||
| 407 | sub write_history { | ||
| 408 | my $self = shift; | ||
| 409 | |||
| 410 | my $hist_file = @_ ? shift @_ : $self->history_file; | ||
| 411 | |||
| 412 | if ($self->term->WriteHistory($hist_file)) { | ||
| 413 | $self->history_file($hist_file); | ||
| 414 | $self->set_error(''); | ||
| 415 | return 1; | ||
| 416 | } | ||
| 417 | else { | ||
| 418 | return $self->set_error("$hist_file: $!"); | ||
| 419 | } | ||
| 420 | } | ||
| 421 | |||
| 422 | |||
| 423 | sub execute { | ||
| 424 | my ($self, $cmd) = @_; | ||
| 425 | |||
| 426 | my ($error, @cmd) = $self->_split_line($cmd); | ||
| 427 | |||
| 428 | my %args = ( | ||
| 429 | status => 0, | ||
| 430 | error => '', | ||
| 431 | command_line => $cmd, | ||
| 432 | command_path => [$self], | ||
| 433 | unparsed => \@cmd, | ||
| 434 | options => {}, | ||
| 435 | arguments => [], | ||
| 436 | ); | ||
| 437 | |||
| 438 | return $self->try_callback(%args, status => -1, error => $error) | ||
| 439 | if length $error; | ||
| 440 | |||
| 441 | if (@cmd == 0) { | ||
| 442 | $args{error} = loc("missing command"); | ||
| 443 | $args{status} = -1; | ||
| 444 | } | ||
| 445 | elsif (my $cmd_ref = $self->find_command($cmd[0])) { | ||
| 446 | %args = $cmd_ref->execute(%args, | ||
| 447 | unparsed => [@cmd[1..$#cmd]] | ||
| 448 | ); | ||
| 449 | } | ||
| 450 | else { | ||
| 451 | $args{error} = $self->error; | ||
| 452 | $args{status} = -1; | ||
| 453 | } | ||
| 454 | |||
| 455 | return $self->try_callback(%args); | ||
| 456 | } | ||
| 457 | |||
| 458 | } | ||
| 459 | |||
| 460 | 1; | ||
| 461 | |||
| 462 | __END__ | ||