blib/lib/Complete/Fish/Gen/FromGetoptLong.pm

filter: Statementstmt 33.3% 25 / 75 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 62.5% 5 / 8 Podpod 100% 2 / 2 totaltotal 20.2% 32 / 158 CC 31 SCAR 60.8
CC
31
Cov
19%
gen_fish_complete_from_getopt_long_spec
650.0
gen_fish_complete_from_getopt_long_script
56.0
__ANON__
2.0
CRAP 438.1
↑ Summary
1 package Complete::Fish::Gen::FromGetoptLong;
2  
3 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Getopt::Long::Util qw(parse_getopt_long_opt_spec array_getopt_long_spec_to_hash);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use String::ShellQuote;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
11 1 our $DATE = '2022-08-11'; # DATE
12 1 our $DIST = 'Complete-Fish-Gen-FromGetoptLong'; # DIST
13 1 our $VERSION = '0.100'; # VERSION
14  
15 1 our %SPEC;
16  
17 1 require Exporter;
18 1 our @ISA = qw(Exporter);
19 1 our @EXPORT_OK = qw(
20 gen_fish_complete_from_getopt_long_script
21 gen_fish_complete_from_getopt_long_spec
22 );
23  
24 $SPEC{gen_fish_complete_from_getopt_long_spec} = {
25 1 v => 1.1,
26 summary => 'From Getopt::Long spec, generate tab completion '.
27 'commands for the fish shell',
28 description => <<'_',
29  
30  
31 _
32 args => {
33 spec => {
34 summary => 'Getopt::Long options specification',
35 schema => 'hash*',
36 req => 1,
37 pos => 0,
38 },
39 opt_desc => {
40 summary => 'Description for each option',
41 description => <<'_',
42  
43 This is optional and allows adding description for the complete command. Each
44 key of the hash should correspond to the option name without the dashes, e.g.
45 `s`, `long`.
46  
47 _
48 schema => 'hash*',
49 },
50 cmdname => {
51 summary => 'Command name to be completed',
52 schema => 'str*',
53 req => 1,
54 },
55 compname => {
56 summary => 'Completer name, if there is a completer for option values',
57 schema => 'str*',
58 },
59 },
60 result => {
61 schema => 'str*',
62 summary => 'A script that can be fed to the fish shell',
63 },
64 };
65 0 ▶sub gen_fish_complete_from_getopt_long_spec {
Subroutine gen_fish_complete_from_getopt_long_spec: not called CC=25 SCAR=64.8
Pod documented
66 0 my %args = @_;
67  
68 0 ▶ my $gospec = $args{spec} or return [400, "Please specify 'spec'"];
Branch: 1 branch
BranchTrueFalseTotal
my $gospec = $args{"spec"} or return [400, "Please specify 'spec'"] 0 0 0
69 0 ▶ my $cmdname = $args{cmdname} or return [400, "Please specify cmdname"];
Branch: 1 branch
BranchTrueFalseTotal
my $cmdname = $args{"cmdname"} or return [400, "Please specify cmdname"] 0 0 0
70 0 my $compname = $args{compname};
71 0 my $opt_desc = $args{opt_desc};
72  
73 0 my @cmds;
74 0 my $prefix = "complete -c ".shell_quote($cmdname);
75 0 my $a_val;
76 0 ▶ $a_val = shell_quote("(begin; set -lx COMP_SHELL fish; set -lx COMP_LINE (commandline); set -lx COMP_POINT (commandline -C); ".shell_quote($compname)."; end)")
Branch: 1 branch
BranchTrueFalseTotal
if $compname 0 0 0
77 if $compname;
78 0 push @cmds, "$prefix -e"; # currently does not work (fish bug?)
79 0 for my $ospec (sort {
80 # make sure <> is the last
81 0 ▶ my $a_is_diamond = $a eq '<>' ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
$a eq '<>' ? : 0 0 0
82 0 ▶ my $b_is_diamond = $b eq '<>' ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
$b eq '<>' ? : 0 0 0
83 0 ▶ $a_is_diamond <=> $b_is_diamond || $a cmp $b
Condition0%0/3
$a_is_diamond <=> $b_is_diamond || $a cmp $b
l!l&&r!l&&!r
000
MC/DC0%0/2
$a_is_diamond <=> $b_is_diamond || $a cmp $b
$a_is_diamond <=> $b_is_diamond $a cmp $b
Truth table0%0/3
A || B
A= $a_is_diamond <=> $b_is_diamondB= $a cmp $b
ABresult
1X1
011
000
84 } keys %$gospec) {
85 0 ▶ my $res = parse_getopt_long_opt_spec($ospec)
Branch: 1 branch
BranchTrueFalseTotal
my $res = parse_getopt_long_opt_spec($ospec) or die "Can't parse option spec '${ospec}'" 0 0 0
86 or die "Can't parse option spec '$ospec'";
87 0 ▶ if ($res->{is_arg} && $compname) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{'is_arg'} and $compname) { } 0 0 0
Condition0%0/3
$res->{"is_arg"} and $compname
!ll&&!rl&&r
000
MC/DC0%0/2
$res->{"is_arg"} and $compname
$res->{"is_arg"} $compname
Truth table0%0/3
A and B
A= $res->{"is_arg"}B= $compname
ABresult
0X0
100
111
88 0 push @cmds, "$prefix -a $a_val";
89 } else {
90 0 ▶ $res->{min_vals} //= $res->{type} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$res->{'type'} ? : 0 0 0
Condition0%0/2
$res->{"min_vals"} //= $res->{"type"} ? 1 : 0
l!l
00
MC/DC0%0/1
$res->{"min_vals"} //= $res->{"type"} ? 1 : 0
$res->{"min_vals"}
Truth table0%0/2
A
A= $res->{"min_vals"}
Aresult
11
00
91 0 ▶ $res->{max_vals} //= $res->{type} || $res->{opttype} ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
$res->{'type'} || $res->{'opttype'} ? : 0 0 0
Condition0%0/5
$res->{"max_vals"} //= $res->{"type"} || $res->{"opttype"} ? 1 : 0
l!l
00
$res->{"type"} or $res->{"opttype"}
l!l&&r!l&&!r
000
MC/DC0%0/1
$res->{"max_vals"} //= $res->{"type"} || $res->{"opttype"} ? 1 : 0
$res->{"max_vals"}
Truth table0%0/2
A
A= $res->{"max_vals"}
Aresult
11
00
92 0 for my $o0 (@{ $res->{opts} }) {
93 0 ▶ my @o = $res->{is_neg} && length($o0) > 1 ?
Branch: 1 branch
BranchTrueFalseTotal
$res->{'is_neg'} && length $o0 > 1 ? : 0 0 0
Condition0%0/3
$res->{"is_neg"} and length $o0 > 1
!ll&&!rl&&r
000
MC/DC0%0/2
$res->{"is_neg"} and length $o0 > 1
$res->{"is_neg"} length $o0 > 1
Truth table0%0/3
A and B
A= $res->{"is_neg"}B= length $o0 > 1
ABresult
0X0
100
111
94 ($o0, "no$o0", "no-$o0") : ($o0);
95 0 for my $o (@o) {
96 0 my $cmd = $prefix;
97 0 ▶ $cmd .= length($o) > 1 ? " -l '$o'" : " -s '$o'";
Branch: 1 branch
BranchTrueFalseTotal
length $o > 1 ? : 0 0 0
98 0 ▶ if ($opt_desc && $opt_desc->{$o}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_desc and $opt_desc->{$o}) 0 0 0
Condition0%0/3
$opt_desc and $opt_desc->{$o}
!ll&&!rl&&r
000
MC/DC0%0/2
$opt_desc and $opt_desc->{$o}
$opt_desc $opt_desc->{$o}
Truth table0%0/3
A and B
A= $opt_descB= $opt_desc->{$o}
ABresult
0X0
100
111
99 0 $cmd .= " -d ".shell_quote($opt_desc->{$o});
100 }
101 0 ▶ if ($res->{min_vals} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{"min_vals"} > 0) 0 0 0
102 0 ▶ if ($compname) {
Branch: 1 branch
BranchTrueFalseTotal
if ($compname) { } 0 0 0
103 0 $cmd .= " -r -f -a $a_val";
104 } else {
105 0 $cmd .= " -r";
106 }
107 }
108 0 push @cmds, $cmd;
109 }
110 }
111 }
112 }
113 0 [200, "OK", join("", map {"$_\n"} @cmds)];
114 }
115  
116 $SPEC{gen_fish_complete_from_getopt_long_script} = {
117 1 v => 1.1,
118 summary => 'Generate fish completion script from Getopt::Long script',
119 description => <<'_',
120  
121 This routine generate fish `complete` command for each short/long option,
122 enabling fish to display the options in a different color.
123  
124 Getopt::Long::Complete scripts are also supported.
125  
126 _
127 args => {
128 filename => {
129 schema => 'filename*',
130 req => 1,
131 pos => 0,
132 cmdline_aliases => {f=>{}},
133 },
134 cmdname => {
135 summary => 'Command name to be completed, defaults to filename',
136 schema => 'str*',
137 },
138 compname => {
139 summary => 'Completer name',
140 schema => 'str*',
141 },
142 skip_detect => {
143 schema => ['bool', is=>1],
144 cmdline_aliases => {D=>{}},
145 },
146 },
147 result => {
148 schema => 'str*',
149 summary => 'A script that can be fed to the fish shell',
150 },
151 };
152 0 ▶sub gen_fish_complete_from_getopt_long_script {
Subroutine gen_fish_complete_from_getopt_long_script: not called CC=7 SCAR=40.3
Pod documented
153 0 my %args = @_;
154  
155 0 my $filename = $args{filename};
156 0 ▶ return [404, "No such file or not a file: $filename"] unless -f $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $filename 0 0 0
157  
158 0 require Getopt::Long::Dump;
159 my $dump_res = Getopt::Long::Dump::dump_getopt_long_script(
160 filename => $filename,
161 skip_detect => $args{skip_detect},
162 0 );
163 0 ▶ return $dump_res unless $dump_res->[0] == 200;
Branch: 1 branch
BranchTrueFalseTotal
unless $dump_res->[0] == 200 0 0 0
164  
165 0 my $cmdname = $args{cmdname};
166 0 ▶ if (!$cmdname) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cmdname) 0 0 0
167 0 ($cmdname = $filename) =~ s!.+/!!;
168 }
169 0 my $compname = $args{compname};
170  
171 0 my $glspec = array_getopt_long_spec_to_hash($dump_res->[2]);
172  
173 # GL:Complete scripts can also complete arguments
174 0 ▶ my $mod = $dump_res->[3]{'func.detect_res'}[3]{'func.module'} // '';
Condition0%0/2
$dump_res->[3]{"func.detect_res"}[3]{"func.module"} // ""
l!l
00
MC/DC0%0/1
$dump_res->[3]{"func.detect_res"}[3]{"func.module"} // ""
$dump_res->[3]{"func.detect_res"}[3]{"func.module"}
Truth table0%0/2
A
A= $dump_res->[3]{"func.detect_res"}[3]{"func.module"}
Aresult
11
00
175 0 ▶ if ($mod eq 'Getopt::Long::Complete') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod eq "Getopt::Long::Complete") 0 0 0
176 0 ▶ $compname //= $cmdname;
Condition0%0/3
$compname //= $cmdname
l!l&&r!l&&!r
000
MC/DC0%0/2
$compname //= $cmdname
$compname $cmdname
Truth table0%0/3
A //= B
A= $compnameB= $cmdname
ABresult
1X1
011
000
177 0 ▶ $glspec->{'<>'} = sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
178 }
179  
180 gen_fish_complete_from_getopt_long_spec(
181 0 spec => $dump_res->[2],
182 cmdname => $cmdname,
183 compname => $compname,
184 );
185 }
186  
187 1 1;
188 # ABSTRACT: Generate fish completion script from Getopt::Long spec/script
189  
190 __END__
↑ Summary