| 1 | # ABSTRACT: Shell Completion generator for fish | ||||||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | package App::Spec::Completion::Fish; | ||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | 2 | our $VERSION = 'v0.15.1'; # TRIAL VERSION | |||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Moo; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 2 | extends 'App::Spec::Completion'; | |||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | # prefix for generated helper functions, e.g. __myapp or __lwp_request | ||||||||||||||||||||||||||||||||
| 12 | 2 | has prefix => ( is => 'lazy' ); | |||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | 5 | ▶ | sub _build_prefix { | ||||||||||||||||||||||||||||||
|
Subroutine
_build_prefix: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 15 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 16 | 5 | return '__' . _ident($self->spec->name); | |||||||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||
| 19 | 5 | ▶ | sub generate_completion { | ||||||||||||||||||||||||||||||
|
Subroutine
generate_completion: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 20 | 5 | my ($self, %args) = @_; | |||||||||||||||||||||||||||||||
| 21 | 5 | my $spec = $self->spec; | |||||||||||||||||||||||||||||||
| 22 | 5 | my $appname = $spec->name; | |||||||||||||||||||||||||||||||
| 23 | 5 | my $p = $self->prefix; | |||||||||||||||||||||||||||||||
| 24 | 5 | my $appspec_version = App::Spec->VERSION; | |||||||||||||||||||||||||||||||
| 25 | 5 | my %state = ( | |||||||||||||||||||||||||||||||
| 26 | functions => [], # generated functions for command completions | ||||||||||||||||||||||||||||||||
| 27 | optspecs => {}, # subcommand path => { option name => takes value } | ||||||||||||||||||||||||||||||||
| 28 | parents => [], # subcommand paths which have subcommands | ||||||||||||||||||||||||||||||||
| 29 | dynamic => 0, # true if ${p}_dynamic is needed | ||||||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||||||
| 31 | 5 | my $lines = $self->completion_commands( | |||||||||||||||||||||||||||||||
| 32 | commands => $spec->subcommands, | ||||||||||||||||||||||||||||||||
| 33 | options => $spec->options, | ||||||||||||||||||||||||||||||||
| 34 | parameters => $spec->parameters, | ||||||||||||||||||||||||||||||||
| 35 | previous => [], | ||||||||||||||||||||||||||||||||
| 36 | state => \%state, | ||||||||||||||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||||||||||||||
| 38 | 5 | my $optspecs = _optspecs_cases($state{optspecs}); | |||||||||||||||||||||||||||||||
| 39 | 5 | ▶ | my $parents = join ' ', map { length($_) ? _quote($_) : "''" } @{ $state{parents} }; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 40 | 5 | my $dynamic = ''; | |||||||||||||||||||||||||||||||
| 41 | 5 | ▶ | if ($state{dynamic}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 42 | 1 | $dynamic = <<"EOM"; | |||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | # dynamic completion via App::Spec::Run, prints "value<TAB>description" lines | ||||||||||||||||||||||||||||||||
| 45 | function ${p}_dynamic | ||||||||||||||||||||||||||||||||
| 46 | set -l cmd (commandline -xpc) (commandline -ct) | ||||||||||||||||||||||||||||||||
| 47 | PERL5_APPSPECRUN_SHELL=fish PERL5_APPSPECRUN_COMPLETION_PARAMETER=\$argv[1] \$cmd | ||||||||||||||||||||||||||||||||
| 48 | end | ||||||||||||||||||||||||||||||||
| 49 | EOM | ||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | 5 | return <<"EOM"; | |||||||||||||||||||||||||||||||
| 53 | # fish completion for $appname | ||||||||||||||||||||||||||||||||
| 54 | # Generated with perl module App::Spec v$appspec_version | ||||||||||||||||||||||||||||||||
| 55 | # Requires fish >= 4.0 (uses `commandline -x`) | ||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | if string match -qr '^[0-3]\\.' -- \$version | ||||||||||||||||||||||||||||||||
| 58 | echo "$appname completion requires fish >= 4.0 (running \$version)" >&2 | ||||||||||||||||||||||||||||||||
| 59 | return 1 | ||||||||||||||||||||||||||||||||
| 60 | end | ||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | # ${p}_optspecs PATH...: argparse optspecs of the options declared by PATH | ||||||||||||||||||||||||||||||||
| 63 | function ${p}_optspecs | ||||||||||||||||||||||||||||||||
| 64 | switch "\$argv" | ||||||||||||||||||||||||||||||||
| 65 | $optspecs | ||||||||||||||||||||||||||||||||
| 66 | end | ||||||||||||||||||||||||||||||||
| 67 | end | ||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | # positional words after the program name, options and their values removed | ||||||||||||||||||||||||||||||||
| 70 | # like App::Spec::Run: remove the options of the current subcommand level, | ||||||||||||||||||||||||||||||||
| 71 | # then the next word is the subcommand of the next level | ||||||||||||||||||||||||||||||||
| 72 | function ${p}_words | ||||||||||||||||||||||||||||||||
| 73 | set -l rest (commandline -xpc) | ||||||||||||||||||||||||||||||||
| 74 | set -e rest[1] | ||||||||||||||||||||||||||||||||
| 75 | set -l words | ||||||||||||||||||||||||||||||||
| 76 | # -S (fish >= 4.1): no abbreviated long options, otherwise an unknown -e | ||||||||||||||||||||||||||||||||
| 77 | # is taken for e.g. --exists-action; fish 4.0 has no way to turn this off | ||||||||||||||||||||||||||||||||
| 78 | set -l strict -S | ||||||||||||||||||||||||||||||||
| 79 | string match -q '4.0.*' -- \$version; and set strict | ||||||||||||||||||||||||||||||||
| 80 | while true | ||||||||||||||||||||||||||||||||
| 81 | set -l specs (${p}_optspecs \$words) | ||||||||||||||||||||||||||||||||
| 82 | # a trailing option still waiting for its value makes argparse fail | ||||||||||||||||||||||||||||||||
| 83 | argparse \$strict -i \$specs -- \$rest 2>/dev/null | ||||||||||||||||||||||||||||||||
| 84 | or argparse \$strict -i \$specs -- \$rest[1..-2] 2>/dev/null | ||||||||||||||||||||||||||||||||
| 85 | or return | ||||||||||||||||||||||||||||||||
| 86 | set -l positional (string match -v -- '-*' \$argv) | ||||||||||||||||||||||||||||||||
| 87 | if not contains -- "\$words" $parents; or not set -q positional[1] | ||||||||||||||||||||||||||||||||
| 88 | set -a words \$positional | ||||||||||||||||||||||||||||||||
| 89 | break | ||||||||||||||||||||||||||||||||
| 90 | end | ||||||||||||||||||||||||||||||||
| 91 | set -a words \$positional[1] | ||||||||||||||||||||||||||||||||
| 92 | set -e argv[(contains -i -- \$positional[1] \$argv)] | ||||||||||||||||||||||||||||||||
| 93 | set rest \$argv | ||||||||||||||||||||||||||||||||
| 94 | end | ||||||||||||||||||||||||||||||||
| 95 | string join \\n -- \$words | ||||||||||||||||||||||||||||||||
| 96 | end | ||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||
| 98 | # ${p}_using PATH... N|N+ | ||||||||||||||||||||||||||||||||
| 99 | # true if the words are PATH followed by exactly N (N+: at least N) words | ||||||||||||||||||||||||||||||||
| 100 | function ${p}_using | ||||||||||||||||||||||||||||||||
| 101 | set -l n \$argv[-1] | ||||||||||||||||||||||||||||||||
| 102 | set -l path \$argv[1..-2] | ||||||||||||||||||||||||||||||||
| 103 | set -l words (${p}_words) | ||||||||||||||||||||||||||||||||
| 104 | set -l np (count \$path) | ||||||||||||||||||||||||||||||||
| 105 | if test \$np -gt 0 | ||||||||||||||||||||||||||||||||
| 106 | test "\$words[1..\$np]" = "\$path"; or return 1 | ||||||||||||||||||||||||||||||||
| 107 | end | ||||||||||||||||||||||||||||||||
| 108 | set -l rest (math (count \$words) - \$np) | ||||||||||||||||||||||||||||||||
| 109 | if string match -q '*+' -- \$n | ||||||||||||||||||||||||||||||||
| 110 | test \$rest -ge (string trim -r -c + -- \$n) | ||||||||||||||||||||||||||||||||
| 111 | else | ||||||||||||||||||||||||||||||||
| 112 | test \$rest -eq \$n | ||||||||||||||||||||||||||||||||
| 113 | end | ||||||||||||||||||||||||||||||||
| 114 | end | ||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||
| 116 | # ${p}_prefix PATH...: true if the words start with PATH | ||||||||||||||||||||||||||||||||
| 117 | function ${p}_prefix | ||||||||||||||||||||||||||||||||
| 118 | ${p}_using \$argv 0+ | ||||||||||||||||||||||||||||||||
| 119 | end | ||||||||||||||||||||||||||||||||
| 120 | 5 | $dynamic@{[ join '', @{ $state{functions} } ]} | |||||||||||||||||||||||||||||||
| 121 | 5 | complete -c @{[ _quote($appname) ]} -f | |||||||||||||||||||||||||||||||
| 122 | 5 | @{[ join "\n", @$lines ]} | |||||||||||||||||||||||||||||||
| 123 | EOM | ||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||
| 126 | 71 | ▶ | sub completion_commands { | ||||||||||||||||||||||||||||||
|
Subroutine
completion_commands: called CC=20 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 127 | 71 | my ($self, %args) = @_; | |||||||||||||||||||||||||||||||
| 128 | 71 | ▶ | my $commands = $args{commands} || {}; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$args{'commands'} || {}
MC/DC100%1/1
$args{'commands'} || {}
$args{'commands'}
Truth table100%2/2
A
A= $args{'commands'}
| |||||||||||||||||||||||||||||||||
| 129 | 71 | ▶ | my $options = $args{options} || []; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$args{'options'} || []
MC/DC0%0/1
$args{'options'} || []
$args{'options'}
Truth table50%1/2
A
A= $args{'options'}
| |||||||||||||||||||||||||||||||||
| 130 | 71 | ▶ | my $parameters = $args{parameters} || []; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$args{'parameters'} || []
MC/DC0%0/1
$args{'parameters'} || []
$args{'parameters'}
Truth table50%1/2
A
A= $args{'parameters'}
| |||||||||||||||||||||||||||||||||
| 131 | 71 | my $previous = $args{previous}; | |||||||||||||||||||||||||||||||
| 132 | 71 | my $state = $args{state}; | |||||||||||||||||||||||||||||||
| 133 | 71 | my $appname = _quote($self->spec->name); | |||||||||||||||||||||||||||||||
| 134 | 71 | my $p = $self->prefix; | |||||||||||||||||||||||||||||||
| 135 | 71 | my @path = map { _quote($_) } @$previous; | |||||||||||||||||||||||||||||||
| 136 | 71 | ▶ | my $using = sub { _quote(join ' ', "${p}_using", @path, @_) }; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 137 | 71 | my $prefix = _quote(join ' ', "${p}_prefix", @path); | |||||||||||||||||||||||||||||||
| 138 | 71 | my @lines; | |||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||
| 140 | 71 | for my $key (sort grep { not m/^_/ } keys %$commands) { | |||||||||||||||||||||||||||||||
| 141 | 62 | my $cmd = $commands->{ $key }; | |||||||||||||||||||||||||||||||
| 142 | 62 | push @lines, "complete -c $appname -n " . $using->(0) | |||||||||||||||||||||||||||||||
| 143 | . " -f -a " . _quote($cmd->name) . _desc($cmd->summary); | ||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||
| 146 | 71 | for my $i (0 .. $#$parameters) { | |||||||||||||||||||||||||||||||
| 147 | 27 | my $param = $parameters->[ $i ]; | |||||||||||||||||||||||||||||||
| 148 | 27 | ▶ | my $values = $self->value_args(%args, arg => $param) or next; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 149 | 23 | ▶ | my $n = $param->multiple ? "$i+" : $i; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 150 | 23 | push @lines, "complete -c $appname -n " . $using->($n) | |||||||||||||||||||||||||||||||
| 151 | . " $values" . _desc($param->summary); | ||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||
| 154 | 71 | my %optspecs; | |||||||||||||||||||||||||||||||
| 155 | 71 | for my $opt (@$options) { | |||||||||||||||||||||||||||||||
| 156 | 61 | ▶ | my @names = ($opt->name, @{ $opt->aliases || [] }); | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$opt->aliases || []
MC/DC0%0/1
$opt->aliases || []
$opt->aliases
Truth table50%1/2
A
A= $opt->aliases
| |||||||||||||||||||||||||||||||||
| 157 | 61 | my $type = $opt->type; | |||||||||||||||||||||||||||||||
| 158 | 61 | ▶ | my $takes_value = (not ref $type and $type ne 'flag') ? 1 : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not ref $type and $type ne "flag"
MC/DC50%1/2
not ref $type and $type ne "flag"
not ref $type
$type ne "flag"
Truth table66%2/3
not(A) and B
A= not ref $typeB= $type ne "flag"
| |||||||||||||||||||||||||||||||||
| 159 | # argparse rejects duplicate names; value-taking wins | ||||||||||||||||||||||||||||||||
| 160 | 61 | ▶ | $optspecs{ $_ } ||= $takes_value for @names; | ||||||||||||||||||||||||||||||
|
Condition66%2/3
$optspecs{$_} ||= $takes_value
MC/DC50%1/2
$optspecs{$_} ||= $takes_value
$optspecs{$_}
$takes_value
Truth table66%2/3
A ||= B
A= $optspecs{$_}B= $takes_value
| |||||||||||||||||||||||||||||||||
| 161 | my $names = join ' ', map { | ||||||||||||||||||||||||||||||||
| 162 | 61 | ▶ | (length > 1 ? "-l " : "-s ") . _quote($_) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 163 | } @names; | ||||||||||||||||||||||||||||||||
| 164 | 61 | my $line = "complete -c $appname -n $prefix $names" . _desc($opt->summary); | |||||||||||||||||||||||||||||||
| 165 | 61 | ▶ | if ($takes_value) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 166 | 25 | my $values = $self->value_args(%args, arg => $opt); | |||||||||||||||||||||||||||||||
| 167 | # -x: plain -r would offer files for a free-form value | ||||||||||||||||||||||||||||||||
| 168 | 25 | ▶ | $line .= $values ? " -r $values" : " -x"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||
| 170 | 61 | push @lines, $line; | |||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||
| 172 | 71 | ▶ | $state->{optspecs}->{ "@$previous" } = \%optspecs if %optspecs; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 173 | 71 | ▶ | push @{ $state->{parents} }, "@$previous" if %$commands; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||
| 175 | 71 | for my $key (sort keys %$commands) { | |||||||||||||||||||||||||||||||
| 176 | 66 | my $cmd = $commands->{ $key }; | |||||||||||||||||||||||||||||||
| 177 | 66 | push @lines, @{ $self->completion_commands( | |||||||||||||||||||||||||||||||
| 178 | commands => $cmd->subcommands, | ||||||||||||||||||||||||||||||||
| 179 | options => $cmd->options, | ||||||||||||||||||||||||||||||||
| 180 | parameters => $cmd->parameters, | ||||||||||||||||||||||||||||||||
| 181 | previous => [@$previous, $cmd->name], | ||||||||||||||||||||||||||||||||
| 182 | state => $state, | ||||||||||||||||||||||||||||||||
| 183 | ) }; | ||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||
| 186 | 71 | return \@lines; | |||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||
| 189 | # one switch case per subcommand path with options | ||||||||||||||||||||||||||||||||
| 190 | 5 | ▶ | sub _optspecs_cases { | ||||||||||||||||||||||||||||||
|
Subroutine
_optspecs_cases: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 191 | 5 | my ($optspecs) = @_; | |||||||||||||||||||||||||||||||
| 192 | 5 | my @cases; | |||||||||||||||||||||||||||||||
| 193 | 5 | for my $path (sort keys %$optspecs) { | |||||||||||||||||||||||||||||||
| 194 | 27 | my $specs = $optspecs->{ $path }; | |||||||||||||||||||||||||||||||
| 195 | 27 | ▶ | my $list = join ' ', map { $_ . ($specs->{ $_ } ? '=' : '') } sort keys %$specs; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 196 | 27 | ▶ | push @cases, ' case ' . (length $path ? _quote($path) : "''") | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 197 | . "\n string join \\n -- $list"; | ||||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||
| 199 | 5 | return join "\n", @cases; | |||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||
| 202 | # returns complete(1) flags for the possible values of an option or parameter | ||||||||||||||||||||||||||||||||
| 203 | 52 | ▶ | sub value_args { | ||||||||||||||||||||||||||||||
|
Subroutine
value_args: called CC=7 SCAR=19.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 204 | 52 | my ($self, %args) = @_; | |||||||||||||||||||||||||||||||
| 205 | 52 | my $arg = $args{arg}; | |||||||||||||||||||||||||||||||
| 206 | 52 | my $type = $arg->type; | |||||||||||||||||||||||||||||||
| 207 | 52 | ▶ | if (my $enum = $arg->enum) { | ||||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||||
| 208 | 8 | return '-f -a ' . _quote_list(map { _quote($_) } @$enum); | |||||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||||
| 210 | elsif ($type =~ m/^file(name)?\z/) { | ||||||||||||||||||||||||||||||||
| 211 | 4 | return '-F'; | |||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||
| 213 | elsif ($type =~ m/^dir(name)?\z/) { | ||||||||||||||||||||||||||||||||
| 214 | 2 | return q{-f -a '(__fish_complete_directories (commandline -ct))'}; | |||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||
| 216 | elsif ($type eq 'user') { | ||||||||||||||||||||||||||||||||
| 217 | 0 | return q{-f -a '(__fish_complete_users)'}; | |||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||
| 219 | elsif ($type eq 'host') { | ||||||||||||||||||||||||||||||||
| 220 | 0 | return q{-f -a '(__fish_print_hostnames)'}; | |||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||
| 222 | elsif ($arg->completion) { | ||||||||||||||||||||||||||||||||
| 223 | 19 | return '-f -a ' . _quote('(' . $self->dynamic_completion(%args) . ')'); | |||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||
| 225 | 19 | return ''; | |||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||
| 228 | # returns the call for dynamic completion, generating a function if needed | ||||||||||||||||||||||||||||||||
| 229 | 19 | ▶ | sub dynamic_completion { | ||||||||||||||||||||||||||||||
|
Subroutine
dynamic_completion: called CC=18 SCAR=32.3
Pod
documented
| |||||||||||||||||||||||||||||||||
| 230 | 19 | my ($self, %args) = @_; | |||||||||||||||||||||||||||||||
| 231 | 19 | my $arg = $args{arg}; | |||||||||||||||||||||||||||||||
| 232 | 19 | my $name = $arg->name; | |||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||
| 234 | 19 | my $def = $arg->completion; | |||||||||||||||||||||||||||||||
| 235 | 19 | my ($op, $command, $command_string); | |||||||||||||||||||||||||||||||
| 236 | 19 | ▶ | if (not ref $def and $def == 1) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
not ref $def and $def == 1
MC/DC50%1/2
not ref $def and $def == 1
not ref $def
$def == 1
Truth table66%2/3
not(A) and B
A= not ref $defB= $def == 1
| |||||||||||||||||||||||||||||||||
| 237 | 3 | ▶ | my $possible_values = $arg->values or die "Error for '$name': completion: 1 but 'values' not defined"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 238 | 3 | ▶ | $op = $possible_values->{op} or die "Error for '$name': 'values' needs an 'op'"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||
| 240 | elsif (ref $def) { | ||||||||||||||||||||||||||||||||
| 241 | 16 | $op = $def->{op}; | |||||||||||||||||||||||||||||||
| 242 | 16 | $command = $def->{command}; | |||||||||||||||||||||||||||||||
| 243 | 16 | $command_string = $def->{command_string}; | |||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||
| 245 | else { | ||||||||||||||||||||||||||||||||
| 246 | 0 | die "Error for '$name': invalid value for 'completion'"; | |||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||
| 249 | 19 | ▶ | if ($op) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 250 | 5 | $args{state}->{dynamic} = 1; | |||||||||||||||||||||||||||||||
| 251 | 5 | return $self->prefix . '_dynamic ' . _quote($name); | |||||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||
| 254 | 14 | ▶ | my $function_name = join '_', $self->prefix, (map { _ident($_) } @{ $args{previous} }), | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 255 | ($arg->isa("App::Spec::Option") ? "option" : "param"), | ||||||||||||||||||||||||||||||||
| 256 | _ident($name), "completion"; | ||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||
| 258 | 14 | my $run; | |||||||||||||||||||||||||||||||
| 259 | 14 | ▶ | if ($command) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 260 | 13 | my @words; | |||||||||||||||||||||||||||||||
| 261 | 13 | for my $word (@$command) { | |||||||||||||||||||||||||||||||
| 262 | 38 | ▶ | unless (ref $word) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 263 | 25 | push @words, _quote($word); | |||||||||||||||||||||||||||||||
| 264 | 25 | next; | |||||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||||||
| 266 | 13 | ▶ | my $replace = $word->{replace} or next; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 267 | 13 | ▶ | if (ref $replace eq 'ARRAY') { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 268 | 0 | ▶ | next unless $replace->[0] eq 'SHELL_WORDS'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 269 | 0 | my $num = $replace->[1]; | |||||||||||||||||||||||||||||||
| 270 | # same indexing as zsh $words[$CURRENT...], CURRENT is the last word | ||||||||||||||||||||||||||||||||
| 271 | 0 | ▶ | my $index = $num eq 'CURRENT' ? -1 | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 272 | : $num =~ m/^-(\d+)\z/ ? -($1 + 1) | ||||||||||||||||||||||||||||||||
| 273 | : $num; | ||||||||||||||||||||||||||||||||
| 274 | 0 | push @words, "\$words[$index]"; | |||||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||||||
| 276 | elsif ($replace eq 'SELF') { | ||||||||||||||||||||||||||||||||
| 277 | 13 | push @words, '$words[1]'; | |||||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||
| 280 | 13 | $run = "@words"; | |||||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||||||
| 282 | elsif (defined $command_string) { | ||||||||||||||||||||||||||||||||
| 283 | # command_string is bash code in existing specs | ||||||||||||||||||||||||||||||||
| 284 | 1 | $run = 'CURRENT_WORD=$words[-1] bash -c ' . _quote($command_string); | |||||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||||||
| 286 | else { | ||||||||||||||||||||||||||||||||
| 287 | 0 | die "Error for '$name': 'completion' needs 'op', 'command' or 'command_string'"; | |||||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||
| 290 | 14 | push @{ $args{state}->{functions} }, <<"EOM"; | |||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||
| 292 | function $function_name | ||||||||||||||||||||||||||||||||
| 293 | set -l words (commandline -xpc) (commandline -ct) | ||||||||||||||||||||||||||||||||
| 294 | $run | ||||||||||||||||||||||||||||||||
| 295 | end | ||||||||||||||||||||||||||||||||
| 296 | EOM | ||||||||||||||||||||||||||||||||
| 297 | 14 | return $function_name; | |||||||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||
| 300 | # " -d 'summary'" or nothing for an empty summary | ||||||||||||||||||||||||||||||||
| 301 | 146 | ▶ | sub _desc { | ||||||||||||||||||||||||||||||
|
Subroutine
_desc: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 302 | 146 | my ($summary) = @_; | |||||||||||||||||||||||||||||||
| 303 | 146 | ▶ | $summary //= ''; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$summary //= ""
MC/DC100%1/1
$summary //= ""
$summary
Truth table100%2/2
A
A= $summary
| |||||||||||||||||||||||||||||||||
| 304 | 146 | $summary =~ s/\s+/ /g; | |||||||||||||||||||||||||||||||
| 305 | 146 | $summary =~ s/^ | \z//g; | |||||||||||||||||||||||||||||||
| 306 | 146 | ▶ | return length $summary ? " -d " . _quote($summary) : ''; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||
| 309 | # fish identifier part, e.g. lwp-request => lwp_request | ||||||||||||||||||||||||||||||||
| 310 | 34 | ▶ | sub _ident { | ||||||||||||||||||||||||||||||
|
Subroutine
_ident: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 311 | 34 | my ($name) = @_; | |||||||||||||||||||||||||||||||
| 312 | 34 | $name =~ tr/A-Za-z0-9_/_/c; | |||||||||||||||||||||||||||||||
| 313 | 34 | return $name; | |||||||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||
| 316 | # fish single quoted string, bare word if no quoting is needed | ||||||||||||||||||||||||||||||||
| 317 | 740 | ▶ | sub _quote { | ||||||||||||||||||||||||||||||
|
Subroutine
_quote: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 318 | 740 | my ($string) = @_; | |||||||||||||||||||||||||||||||
| 319 | 740 | ▶ | return $string if $string =~ m{\A[A-Za-z0-9_.,:+/@][A-Za-z0-9_.,:+/@-]*\z}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 320 | 310 | $string =~ s/([\\'])/\\$1/g; | |||||||||||||||||||||||||||||||
| 321 | 310 | return "'$string'"; | |||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||
| 324 | # quoted list of already quoted words: | ||||||||||||||||||||||||||||||||
| 325 | # "'almond milk' 'soy milk'" reads better than '\'almond milk\' \'soy milk\'' | ||||||||||||||||||||||||||||||||
| 326 | 8 | ▶ | sub _quote_list { | ||||||||||||||||||||||||||||||
|
Subroutine
_quote_list: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 327 | 8 | my $list = join ' ', @_; | |||||||||||||||||||||||||||||||
| 328 | 8 | ▶ | return _quote($list) unless $list =~ m/'/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 329 | 1 | $list =~ s/([\\"\$])/\\$1/g; | |||||||||||||||||||||||||||||||
| 330 | 1 | return qq{"$list"}; | |||||||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||
| 333 | 2 | 1; | |||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||
| 335 | __DATA__ | ||||||||||||||||||||||||||||||||