Usul/TraitFor/UntaintedGetopts.pm

filter: Statementstmt 95.1% 136 / 143 Branchbran 72.9% 35 / 48 Conditioncond 64.8% 24 / 37 MC/DCmcdc 34.7% 8 / 23 Subroutinesub 91.6% 22 / 24 Podpod 100% 6 / 6 totaltotal 82.2% 231 / 281 CC 42 SCAR 19.4
CC
42
Cov
86%
__ANON__
16.8
__ANON__
8.1
__ANON__
6.0
CRAP 7.0
« Usul/TraitFor/OutputLogging.pm ↑ Summary Usul/TraitFor/Usage.pm »
1 package Class::Usul::TraitFor::UntaintedGetopts;
2  
3 18 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 18 ▶use Class::Usul::Constants qw( FAILED NUL QUOTED_RE TRUE );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 18 ▶use Class::Usul::Functions qw( emit_err untaint_cmdline );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 18 ▶use Class::Usul::Getopt qw( describe_options );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 18 ▶use Data::Record;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 18 ▶use Encode qw( decode );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 18 ▶use JSON::MaybeXS qw( decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 18 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 18 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 18 my $Extra_Argv = []; my $Untainted_Argv = [];
15  
16 18 my $Usage = "Did we forget new_with_options?\n";
17  
18 # Private functions
19 5 ▶my $_extra_argv = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
20 5 ▶ return $_[ 0 ]->{_extra_argv} //= [ @{ $Extra_Argv } ];
Condition100%2/2
$_[0]{"_extra_argv"} //= [@{$Extra_Argv;}]
l!l
41
MC/DC100%1/1
$_[0]{"_extra_argv"} //= [@{$Extra_Argv;}]
$_[0]{"_extra_argv"}
Truth table100%2/2
A
A= $_[0]{"_extra_argv"}
Aresult
11
00
21 18 };
22  
23 4 ▶my $_extract_params = sub {
Subroutine __ANON__: called CC=8 SCAR=20.9
24 4 my ($args, $config, $options_data, $cmdline_opt) = @_;
25  
26 4 my $params = { %{ $args } }; my @missing_required;
27  
28 4 my $prefer = $config->{prefer_commandline};
29  
30 4 for my $name (keys %{ $options_data }) {
31 67 my $option = $options_data->{ $name };
32  
33 67 ▶ if ($prefer or not defined $params->{ $name }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefer or not defined $params->{$name}) 67 0 67
Condition33%1/3
$prefer or not defined $params->{$name}
l!l&&r!l&&!r
6700
MC/DC0%0/2
$prefer or not defined $params->{$name}
$prefer not defined $params->{$name}
Truth table33%1/3
A or not(B)
A= $preferB= not defined $params->{$name}
ABresult
1X1
011
000
34 67 my $val; defined ($val = $cmdline_opt->$name()) and
35 67 ▶ $params->{ $name } = $option->{json} ? decode_json( $val ) : $val;
Branch: 2 branches
BranchTrueFalseTotal
defined($val = $cmdline_opt->$name) and $params->{$name} = $option->{"json"} ? &decode_json($val) : $val 9 58 67
$option->{'json'} ? : 1 8 9
36 }
37  
38 67 ▶ $option->{required} and not defined $params->{ $name }
Branch: 1 branch
BranchTrueFalseTotal
$option->{"required"} and not defined $params->{$name} and push @missing_required, $name 1 66 67
Condition100%3/3
$option->{"required"} and not defined $params->{$name}
!ll&&!rl&&r
6421
MC/DC100%2/2
$option->{"required"} and not defined $params->{$name}
$option->{"required"} not defined $params->{$name}
Truth table100%3/3
A and not(B)
A= $option->{"required"}B= not defined $params->{$name}
ABresult
0X0
100
111
39 and push @missing_required, $name;
40 }
41  
42 4 return ($params, @missing_required);
43 18 };
44  
45 67 ▶my $_option_specification = sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
46 67 my ($name, $opt) = @_;
47  
48 67 my $dash_name = $name; $dash_name =~ tr/_/-/; # Dash name support
49 67 my $option_spec = $dash_name;
50  
51 67 ▶ defined $opt->{short } and $option_spec .= '|'.$opt->{short};
Branch: 1 branch
BranchTrueFalseTotal
defined $opt->{"short"} and $option_spec .= '|' . $opt->{"short"} 28 39 67
52 67 ▶ $opt->{repeatable} and not defined $opt->{format} and $option_spec .= '+';
Branch: 1 branch
BranchTrueFalseTotal
$opt->{"repeatable"} and not defined $opt->{"format"} and $option_spec .= '+' 6 61 67
Condition100%3/3
$opt->{"repeatable"} and not defined $opt->{"format"}
!ll&&!rl&&r
5836
MC/DC100%2/2
$opt->{"repeatable"} and not defined $opt->{"format"}
$opt->{"repeatable"} not defined $opt->{"format"}
Truth table100%3/3
A and not(B)
A= $opt->{"repeatable"}B= not defined $opt->{"format"}
ABresult
0X0
100
111
53 67 ▶ $opt->{negateable} and $option_spec .= '!';
Branch: 1 branch
BranchTrueFalseTotal
$opt->{"negateable"} and $option_spec .= '!' 2 65 67
54 67 ▶ defined $opt->{format} and $option_spec .= '='.$opt->{format};
Branch: 1 branch
BranchTrueFalseTotal
defined $opt->{"format"} and $option_spec .= '=' . $opt->{"format"} 33 34 67
55 67 return $option_spec;
56 18 };
57  
58 4 ▶my $_set_usage_conf = sub { # Should be in describe_options third argument
Subroutine __ANON__: called CC=1 SCAR=0.0
59 4 return Class::Usul::Getopt::Usage->usage_conf( $_[ 0 ] );
60 18 };
61  
62 3 ▶my $_split_args = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
63 3 my $splitters = shift; my @new_argv;
64  
65 3 ▶ for (my $i = 0, my $nargvs = @ARGV; $i < $nargvs; $i++) { # Parse all argv
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $nargvs) 19 3 22
66 19 my $arg = $ARGV[ $i ];
67  
68 19 my ($name, $value) = split m{ [=] }mx, $arg, 2; $name =~ s{ \A --? }{}mx;
69  
70 19 ▶ if (my $splitter = $splitters->{ $name }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $splitter = $splitters->{$name}) { } 3 16 19
71 3 ▶ $value //= $ARGV[ ++$i ];
Condition66%2/3
$value //= $ARGV[++$i]
l!l&&r!l&&!r
120
MC/DC0%0/2
$value //= $ARGV[++$i]
$value $ARGV[++$i]
Truth table66%2/3
A //= B
A= $valueB= $ARGV[++$i]
ABresult
1X1
011
000
72  
73 3 for my $subval (map { s{ \A [\'\"] | [\'\"] \z }{}gmx; $_ }
74 $splitter->records( $value )) {
75 4 push @new_argv, "--${name}", $subval;
76 }
77 }
78 16 else { push @new_argv, $arg }
79 }
80  
81 3 return @new_argv;
82 18 };
83  
84 199 ▶my $_sort_options = sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
85 199 my ($opts, $a, $b) = @_; my $max = 999;
86  
87 199 ▶ my $oa = $opts->{ $a }{order} || $max; my $ob = $opts->{ $b }{order} || $max;
Condition66%4/6
$opts->{$a}{'order'} || $max
l!l&&r!l&&!r
161830
$opts->{$b}{'order'} || $max
l!l&&r!l&&!r
101890
MC/DC0%0/2
$opts->{$a}{'order'} || $max
$opts->{$a}{'order'} $max
MC/DC0%0/2
$opts->{$b}{'order'} || $max
$opts->{$b}{'order'} $max
Truth table66%2/3
A || B
A= $opts->{$a}{'order'}B= $max
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= $opts->{$b}{'order'}B= $max
ABresult
1X1
011
000
88  
89 199 ▶ return ($oa == $max) && ($ob == $max) ? $a cmp $b : $oa <=> $ob;
Branch: 1 branch
BranchTrueFalseTotal
$oa == $max && $ob == $max ? : 175 24 199
Condition100%3/3
$oa == $max and $ob == $max
!ll&&!rl&&r
168175
MC/DC100%2/2
$oa == $max and $ob == $max
$oa == $max $ob == $max
Truth table100%3/3
A and B
A= $oa == $maxB= $ob == $max
ABresult
0X0
100
111
90 18 };
91  
92 0 ▶my $_untainted_argv = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
93 0 ▶ return $_[ 0 ]->{_untainted_argv} //= [ @{ $Untainted_Argv } ];
Condition0%0/2
$_[0]{"_untainted_argv"} //= [@{$Untainted_Argv;}]
l!l
00
MC/DC0%0/1
$_[0]{"_untainted_argv"} //= [@{$Untainted_Argv;}]
$_[0]{"_untainted_argv"}
Truth table0%0/2
A
A= $_[0]{"_untainted_argv"}
Aresult
11
00
94 18 };
95  
96 4 ▶my $_build_options = sub {
Subroutine __ANON__: called CC=6 SCAR=18.0
97 4 my $options_data = shift; my $splitters = {}; my @options = ();
98  
99 4 for my $name (sort { $_sort_options->( $options_data, $a, $b ) }
100 4 keys %{ $options_data }) {
101 67 my $option = $options_data->{ $name };
102 67 ▶ my $cfg = $option->{config} // {};
Condition50%1/2
$option->{"config"} // {}
l!l
067
MC/DC0%0/1
$option->{"config"} // {}
$option->{"config"}
Truth table50%1/2
A
A= $option->{"config"}
Aresult
11
00
103 67 ▶ my $doc = $option->{doc } // "No help for ${name}";
Condition100%2/2
$option->{"doc"} // "No help for $name"
l!l
4522
MC/DC100%1/1
$option->{"doc"} // "No help for $name"
$option->{"doc"}
Truth table100%2/2
A
A= $option->{"doc"}
Aresult
11
00
104  
105 67 push @options, [ $_option_specification->( $name, $option ), $doc, $cfg ];
106 67 ▶ defined $option->{autosplit} or next;
Branch: 1 branch
BranchTrueFalseTotal
defined $option->{"autosplit"} or next 3 64 67
107 $splitters->{ $name } = Data::Record->new( {
108 3 split => $option->{autosplit}, unless => QUOTED_RE } );
109 $option->{short}
110 3 ▶ and $splitters->{ $option->{short} } = $splitters->{ $name };
Branch: 1 branch
BranchTrueFalseTotal
$option->{"short"} and $$splitters{$option->{"short"}} = $splitters->{$name} 0 3 3
111 }
112  
113 4 return ($splitters, @options);
114 18 };
115  
116 # Private methods
117 4 ▶my $_parse_options = sub {
Subroutine __ANON__: called CC=13 SCAR=28.2
118 4 my ($self, %args) = @_; my $opt;
119  
120 4 ▶ my $class = blessed $self || $self;
Condition33%1/3
builtin::blessed($self) || $self
l!l&&r!l&&!r
040
MC/DC0%0/2
builtin::blessed($self) || $self
builtin::blessed($self) $self
Truth table33%1/3
A || B
A= builtin::blessed($self)B= $self
ABresult
1X1
011
000
121 4 my %data = $class->_options_data;
122 4 my %config = $class->_options_config;
123 4 ▶ my $enc = $config{encoding} // 'UTF-8';
Condition50%1/2
$config{"encoding"} // "UTF-8"
l!l
04
MC/DC0%0/1
$config{"encoding"} // "UTF-8"
$config{"encoding"}
Truth table50%1/2
A
A= $config{"encoding"}
Aresult
11
00
124  
125 4 my @skip_options; defined $config{skip_options}
126 4 ▶ and @skip_options = @{ $config{skip_options} };
Branch: 1 branch
BranchTrueFalseTotal
defined $config{"skip_options"} and @skip_options = @{$config{"skip_options"};} 4 0 4
127  
128 4 ▶ @skip_options and delete @data{ @skip_options };
Branch: 1 branch
BranchTrueFalseTotal
@skip_options and delete @data{@skip_options} 0 4 4
129  
130 4 my ($splitters, @options) = $_build_options->( \%data );
131  
132 4 my %gld_conf; my @gld_attr = ('getopt_conf', 'show_defaults');
133  
134 4 ▶ my $usage_opt = $config{usage_opt} ? $config{usage_opt} : 'Usage: %c %o';
Branch: 1 branch
BranchTrueFalseTotal
$config{'usage_opt'} ? : 4 0 4
135  
136 4 @gld_conf{ @gld_attr } = @config{ @gld_attr };
137 4 ▶ $config{usage_conf } and $_set_usage_conf->( $config{usage_conf} );
Branch: 1 branch
BranchTrueFalseTotal
$config{"usage_conf"} and &$_set_usage_conf($config{"usage_conf"}) 4 0 4
138 4 ▶ $config{protect_argv } and local @ARGV = @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
$config{"protect_argv"} and local(@ARGV) = @ARGV 4 0 4
139 4 ▶ $enc and @ARGV = map { decode( $enc, $_ ) } @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
$enc and @ARGV = map({&decode($enc, $_);} @ARGV) 4 0 4
140 4 ▶ $config{no_untaint } or @ARGV = map { untaint_cmdline $_ } @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
$config{"no_untaint"} or @ARGV = map({&untaint_cmdline($_);} @ARGV) 0 4 4
141 4 $Untainted_Argv = [ @ARGV ];
142 4 ▶ keys %{ $splitters } and @ARGV = $_split_args->( $splitters );
Branch: 1 branch
BranchTrueFalseTotal
keys %{$splitters;} and @ARGV = &$_split_args($splitters) 3 1 4
143 4 ($opt, $Usage) = describe_options( $usage_opt, @options, \%gld_conf );
144 4 $Extra_Argv = [ @ARGV ];
145  
146 4 my ($params, @missing)
147 = $_extract_params->( \%args, \%config, \%data, $opt );
148  
149 4 ▶ if ($config{missing_fatal} and @missing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config{"missing_fatal"} and @missing) 0 4 4
Condition33%1/3
$config{"missing_fatal"} and @missing
!ll&&!rl&&r
400
MC/DC0%0/2
$config{"missing_fatal"} and @missing
$config{"missing_fatal"} @missing
Truth table33%1/3
A and B
A= $config{"missing_fatal"}B= @missing
ABresult
0X0
100
111
150 0 emit_err join( "\n", map { "Option '${_}' is missing" } @missing );
151 0 emit_err $Usage;
152 0 exit FAILED;
153 }
154  
155 4 return %{ $params };
156 18 };
157  
158 # Construction
159 4 ▶sub new_with_options {
Subroutine new_with_options: called CC=1 SCAR=0.0
Pod documented
160 4 my $self = shift; return $self->new( $self->$_parse_options( @_ ) );
161 }
162  
163 # Public methods
164 3 ▶sub extra_argv {
Subroutine extra_argv: called CC=2 SCAR=7.6
Pod documented
165 3 ▶ return defined $_[ 1 ] ? $_extra_argv->( $_[ 0 ] )->[ $_[ 1 ] ]
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 0 3 3
166 : $_extra_argv->( $_[ 0 ] );
167 }
168  
169 1 ▶sub next_argv {
Subroutine next_argv: called CC=1 SCAR=0.0
Pod documented
170 1 return shift @{ $_extra_argv->( $_[ 0 ] ) };
171 }
172  
173 3 ▶sub options_usage {
Subroutine options_usage: called CC=1 SCAR=0.0
Pod documented
174 3 return ucfirst $Usage;
175 }
176  
177 1 ▶sub unshift_argv {
Subroutine unshift_argv: called CC=1 SCAR=0.0
Pod documented
178 1 return unshift @{ $_extra_argv->( $_[ 0 ] ) }, $_[ 1 ];
179 }
180  
181 0 ▶sub untainted_argv {
Subroutine untainted_argv: not called CC=2 SCAR=17.9
Pod documented
182 0 ▶ return defined $_[ 1 ] ? $_untainted_argv->( $_[ 0 ] )->[ $_[ 1 ] ]
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 0 0 0
183 : $_untainted_argv->( $_[ 0 ] );
184 }
185  
186 18 1;
187  
188 __END__
« Usul/TraitFor/OutputLogging.pm ↑ Summary Usul/TraitFor/Usage.pm »