Janus/CLI.pm

filter: Statementstmt 64.1% 68 / 106 Branchbran 42.8% 30 / 70 Conditioncond 37.5% 9 / 24 MC/DCmcdc 12.5% 2 / 16 Subroutinesub 80.0% 8 / 10 Podpod 0.0% 0 / 6 totaltotal 50.4% 117 / 232 CC 45 SCAR 37.0
CC
45
Cov
54%
consider_values
58.5
parse_values
46.7
complain_about
30.0
CRAP 40.4
« Janus/SessionBase.pm ↑ Summary Janus/Facade.pm »
1
2 4 require 5;
3 package Getopt::Janus::CLI;
4 # Get command-line interface options (yup, from @ARGV)
5
6 4 @ISA = ('Getopt::Janus::SessionBase');
7 4 $VERSION = '1.03';
8 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Getopt::Janus (); # makes sure Getopt::Janus::DEBUG is defined
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶BEGIN { *DEBUG = \&Getopt::Janus::DEBUG }
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Getopt::Janus::SessionBase;
Subroutine BEGIN: called CC=1 SCAR=0.0
12
13 4 Getopt::Janus::DEBUG and print "Revving up ", __PACKAGE__, "\n";
14
15 0 ▶sub open_new_files { } # block it happening
Subroutine open_new_files: not called CC=1 SCAR=0.0
Pod undocumented
16
17 # TODO: make -h / --help produce help/longhelp (latter with license)
18
19 # ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
20
21 2 ▶sub review_result { } # no need for this all
Subroutine review_result: called CC=1 SCAR=0.0
Pod undocumented
22
23 2 ▶sub get_option_values {
Subroutine get_option_values: called CC=5 SCAR=20.9
Pod undocumented
24 2 my $self = shift;
25
26 2 ▶ if($^O =~ m/Win32/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O =~ /Win32/) 0 2 2
27 0 ▶ while( @ARGV and !length $ARGV[-1] ) { pop @ARGV }
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV and not length $ARGV[-1]) 0 0 0
Condition0%0/3
@ARGV and not length $ARGV[-1]
!ll&&!rl&&r
000
MC/DC0%0/2
@ARGV and not length $ARGV[-1]
@ARGV not length $ARGV[-1]
Truth table0%0/3
A and not(B)
A= @ARGVB= not length $ARGV[-1]
ABresult
0X0
100
111
28 }
29
30 2 my $run_flag = 1;
31 2 my @args = @ARGV;
32 2 my %unknowns;
33
34 my @values;
35 2 $self->parse_values(\@values, \@args, \%unknowns, \$run_flag);
36
37 2 ▶ if( $run_flag ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($run_flag) { } 2 0 2
38 2 DEBUG and print "parse_values has run_flag on, with values @values\n";
39 2 $self->consider_values( \@values );
40 } else {
41 0 DEBUG and print "parse_values has run_flag off\n values [@values]",
42 "\n unknowns [@{[sort keys %unknowns]}]\n args [@args]\n";
43 0 $self->complain_about( \@args, \%unknowns );
44 0 exit 1;
45 }
46 2 return;
47 }
48
49 # ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
50
51 2 ▶sub parse_values {
Subroutine parse_values: called CC=20 SCAR=38.4
Pod undocumented
52 2 my($self, $values, $args, $unknowns, $run_flag_s) = (@_);
53 2 my($long, $short) = @$self{ 'long', 'short' };
54
55 2 my $dummy = {'type' => 'yes_no', "_HACK_", 1};
56
57 2 local $_;
58 2 ▶ while(@$args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$args) 5 1 6
59 5 $_ = $args->[0];
60 5 ▶ last if $_ eq '-'; # not a switch at all
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "-" 0 5 5
61 5 ▶ shift(@$args), last if $_ eq '--'; # switch meaning 'end of switches'
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "--" 1 4 5
62
63 4 ▶ if( m/^-([_0-9a-zA-Z])$/s or m/^--?([-_0-9a-zA-Z]{2,})$/s ) { # -x or --xax
Branch: 3 branches
BranchTrueFalseTotal
if (/^-([_0-9a-zA-Z])$/s or /^--?([-_0-9a-zA-Z]{2,})$/s) { } 2 2 4
elsif (/^-([_0-9a-zA-Z])=(.*?)$/s) { } 1 1 2
elsif (/^--?([-_0-9a-zA-Z]{2,})=(.*?)$/s) { } 1 0 1
Condition100%3/3
/^-([_0-9a-zA-Z])$/s or /^--?([-_0-9a-zA-Z]{2,})$/s
l!l&&r!l&&!r
112
MC/DC100%2/2
/^-([_0-9a-zA-Z])$/s or /^--?([-_0-9a-zA-Z]{2,})$/s
/^-([_0-9a-zA-Z])$/s /^--?([-_0-9a-zA-Z]{2,})$/s
Truth table100%3/3
A or B
A= /^-([_0-9a-zA-Z])$/sB= /^--?([-_0-9a-zA-Z]{2,})$/s
ABresult
1X1
011
000
64 # And tolerate -xax
65 2 ▶ if(not( $short->{$1} || $long->{$1} )) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $short->{$1} || $long->{$1}) { } 0 2 2
elsif ('yes_no' eq +($short->{$1} || $long->{$1})->{'type'} or 'HELP' eq +($short->{$1} || $long->{$1})->{'type'}) { } 2 0 2
Condition66%2/3
$short->{$1} || $long->{$1}
l!l&&r!l&&!r
110
MC/DC0%0/2
$short->{$1} || $long->{$1}
$short->{$1} $long->{$1}
Truth table66%2/3
A || B
A= $short->{$1}B= $long->{$1}
ABresult
1X1
011
000
66 0 ++$unknowns->{$1};
67 0 DEBUG and print "Unknown option $1\n";
68 0 shift @$args;
69 ▶ } elsif(
Condition33%3/9
"yes_no" eq +($short->{$1} || $long->{$1})->{"type"} or "HELP" eq +($short->{$1} || $long->{$1})->{"type"}
l!l&&r!l&&!r
200
$short->{$1} || $long->{$1}
l!l&&r!l&&!r
110
$short->{$1} || $long->{$1}
l!l&&r!l&&!r
000
MC/DC0%0/2
"yes_no" eq +($short->{$1} || $long->{$1})->{"type"} or "HELP" eq +($short->{$1} || $long->{$1})->{"type"}
"yes_no" eq +($short->{$1} || $long->{$1})->{"type"} "HELP" eq +($short->{$1} || $long->{$1})->{"type"}
MC/DC0%0/2
$short->{$1} || $long->{$1}
$short->{$1} $long->{$1}
MC/DC0%0/2
$short->{$1} || $long->{$1}
$short->{$1} $long->{$1}
Truth table33%1/3
A or B
A= "yes_no" eq +($short->{$1} || $long->{$1})->{"type"}B= "HELP" eq +($short->{$1} || $long->{$1})->{"type"}
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= $short->{$1}B= $long->{$1}
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $short->{$1}B= $long->{$1}
ABresult
1X1
011
000
70 'yes_no' eq ( $short->{$1} || $long->{$1} )->{'type'}
71 or 'HELP' eq ( $short->{$1} || $long->{$1} )->{'type'}
72 ) {
73 2 push @$values, $1 => 1; # just note it as a true value and move on
74 2 shift @$args;
75 } else {
76 # It's a nonboolean value -- so snare the value and re-cycle
77 # it as a -x=foo or --xax=foo for the next pass
78 0 ▶ push @$args, '' if @$args == 1 and $^O =~ m/Win32/;
Branch: 1 branch
BranchTrueFalseTotal
if @$args == 1 and $^O =~ /Win32/ 0 0 0
Condition0%0/3
@$args == 1 and $^O =~ /Win32/
!ll&&!rl&&r
000
MC/DC0%0/2
@$args == 1 and $^O =~ /Win32/
@$args == 1 $^O =~ /Win32/
Truth table0%0/3
A and B
A= @$args == 1B= $^O =~ /Win32/
ABresult
0X0
100
111
79 0 $args->[0] .= '=' . splice(@$args,1,1);
80 }
81
82 } elsif( m/^-([_0-9a-zA-Z])=(.*?)$/s ) { # -x=foo
83 1 ▶ unless( exists $short->{$1} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $short->{$1}) { } 1 0 1
84 0 ++$unknowns->{$1};
85 } else {
86 1 push @$values, $1 => $2;
87 }
88 1 shift(@$args);
89
90 } elsif( m/^--?([-_0-9a-zA-Z]{2,})=(.*?)$/s ) { # --xax=foo
91 # and tolerate -xax=foo
92 1 ▶ unless( exists $long->{$1} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $long->{$1}) { } 1 0 1
93 0 ++$unknowns->{$1};
94 } else {
95 1 push @$values, $1 => $2;
96 }
97 1 shift(@$args);
98
99 } else {
100 0 $$run_flag_s = 0;
101 0 last; # leaving things unprocessed
102 }
103 }
104 2 ▶ $$run_flag_s = 0 if keys %$unknowns or @$args;
Branch: 1 branch
BranchTrueFalseTotal
if keys %$unknowns or @$args 0 2 2
Condition33%1/3
keys %$unknowns or @$args
l!l&&r!l&&!r
002
MC/DC0%0/2
keys %$unknowns or @$args
keys %$unknowns @$args
Truth table33%1/3
A or B
A= keys %$unknownsB= @$args
ABresult
1X1
011
000
105 2 return;
106 }
107
108 # ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
109
110 0 ▶sub complain_about {
Subroutine complain_about: not called CC=5 SCAR=34.0
Pod undocumented
111 0 my( $self, $args, $unknowns ) = @_;
112
113 0 ▶ if( keys %$unknowns ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$unknowns) 0 0 0
114 0 my @them = sort keys %$unknowns;
115 0 ▶ foreach (@them) { s/^(.)$/-$1/s or s/^(.+)$/--$1/s } # add the prefixes
Branch: 1 branch
BranchTrueFalseTotal
s/^(.)$/-$1/s or s/^(.+)$/--$1/s 0 0 0
116 0 print "Unknown options that you used: [@them]\n\n"
117 }
118 0 ▶ print "Arguments left unprocessed: [@$args]\n\n" if @$args;
Branch: 1 branch
BranchTrueFalseTotal
if @$args 0 0 0
119 0 print $self->short_help_message;
120 0 return;
121 }
122
123 # ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
124
125 2 ▶sub consider_values {
Subroutine consider_values: called CC=18 SCAR=40.7
Pod undocumented
126 2 my( $self, $values ) = @_;
127 2 my($long, $short) = @$self{ 'long', 'short' };
128
129 2 my %seen;
130 2 my($option, $key, $value, $type, $oldval);
131 2 DEBUG and print "Values: @$values\n";
132 2 ▶ while( @$values ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$values) 4 2 6
133 4 $key = $values->[0];
134 4 ▶ $option = ( (length($key) == 1) ? $short : $long )->{$key};
Branch: 1 branch
BranchTrueFalseTotal
length $key == 1 ? : 2 2 4
135 4 ++$seen{$option};
136 4 $type = $option->{'type'};
137 4 my $slot_r = $option->{'slot'};
138 4 $oldval = $$slot_r;
139 4 $$slot_r = $values->[1];
140
141 4 splice @$values,0,2;
142
143 4 DEBUG and print "Option \"$key\" = \"$$slot_r\"\n";
144
145 4 ▶ if( $type eq 'HELP' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "HELP") 0 4 4
146 0 ▶ print '', (length($key) == 1)
Branch: 1 branch
BranchTrueFalseTotal
length $key == 1 ? : 0 0 0
147 ? $self->short_help_message : $self->long_help_message;
148 0 exit;
149 }
150
151 4 ▶ if( $seen{$option} > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$option} > 1) 0 4 4
152 print "Duplicate setting for option ",
153 0 join('/', grep defined($_), @$option{'short', 'long'}),
154 ": \"$oldval\" and \"$$slot_r\".\n";
155 0 exit;
156 }
157
158 4 ▶ if( $type eq 'yes_no' ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($type eq 'yes_no') { } 2 2 4
elsif ($type eq 'string') { } 0 2 2
elsif ($type eq 'new_file') { } 0 2 2
elsif ($type eq 'file') { } 0 2 2
elsif ($type eq 'choose') { } 2 0 2
159 2 DEBUG > 1 and print "(Type $type needs no checking.)\n";
160
161 } elsif( $type eq 'string' ) {
162 0 DEBUG > 1 and print "(Type $type needs no checking.)\n";
163
164 } elsif( $type eq 'new_file' ) {
165 0 ▶ if(!length $$slot_r) {
Branch: 1 branch
BranchTrueFalseTotal
if (not length $$slot_r) { } 0 0 0
166 #die "Option $key can't take \"\" as a value" unless length $$slot_r;
167 # No, it's okay to set this to null.
168 } else {
169 # Any further checking?
170 }
171
172 } elsif( $type eq 'file' ) {
173 0 ▶ if(!length $$slot_r) {
Branch: 1 branch
BranchTrueFalseTotal
if (not length $$slot_r) { } 0 0 0
174 #die "Option $key can't take \"\" as a value" unless length $$slot_r;
175 # No, it's okay to set this to null.
176 } else {
177 0 ▶ -e $$slot_r or die "Setting to a non-existent file in $key=$$slot_r\n";
Branch: 1 branch
BranchTrueFalseTotal
-e $$slot_r or die "Setting to a non-existent file in $key=$$slot_r\n" 0 0 0
178 0 ▶ -d _ and die "Setting to a directory in $key=$$slot_r\n";
Branch: 1 branch
BranchTrueFalseTotal
-d _ and die "Setting to a directory in $key=$$slot_r\n" 0 0 0
179 0 ▶ -f _ or die "Setting to a non-file in $key=$$slot_r\n";
Branch: 1 branch
BranchTrueFalseTotal
-f _ or die "Setting to a non-file in $key=$$slot_r\n" 0 0 0
180 0 ▶ -r _ or die "Setting to an unreadable file in $key=$$slot_r\n";
Branch: 1 branch
BranchTrueFalseTotal
-r _ or die "Setting to an unreadable file in $key=$$slot_r\n" 0 0 0
181 0 DEBUG > 1 and print "File $$slot_r checks out.\n";
182 }
183
184 } elsif( $type eq 'choose' ) {
185 2 ▶ if( grep $_ eq $$slot_r, @{$option->{'from'}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep(($_ eq $$slot_r), @{$$option{'from'};})) { } 2 0 2
186 2 DEBUG > 1 and print "Choice $$slot_r checks out.\n";
187 } else {
188 die(
189 "Option $key=$$slot_r needs to be one of: [" .
190 0 join( '|', @{$option->{'from'}}) . "]\n"
191 );
192 }
193
194 } else {
195 0 DEBUG and print "I don't know how to check an option of type $type\n";
196 }
197
198 }
199
200
201
202 2 return;
203 }
204
205 # ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
206 4 1;
207
208 __END__
« Janus/SessionBase.pm ↑ Summary Janus/Facade.pm »