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

filter: Statementstmt 33.9% 19 / 56 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 66.6% 4 / 6 Podpod 100% 1 / 1 totaltotal 29.2% 24 / 82 CC 11 SCAR 45.1
CC
11
Cov
26%
gen_fish_complete_from_perinci_cmdline_script
132.0
__ANON__
2.0
BEGIN
1.0
CRAP 91.1
↑ Summary
1 package Complete::Fish::Gen::FromPerinciCmdLine;
2  
3 1 our $DATE = '2016-10-28'; # DATE
4 1 our $VERSION = '0.004'; # VERSION
5  
6 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use String::ShellQuote;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 our %SPEC;
13  
14 1 require Exporter;
15 1 our @ISA = qw(Exporter);
16 1 our @EXPORT_OK = qw(gen_fish_complete_from_perinci_cmdline_script);
17  
18 $SPEC{gen_fish_complete_from_perinci_cmdline_script} = {
19 1 v => 1.1,
20 summary => 'Dump Perinci::CmdLine script '.
21 'and generate fish completion from it',
22 description => <<'_',
23  
24 This routine generate `complete` fish command for each option, allowing fish to
25 display the options in a different color.
26  
27 _
28 args => {
29 filename => {
30 schema => 'filename*',
31 req => 1,
32 pos => 0,
33 cmdline_aliases => {f=>{}},
34 },
35 cmdname => {
36 summary => 'Command name (by default will be extracted from filename)',
37 schema => 'str*',
38 },
39 compname => {
40 summary => 'Completer name (in case different from cmdname)',
41 schema => 'str*',
42 },
43 skip_detect => {
44 schema => ['bool', is=>1],
45 cmdline_aliases => {D=>{}},
46 },
47 },
48 result => {
49 schema => 'str*',
50 summary => 'A script that can be fed to the fish shell',
51 },
52 };
53 0 ▶sub gen_fish_complete_from_perinci_cmdline_script {
Subroutine gen_fish_complete_from_perinci_cmdline_script: not called CC=11 SCAR=48.8
Pod documented
54 0 my %args = @_;
55 0 my $filename = $args{filename};
56  
57 0 require Perinci::CmdLine::Dump;
58 my $dump_res = Perinci::CmdLine::Dump::dump_perinci_cmdline_script(
59 filename => $filename,
60 skip_detect => $args{skip_detect},
61 0 );
62 0 ▶ return [500, "Can't dump script: $dump_res->[0] - $dump_res->[1]"]
Branch: 1 branch
BranchTrueFalseTotal
unless $dump_res->[0] == 200 0 0 0
63 unless $dump_res->[0] == 200;
64  
65 0 my $cli = $dump_res->[2];
66  
67 0 ▶ if ($cli->{subcommands}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cli->{"subcommands"}) 0 0 0
68 0 return [412, "Sorry, script with subcommands not yet supported"];
69 }
70  
71 0 state $pa = do {
72 0 require Perinci::Access;
73 0 Perinci::Access->new;
74 };
75 0 my $riap_res = $pa->request(meta => $cli->{url});
76 0 ▶ return [500, "Can't Riap request: meta => $cli->{url}: ".
Branch: 1 branch
BranchTrueFalseTotal
unless $riap_res->[0] == 200 0 0 0
77 "$riap_res->[0] - $riap_res->[1]"]
78 unless $riap_res->[0] == 200;
79  
80 0 my $meta = $riap_res->[2];
81  
82 0 require Perinci::Sub::GetArgs::Argv;
83 my $gengls_res = Perinci::Sub::GetArgs::Argv::gen_getopt_long_spec_from_meta(
84 meta => $meta,
85 meta_is_normalized => 1,
86 common_opts => $cli->{common_opts},
87 per_arg_json => $cli->{per_arg_json},
88 per_arg_yaml => $cli->{per_arg_yaml},
89 0 );
90 0 ▶ return [500, "Can't generate Getopt::Long spec: ".
Branch: 1 branch
BranchTrueFalseTotal
unless $gengls_res->[0] == 200 0 0 0
91 "$gengls_res->[0] - $gengls_res->[1]"]
92 unless $gengls_res->[0] == 200;
93 0 my $glspec = $gengls_res->[2];
94 0 ▶ $glspec->{'<>'} = sub{};
Subroutine __ANON__: not called CC=1 SCAR=6.9
95  
96 0 require Perinci::Sub::To::CLIDocData;
97 my $genclidocdata_res = Perinci::Sub::To::CLIDocData::gen_cli_doc_data_from_meta(
98 common_opts => $cli->{common_opts},
99 ggls_res => $gengls_res,
100 meta => $meta,
101 meta_is_normalized => 1,
102 per_arg_json => $cli->{per_arg_json},
103 per_arg_yaml => $cli->{per_arg_yaml},
104 0 );
105 0 ▶ return [500, "Can't generate CLI doc data: ".
Branch: 1 branch
BranchTrueFalseTotal
unless $genclidocdata_res->[0] == 200 0 0 0
106 "$genclidocdata_res->[0] - $genclidocdata_res->[1]"]
107 unless $genclidocdata_res->[0] == 200;
108 0 my $clidocdata = $genclidocdata_res->[2];
109  
110 0 my $opt_desc = {};
111 0 for my $k (sort keys %{$clidocdata->{opts}}) {
112 0 my $v = $clidocdata->{opts}{$k};
113 0 ▶ next unless $v->{summary};
Branch: 1 branch
BranchTrueFalseTotal
unless $v->{"summary"} 0 0 0
114 0 my @o = $k =~ /--?(\S+)/g;
115 0 for my $o (@o) {
116 0 $opt_desc->{$o} = $v->{summary};
117 }
118 }
119  
120 0 my $cmdname = $args{cmdname};
121 0 ▶ if (!$cmdname) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cmdname) 0 0 0
122 0 ($cmdname = $filename) =~ s!.+/!!;
123 }
124 0 ▶ my $compname = $args{compname} // $cmdname;
Condition0%0/3
$args{"compname"} // $cmdname
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"compname"} // $cmdname
$args{"compname"} $cmdname
Truth table0%0/3
A // B
A= $args{"compname"}B= $cmdname
ABresult
1X1
011
000
125  
126 0 require Complete::Fish::Gen::FromGetoptLong;
127 0 Complete::Fish::Gen::FromGetoptLong::gen_fish_complete_from_getopt_long_spec(
128 spec => $glspec,
129 opt_desc => $opt_desc,
130 cmdname => $cmdname,
131 compname => $compname,
132 );
133 }
134  
135 1 1;
136 # ABSTRACT: Dump Perinci::CmdLine script and generate fish completion from it
137  
138 __END__
↑ Summary