| 1 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | package Term::ReadLine::Repl; | ||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | our $VERSION = '0.0.1'; | |||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | Term::ReadLine::Repl - A batteries included interactive Term::ReadLine REPL module | ||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | use Term::ReadLine::Repl; | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | # A simple repl | ||||||||||||||||||||||||||||||||||||||||||
| 17 | my $repl = Term::ReadLine::Repl->new( | ||||||||||||||||||||||||||||||||||||||||||
| 18 | { | ||||||||||||||||||||||||||||||||||||||||||
| 19 | name => 'myrepl', | ||||||||||||||||||||||||||||||||||||||||||
| 20 | cmd_schema => { | ||||||||||||||||||||||||||||||||||||||||||
| 21 | ls => { | ||||||||||||||||||||||||||||||||||||||||||
| 22 | exec => sub { my @list = qw(a b c); print for @list }, | ||||||||||||||||||||||||||||||||||||||||||
| 23 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 24 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | # A complete repl | ||||||||||||||||||||||||||||||||||||||||||
| 29 | $repl = Term::ReadLine::Repl->new( | ||||||||||||||||||||||||||||||||||||||||||
| 30 | { | ||||||||||||||||||||||||||||||||||||||||||
| 31 | name => 'myrepl', | ||||||||||||||||||||||||||||||||||||||||||
| 32 | prompt => '(%s)>', | ||||||||||||||||||||||||||||||||||||||||||
| 33 | cmd_schema => { | ||||||||||||||||||||||||||||||||||||||||||
| 34 | stats => { | ||||||||||||||||||||||||||||||||||||||||||
| 35 | exec => \&get_stats, | ||||||||||||||||||||||||||||||||||||||||||
| 36 | args => [ | ||||||||||||||||||||||||||||||||||||||||||
| 37 | { | ||||||||||||||||||||||||||||||||||||||||||
| 38 | refresh => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 39 | host => 'hostname', | ||||||||||||||||||||||||||||||||||||||||||
| 40 | guest => 'guestname', | ||||||||||||||||||||||||||||||||||||||||||
| 41 | list => 'host|guest', | ||||||||||||||||||||||||||||||||||||||||||
| 42 | cluster => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||
| 44 | ], | ||||||||||||||||||||||||||||||||||||||||||
| 45 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 46 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 47 | passthrough => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 48 | hist_file => '/path/to/.hist_file', | ||||||||||||||||||||||||||||||||||||||||||
| 49 | get_opts => \&arg_parse, | ||||||||||||||||||||||||||||||||||||||||||
| 50 | custom_logic => \&my_custom_loop_ctrl, | ||||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||
| 52 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | $repl->run(); | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | C<Term::ReadLine::Repl> provides a simple framework for building interactive | ||||||||||||||||||||||||||||||||||||||||||
| 59 | command-line REPLs (Read-Eval-Print Loops) on top of L<Term::ReadLine>. It | ||||||||||||||||||||||||||||||||||||||||||
| 60 | handles tab completion, command history, a built-in help system, and optional | ||||||||||||||||||||||||||||||||||||||||||
| 61 | passthrough to shell commands, so you can focus on defining your commands | ||||||||||||||||||||||||||||||||||||||||||
| 62 | rather than plumbing the terminal interaction. | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | =head2 Overview | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | You define your commands and their arguments via the C<cmd_schema> hashref | ||||||||||||||||||||||||||||||||||||||||||
| 67 | passed to C<new()>. Each command maps to an C<exec> coderef that is called | ||||||||||||||||||||||||||||||||||||||||||
| 68 | when the user types that command, and an optional C<args> structure that drives | ||||||||||||||||||||||||||||||||||||||||||
| 69 | tab completion. Once constructed, calling C<run()> drops the user into an | ||||||||||||||||||||||||||||||||||||||||||
| 70 | interactive prompt. | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | The module handles the following automatically: | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | =item * | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | B<Tab completion> - command names and their arguments are completed from the | ||||||||||||||||||||||||||||||||||||||||||
| 79 | C<cmd_schema> definition. Passthrough commands (prefixed with C<!>) are | ||||||||||||||||||||||||||||||||||||||||||
| 80 | excluded. | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | =item * | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | B<Command history> - input history is maintained in-session via | ||||||||||||||||||||||||||||||||||||||||||
| 85 | L<Term::ReadLine>, and can be persisted across sessions by supplying a | ||||||||||||||||||||||||||||||||||||||||||
| 86 | C<hist_file> path. | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | =item * | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | B<Built-in commands> - C<help> and C<quit>/C<exit> are injected automatically | ||||||||||||||||||||||||||||||||||||||||||
| 91 | into every REPL. | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | =item * | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | B<Shell passthrough> - when C<passthrough> is enabled, any input prefixed with | ||||||||||||||||||||||||||||||||||||||||||
| 96 | C<!> is forwarded directly to the system shell, making it easy to run one-off | ||||||||||||||||||||||||||||||||||||||||||
| 97 | shell commands without leaving the REPL. | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | =item * | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | B<Custom loop hooks> - the C<get_opts> and C<custom_logic> callbacks let you | ||||||||||||||||||||||||||||||||||||||||||
| 102 | plug L<Getopt::Long> parsing and arbitrary mid-loop logic into the REPL without | ||||||||||||||||||||||||||||||||||||||||||
| 103 | having to subclass or modify the module. | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | =head1 CONSTRUCTOR | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | =item C<new(\%args)> | ||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | Creates and returns a new C<Term::ReadLine::Repl> object. Accepts a hashref | ||||||||||||||||||||||||||||||||||||||||||
| 114 | with the following keys: | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | =item C<name> (required) | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | A string used as the name of the REPL, displayed in the welcome message and | ||||||||||||||||||||||||||||||||||||||||||
| 121 | optionally interpolated into the prompt via C<%s>. | ||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | =item C<cmd_schema> (required) | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | A hashref defining the available commands. Each key is a command name, and | ||||||||||||||||||||||||||||||||||||||||||
| 126 | its value is a hashref with the following keys: | ||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | =item C<exec> (required) | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | A coderef that is called when the command is invoked. Any arguments supplied | ||||||||||||||||||||||||||||||||||||||||||
| 133 | on the command line (after the command name) are passed to the coderef. | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | =item C<args> (optional) | ||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | An arrayref of hashrefs describing the command's arguments for tab completion. | ||||||||||||||||||||||||||||||||||||||||||
| 138 | Each hashref maps an argument name to either C<undef> (flag, no value expected) | ||||||||||||||||||||||||||||||||||||||||||
| 139 | or a string describing the expected value (used as a completion hint). | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | =item C<prompt> (optional) | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | A C<sprintf>-style format string for the prompt. C<%s> is replaced with the | ||||||||||||||||||||||||||||||||||||||||||
| 146 | REPL name. Defaults to C<(repl)>>. | ||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | =item C<passthrough> (optional) | ||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | When set to a true value, any input beginning with C<!> is passed directly to | ||||||||||||||||||||||||||||||||||||||||||
| 151 | the system shell. For example, C<!ls -la> would run C<ls -la>. Defaults to C<0>. | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | =item C<hist_file> (optional) | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | Path to a file used for persistent command history. History is loaded on | ||||||||||||||||||||||||||||||||||||||||||
| 156 | startup and saved on exit. If not specified, history is not persisted. | ||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | =item C<get_opts> (optional) | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | A coderef to a L<Getopt::Long> parsing function. When provided, it is called | ||||||||||||||||||||||||||||||||||||||||||
| 161 | before each command dispatch with C<@ARGV> populated from the current input line. | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | =item C<custom_logic> (optional) | ||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | A coderef invoked on each loop iteration before command dispatch. Receives an | ||||||||||||||||||||||||||||||||||||||||||
| 166 | arrayref of the parsed input tokens. May return a hashref with the following | ||||||||||||||||||||||||||||||||||||||||||
| 167 | optional keys: | ||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | =item C<action> | ||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | Set to C<'next'> to skip to the next loop iteration, or C<'last'> to exit | ||||||||||||||||||||||||||||||||||||||||||
| 174 | the REPL loop. | ||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | =item C<schema> | ||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | A replacement C<cmd_schema> hashref to swap in for subsequent iterations. | ||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | =item C<run()> | ||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | Launches the interactive REPL session. Prints a welcome message, then enters | ||||||||||||||||||||||||||||||||||||||||||
| 193 | the read-eval-print loop until the user types C<quit>, C<exit>, or C<EOF>. | ||||||||||||||||||||||||||||||||||||||||||
| 194 | Saves history on exit if C<hist_file> was configured. | ||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | =item C<validate_args(\%args)> | ||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | Validates the constructor argument hashref. Croaks with a descriptive message | ||||||||||||||||||||||||||||||||||||||||||
| 199 | if any required arguments are missing or if any values have an unexpected type. | ||||||||||||||||||||||||||||||||||||||||||
| 200 | Called automatically by C<new()>. | ||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | =head1 BUILT-IN COMMANDS | ||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | The following commands are automatically added to every REPL: | ||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||
| 208 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||
| 210 | =item C<help> | ||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||
| 212 | Prints all available commands and their arguments. | ||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||
| 214 | =item C<quit> / C<exit> | ||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||
| 216 | Exits the REPL session. | ||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||
| 218 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||
| 220 | =head1 TAB COMPLETION | ||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||
| 222 | Tab completion is provided automatically for command names and their defined | ||||||||||||||||||||||||||||||||||||||||||
| 223 | arguments. Completions are driven by the C<args> key in each command's schema. | ||||||||||||||||||||||||||||||||||||||||||
| 224 | Passthrough commands (those beginning with C<!>) are excluded from completion. | ||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||
| 226 | =head1 AUTHORS | ||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||
| 228 | Written by John R. Copyright (c) 2026 | ||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||
| 230 | =head1 LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||
| 232 | This library is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||
| 233 | the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||
| 235 | See L<http://dev.perl.org/licenses/> for more information. | ||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||
| 237 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 240 | 1 | ▶ | use Term::ANSIColor; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 241 | 1 | ▶ | use Term::ReadLine; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 242 | 1 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||
| 244 | sub new { | ||||||||||||||||||||||||||||||||||||||||||
| 245 | 16 | ▶ | my ($class, $args) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||
| 247 | 16 | $class->validate_args($args); | |||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||
| 249 | my $self = { | ||||||||||||||||||||||||||||||||||||||||||
| 250 | name => $args->{name} // 'repl', | ||||||||||||||||||||||||||||||||||||||||||
| 251 | prompt => defined $args->{prompt} ? sprintf $args->{prompt}, $args->{name} : '(repl)>', | ||||||||||||||||||||||||||||||||||||||||||
| 252 | cmd_schema => $args->{cmd_schema}, | ||||||||||||||||||||||||||||||||||||||||||
| 253 | passthrough => $args->{passthrough} // 0, | ||||||||||||||||||||||||||||||||||||||||||
| 254 | hist_file => $args->{hist_file}, | ||||||||||||||||||||||||||||||||||||||||||
| 255 | get_opts => $args->{get_opts}, | ||||||||||||||||||||||||||||||||||||||||||
| 256 | custom_logic => $args->{custom_logic}, | ||||||||||||||||||||||||||||||||||||||||||
| 257 | 7 | ▶ | }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%2/4
$args->{"name"} // "repl"
$args->{"passthrough"} // 0
MC/DC0%0/1
$args->{"name"} // "repl"
$args->{"name"}
MC/DC0%0/1
$args->{"passthrough"} // 0
$args->{"passthrough"}
Truth table50%1/2
A
A= $args->{"name"}
Truth table50%1/2
A
A= $args->{"passthrough"}
| |||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||
| 259 | # Add builtin commands. | ||||||||||||||||||||||||||||||||||||||||||
| 260 | 7 | $self->{cmd_schema}{help}={}; | |||||||||||||||||||||||||||||||||||||||||
| 261 | 7 | $self->{cmd_schema}{quit}={}; | |||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||
| 263 | 7 | bless $self, $class; | |||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||
| 265 | 7 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||
| 268 | sub validate_args { | ||||||||||||||||||||||||||||||||||||||||||
| 269 | 16 | ▶ | my ($self, $args) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
validate_args: called CC=18 SCAR=29.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||
| 271 | # Ensure name and cmd_schema exist (required args) | ||||||||||||||||||||||||||||||||||||||||||
| 272 | 16 | ▶ | croak "name is a required arg!" unless exists $args->{name} && defined $args->{name}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $args->{"name"} and defined $args->{"name"}
MC/DC50%1/2
exists $args->{"name"} and defined $args->{"name"}
exists $args->{"name"}
defined $args->{"name"}
Truth table66%2/3
A and B
A= exists $args->{"name"}B= defined $args->{"name"}
| |||||||||||||||||||||||||||||||||||||||||||
| 273 | 15 | ▶ | croak "cmd_schema is a required arg!" unless exists $args->{cmd_schema} && defined $args->{cmd_schema}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $args->{"cmd_schema"} and defined $args->{"cmd_schema"}
MC/DC50%1/2
exists $args->{"cmd_schema"} and defined $args->{"cmd_schema"}
exists $args->{"cmd_schema"}
defined $args->{"cmd_schema"}
Truth table66%2/3
A and B
A= exists $args->{"cmd_schema"}B= defined $args->{"cmd_schema"}
| |||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||
| 275 | # Ensure cmd_schema is a hashref | ||||||||||||||||||||||||||||||||||||||||||
| 276 | 14 | ▶ | croak "cmd_schema is NOT a hashref!" unless ref $args->{cmd_schema} eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||
| 278 | # Ensure each cmd has an exec key and is a coderef | ||||||||||||||||||||||||||||||||||||||||||
| 279 | 13 | for my $cmd (keys %{$args->{cmd_schema}}) { | |||||||||||||||||||||||||||||||||||||||||
| 280 | 14 | my $schema = $args->{cmd_schema}{$cmd}; | |||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||
| 282 | 14 | ▶ | croak "'$cmd' missing exec key!" unless defined $schema->{exec}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||
| 284 | 13 | ▶ | croak "'$cmd' exec is NOT a coderef!" unless ref $schema->{exec} eq 'CODE'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||
| 286 | # Ensure that args is an array | ||||||||||||||||||||||||||||||||||||||||||
| 287 | 12 | ▶ | if (exists $schema->{args} && defined $schema->{args}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $schema->{"args"} and defined $schema->{"args"}
MC/DC50%1/2
exists $schema->{"args"} and defined $schema->{"args"}
exists $schema->{"args"}
defined $schema->{"args"}
Truth table66%2/3
A and B
A= exists $schema->{"args"}B= defined $schema->{"args"}
| |||||||||||||||||||||||||||||||||||||||||||
| 288 | 4 | ▶ | croak "'$cmd' args is NOT a arrayref!" unless ref $schema->{args}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||
| 290 | 3 | ▶ | croak "'$cmd' args array is empty!" if scalar @{$schema->{args}} < 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||
| 292 | 2 | for my $arg (@{$schema->{args}}) { | |||||||||||||||||||||||||||||||||||||||||
| 293 | 2 | ▶ | croak "'$cmd' non-hashref found in args arrayref!" unless ref $arg eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||
| 298 | # Ensure get_ops is a coderef if present | ||||||||||||||||||||||||||||||||||||||||||
| 299 | 8 | ▶ | if (exists $args->{get_opts} && defined $args->{get_opts}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $args->{"get_opts"} and defined $args->{"get_opts"}
MC/DC50%1/2
exists $args->{"get_opts"} and defined $args->{"get_opts"}
exists $args->{"get_opts"}
defined $args->{"get_opts"}
Truth table66%2/3
A and B
A= exists $args->{"get_opts"}B= defined $args->{"get_opts"}
| |||||||||||||||||||||||||||||||||||||||||||
| 300 | 2 | ▶ | croak "get_opts is NOT a coderef!" unless ref $args->{get_opts} eq 'CODE'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||
| 304 | sub run { | ||||||||||||||||||||||||||||||||||||||||||
| 305 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run: not called CC=19 SCAR=59.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||
| 307 | 0 | my $term = Term::ReadLine->new('Simple Shell'); | |||||||||||||||||||||||||||||||||||||||||
| 308 | 0 | my $attribs = $term->Attribs; | |||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||
| 310 | 0 | ▶ | $self->_read_history($term) if defined $self->{hist_file}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||
| 312 | 0 | print colored(sprintf("Welcome to $self->{name} shell!"), 'green underline italic bold'), "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 313 | 0 | print colored(sprintf("Type 'help' for more options, <TAB> to auto complete."), 'green bold'), "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||
| 315 | # Tab completion. | ||||||||||||||||||||||||||||||||||||||||||
| 316 | 0 | ▶ | $attribs->{completion_function} = sub { return $self->_tab_complete(@_) }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 317 | 0 | my $prompt = colored(sprintf("$self->{prompt} "), 'green'); | |||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||
| 319 | 0 | $|++; | |||||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||||||
| 321 | # Simple REPL loop. | ||||||||||||||||||||||||||||||||||||||||||
| 322 | 0 | ▶ | while (defined (my $input = $term->readline($prompt))) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 323 | 0 | chomp $input; | |||||||||||||||||||||||||||||||||||||||||
| 324 | 0 | ▶ | last if ($input =~ /^(exit|quit)$/); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||
| 326 | 0 | ▶ | next unless $input; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||
| 328 | 0 | ▶ | if ($input =~ 'help') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 329 | 0 | $self->_help(); | |||||||||||||||||||||||||||||||||||||||||
| 330 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||
| 333 | 0 | my @args = split(/\s+/, $input); | |||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||
| 335 | # Command line passthrough. | ||||||||||||||||||||||||||||||||||||||||||
| 336 | 0 | ▶ | if ($self->{passthrough} && @args && $args[0] =~ /^\!/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$self->{"passthrough"} and @args and $args[0] =~ /^\!/
$self->{"passthrough"} and @args
MC/DC0%0/3
$self->{"passthrough"} and @args and $args[0] =~ /^\!/
$self->{"passthrough"}
@args
$args[0] =~ /^\!/
Truth table0%0/4
A and B and C
A= $self->{"passthrough"}B= @argsC= $args[0] =~ /^\!/
| |||||||||||||||||||||||||||||||||||||||||||
| 337 | 0 | $args[0] =~ s/\!//g; | |||||||||||||||||||||||||||||||||||||||||
| 338 | 0 | system(@args); | |||||||||||||||||||||||||||||||||||||||||
| 339 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||||
| 342 | 0 | ▶ | if (defined $self->{get_opts}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 343 | # Clobber ARGV for getopts parsing, doesn't matter because client | ||||||||||||||||||||||||||||||||||||||||||
| 344 | # code parser will slurp args outta @ARGV again right away. | ||||||||||||||||||||||||||||||||||||||||||
| 345 | 0 | @ARGV = @args; | |||||||||||||||||||||||||||||||||||||||||
| 346 | 0 | $self->{get_opts}->(); | |||||||||||||||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||||||||
| 349 | # Custom loop logic. | ||||||||||||||||||||||||||||||||||||||||||
| 350 | # User custom function can return a hashref like the following. | ||||||||||||||||||||||||||||||||||||||||||
| 351 | # { | ||||||||||||||||||||||||||||||||||||||||||
| 352 | # action => next|last|undef, | ||||||||||||||||||||||||||||||||||||||||||
| 353 | # schema => $schema, # Where schema is a hashref containing any changes your custom logic might make to cmd_schema. | ||||||||||||||||||||||||||||||||||||||||||
| 354 | # } | ||||||||||||||||||||||||||||||||||||||||||
| 355 | 0 | ▶ | if (defined $self->{custom_logic}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 356 | 0 | my $result = eval { | |||||||||||||||||||||||||||||||||||||||||
| 357 | 0 | $self->{custom_logic}->(\@args); | |||||||||||||||||||||||||||||||||||||||||
| 358 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||
| 360 | 0 | ▶ | if (defined $result && ref $result eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $result and ref $result eq "HASH"
MC/DC0%0/2
defined $result and ref $result eq "HASH"
defined $result
ref $result eq "HASH"
Truth table0%0/3
A and B
A= defined $resultB= ref $result eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||
| 361 | 0 | ▶ | if (defined $result->{action}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 362 | 0 | ▶ | next if $result->{action} eq 'next'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 363 | 0 | ▶ | last if $result->{action} eq 'last'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 364 | } | ||||||||||||||||||||||||||||||||||||||||||
| 365 | 0 | ▶ | if (defined $result->{schema}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 366 | 0 | $self->{cmd_schema} = $result->{schema}; | |||||||||||||||||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||||||||||||||||
| 369 | } | ||||||||||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||||||||||
| 371 | 0 | my $cmd = shift @args; | |||||||||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||||||||||
| 373 | 0 | ▶ | if (exists $self->{cmd_schema}{$cmd}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 374 | 0 | $self->{cmd_schema}{$cmd}{exec}->(@args); | |||||||||||||||||||||||||||||||||||||||||
| 375 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 376 | 0 | print "No such command '$cmd' run 'help' to see options\n"; | |||||||||||||||||||||||||||||||||||||||||
| 377 | } | ||||||||||||||||||||||||||||||||||||||||||
| 378 | } | ||||||||||||||||||||||||||||||||||||||||||
| 379 | 0 | print "\n" . colored(sprintf("Goodbye!"), 'green bold underline italic'), "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 380 | 0 | ▶ | $self->_save_history($term) if defined $self->{hist_file}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 381 | } | ||||||||||||||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||||||||||||||
| 383 | sub _tab_complete { | ||||||||||||||||||||||||||||||||||||||||||
| 384 | 5 | ▶ | my ($self, $text, $line) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_tab_complete: called CC=17 SCAR=29.6
| |||||||||||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||||||||||
| 386 | # Don't auto complete on passthroughs. | ||||||||||||||||||||||||||||||||||||||||||
| 387 | 5 | ▶ | return () if $line =~ /^\!/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||||||||
| 389 | # Split the current line into words. | ||||||||||||||||||||||||||||||||||||||||||
| 390 | 4 | my @words = split(/\s+/, $line); | |||||||||||||||||||||||||||||||||||||||||
| 391 | 4 | my @complete_words = @words; | |||||||||||||||||||||||||||||||||||||||||
| 392 | 4 | ▶ | pop @complete_words unless $line =~ /\s$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||||||||||||
| 394 | 4 | ▶ | if (@words >= 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 395 | 4 | my $cmd = $words[0]; | |||||||||||||||||||||||||||||||||||||||||
| 396 | 4 | my $arg_index = (scalar(@complete_words) - 1); # -1 because first word is always $cmd | |||||||||||||||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||||||||||||||||
| 398 | 4 | ▶ | if ($self->{cmd_schema}{$cmd}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 399 | 2 | my $schema = $self->{cmd_schema}{$cmd}; | |||||||||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||||||||||
| 401 | # None of the below make sense unless we have args. | ||||||||||||||||||||||||||||||||||||||||||
| 402 | 2 | ▶ | return () unless $schema->{args}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||||||
| 404 | 2 | my $opt_arg_index = $arg_index -1; | |||||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||||||
| 406 | # If next word matches args key, go into optargs | ||||||||||||||||||||||||||||||||||||||||||
| 407 | 2 | ▶ | if (scalar @complete_words && exists $schema->{args}[$opt_arg_index]{$complete_words[-1]}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
scalar @complete_words and exists $schema->{"args"}[$opt_arg_index]{$complete_words[-1]}
MC/DC0%0/2
scalar @complete_words and exists $schema->{"args"}[$opt_arg_index]{$complete_words[-1]}
scalar @complete_words
exists $schema->{"args"}[$opt_arg_index]{$complete_words[-1]}
Truth table33%1/3
A and B
A= scalar @complete_wordsB= exists $schema->{"args"}[$opt_arg_index]{$complete_words[-1]}
| |||||||||||||||||||||||||||||||||||||||||||
| 408 | 0 | my $opt_arg = $schema->{args}[$opt_arg_index]{$complete_words[-1]}; | |||||||||||||||||||||||||||||||||||||||||
| 409 | 0 | ▶ | return "<$opt_arg>" if defined $opt_arg; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 410 | } | ||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||
| 412 | # Count number of opt args in command to subtract from $arg_index. | ||||||||||||||||||||||||||||||||||||||||||
| 413 | 2 | my $num_opt_args=0; | |||||||||||||||||||||||||||||||||||||||||
| 414 | 2 | my @all_opt_args; | |||||||||||||||||||||||||||||||||||||||||
| 415 | 2 | for my $arg (@{$schema->{args}}) { | |||||||||||||||||||||||||||||||||||||||||
| 416 | 2 | for my $key (keys %{$arg}) { | |||||||||||||||||||||||||||||||||||||||||
| 417 | 6 | my $value = $arg->{$key}; | |||||||||||||||||||||||||||||||||||||||||
| 418 | 6 | ▶ | push @all_opt_args, $key if defined $value; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||||||||||||||||
| 420 | } | ||||||||||||||||||||||||||||||||||||||||||
| 421 | 2 | for my $word (@complete_words) { | |||||||||||||||||||||||||||||||||||||||||
| 422 | 2 | for my $opt_arg (@all_opt_args) { | |||||||||||||||||||||||||||||||||||||||||
| 423 | 4 | ▶ | $num_opt_args++ if ($word eq $opt_arg); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 424 | } | ||||||||||||||||||||||||||||||||||||||||||
| 425 | } | ||||||||||||||||||||||||||||||||||||||||||
| 426 | 2 | $arg_index = $arg_index - $num_opt_args; | |||||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||||||
| 428 | 2 | my $args = @{$schema->{args}}[$arg_index]; | |||||||||||||||||||||||||||||||||||||||||
| 429 | 2 | my @keys = keys %{$args}; | |||||||||||||||||||||||||||||||||||||||||
| 430 | 2 | ▶ | return () unless @keys; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 431 | 2 | return grep { /^\Q$text/ } @keys; | |||||||||||||||||||||||||||||||||||||||||
| 432 | } | ||||||||||||||||||||||||||||||||||||||||||
| 433 | } | ||||||||||||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||||||||||||
| 435 | # If we're completing the first word | ||||||||||||||||||||||||||||||||||||||||||
| 436 | 2 | ▶ | if (@words <= 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 437 | 2 | my $cmd = $words[0]; | |||||||||||||||||||||||||||||||||||||||||
| 438 | 2 | my @cmds = keys %{$self->{cmd_schema}}; | |||||||||||||||||||||||||||||||||||||||||
| 439 | 2 | return grep { /^\Q$text/ } @cmds; | |||||||||||||||||||||||||||||||||||||||||
| 440 | } | ||||||||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||||||||
| 442 | # No completion For anything beyond second word. | ||||||||||||||||||||||||||||||||||||||||||
| 443 | 0 | return (); | |||||||||||||||||||||||||||||||||||||||||
| 444 | } | ||||||||||||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||||||||||
| 447 | sub _help { | ||||||||||||||||||||||||||||||||||||||||||
| 448 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_help: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||||||||||||
| 450 | 0 | my $output; | |||||||||||||||||||||||||||||||||||||||||
| 451 | 0 | for my $cmd (keys %{$self->{cmd_schema}}) { | |||||||||||||||||||||||||||||||||||||||||
| 452 | 0 | $output .= "$cmd\n"; | |||||||||||||||||||||||||||||||||||||||||
| 453 | 0 | ▶ | next unless $self->{cmd_schema}{$cmd}{args}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 454 | 0 | for my $args (sort @{$self->{cmd_schema}{$cmd}{args}} ) { | |||||||||||||||||||||||||||||||||||||||||
| 455 | 0 | $output .= " "; | |||||||||||||||||||||||||||||||||||||||||
| 456 | 0 | for my $arg (keys %{$args}) { | |||||||||||||||||||||||||||||||||||||||||
| 457 | 0 | my $opt = $args->{$arg}; | |||||||||||||||||||||||||||||||||||||||||
| 458 | 0 | $output .= "$arg"; | |||||||||||||||||||||||||||||||||||||||||
| 459 | 0 | ▶ | $output .= defined $opt ? "=<$opt>, " : ", "; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 460 | } | ||||||||||||||||||||||||||||||||||||||||||
| 461 | 0 | substr($output, -1) = ""; # Remove trailing space | |||||||||||||||||||||||||||||||||||||||||
| 462 | 0 | substr($output, -1) = ""; # Remove trailing , | |||||||||||||||||||||||||||||||||||||||||
| 463 | 0 | $output .= "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 464 | } | ||||||||||||||||||||||||||||||||||||||||||
| 465 | } | ||||||||||||||||||||||||||||||||||||||||||
| 466 | 0 | print "$output"; | |||||||||||||||||||||||||||||||||||||||||
| 467 | } | ||||||||||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||||||||||
| 469 | sub _read_history { | ||||||||||||||||||||||||||||||||||||||||||
| 470 | 0 | ▶ | my ($self, $term) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_read_history: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||||||||||||||||
| 472 | 0 | ▶ | if (-f $self->{hist_file}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 473 | 0 | ▶ | open my $fh, '<', $self->{hist_file} or warn "Couldn't read auto bal history file: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 474 | 0 | ▶ | while (my $line = <$fh>) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 475 | 0 | chomp $line; | |||||||||||||||||||||||||||||||||||||||||
| 476 | 0 | $term->addhistory($line); | |||||||||||||||||||||||||||||||||||||||||
| 477 | } | ||||||||||||||||||||||||||||||||||||||||||
| 478 | 0 | close $fh; | |||||||||||||||||||||||||||||||||||||||||
| 479 | } | ||||||||||||||||||||||||||||||||||||||||||
| 480 | } | ||||||||||||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||||||||||||
| 482 | sub _save_history { | ||||||||||||||||||||||||||||||||||||||||||
| 483 | 0 | ▶ | my ($self, $term) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_save_history: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||||||||||||||||
| 485 | 0 | my $attribs = $term->Attribs; | |||||||||||||||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||||||||||||||||
| 487 | 0 | ▶ | open my $fh, '>>', $self->{hist_file} or warn "Couldn't save auto bal history: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 488 | 0 | ▶ | if ($term->ReadLine =~ /Gnu/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 489 | 0 | for my $line ($term->GetHistory) { | |||||||||||||||||||||||||||||||||||||||||
| 490 | 0 | print $fh "$line\n"; | |||||||||||||||||||||||||||||||||||||||||
| 491 | } | ||||||||||||||||||||||||||||||||||||||||||
| 492 | } | ||||||||||||||||||||||||||||||||||||||||||
| 493 | 0 | close $fh; | |||||||||||||||||||||||||||||||||||||||||
| 494 | } | ||||||||||||||||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||||||||||||||||
| 496 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||