Usul/Getopt/Usage.pm

filter: Statementstmt 86.7% 105 / 121 Branchbran 57.4% 31 / 54 Conditioncond 44.0% 11 / 25 MC/DCmcdc 7.1% 1 / 14 Subroutinesub 100% 13 / 13 Podpod 100% 2 / 2 totaltotal 71.1% 163 / 229 CC 40 SCAR 22.7
CC
40
Cov
74%
__ANON__
15.5
option_text
14.8
__ANON__
13.6
CRAP 9.7
« Usul/File.pm ↑ Summary Usul/TraitFor/OutputLogging.pm »
1 package Class::Usul::Getopt::Usage;
2  
3 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 18 ▶use parent 'Getopt::Long::Descriptive::Usage';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 18 ▶use List::Util qw( max );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 18 ▶use Term::ANSIColor qw( color );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 18 my $NUL = q(); my $SPC = q( ); my $USAGE_CONF = {};
11  
12 # Private functions
13 26 ▶my $_tabstop = sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
14 26 ▶ my $v = $USAGE_CONF->{tabstop} // 3; return $v; # Eight is too much
Condition50%1/2
$USAGE_CONF->{"tabstop"} // 3
l!l
026
MC/DC0%0/1
$USAGE_CONF->{"tabstop"} // 3
$USAGE_CONF->{"tabstop"}
Truth table50%1/2
A
A= $USAGE_CONF->{"tabstop"}
Aresult
11
00
15 18 };
16  
17 24 ▶my $_split_description = sub {
Subroutine __ANON__: called CC=5 SCAR=26.1
18 24 ▶ my ($length, $desc) = @_; my $width = $USAGE_CONF->{width} // 78;
Condition50%1/2
$USAGE_CONF->{"width"} // 78
l!l
024
MC/DC0%0/1
$USAGE_CONF->{"width"} // 78
$USAGE_CONF->{"width"}
Truth table50%1/2
A
A= $USAGE_CONF->{"width"}
Aresult
11
00
19 # Length of a tab plus 2 for the space between option & desc;
20 24 my $max_length = $width - ( $_tabstop->() + $length + 2 );
21  
22 0 ▶ length $desc <= $max_length and return $desc; my @lines;
Branch: 1 branch
BranchTrueFalseTotal
length $desc <= $max_length and return $desc 24 0 24
23  
24 0 ▶ while (length $desc > $max_length) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $desc > $max_length) 0 0 0
25 0 my $idx = rindex( substr( $desc, 0, $max_length ), $SPC );
26  
27 0 ▶ $idx >= 0 or last;
Branch: 1 branch
BranchTrueFalseTotal
$idx >= 0 or last 0 0 0
28  
29 0 push @lines, substr $desc, 0, $idx; substr( $desc, 0, 1 + $idx ) = $NUL;
30 }
31  
32 0 push @lines, $desc;
33 0 return @lines;
34 18 };
35  
36 24 ▶my $_types = sub {
Subroutine __ANON__: called CC=6 SCAR=19.9
37 24 ▶ my $k = shift; my $option_type = $USAGE_CONF->{option_type} // 'short';
Condition50%1/2
$USAGE_CONF->{"option_type"} // "short"
l!l
024
MC/DC0%0/1
$USAGE_CONF->{"option_type"} // "short"
$USAGE_CONF->{"option_type"}
Truth table50%1/2
A
A= $USAGE_CONF->{"option_type"}
Aresult
11
00
38  
39 24 ▶ $option_type eq 'none' and return; # Old behaviour
Branch: 1 branch
BranchTrueFalseTotal
$option_type eq "none" and return 0 24 24
40 24 ▶ $option_type eq 'verbose' and return uc $k; # New behaviour
Branch: 1 branch
BranchTrueFalseTotal
$option_type eq "verbose" and return uc $k 0 24 24
41  
42 my $types = $USAGE_CONF->{type_map}
43 24 ▶ // { int => 'i', key => 'k', num => 'n', str => 's', };
Condition50%1/2
$USAGE_CONF->{"type_map"} // {"int", "i", "key", "k", "num", "n", "str", "s"}
l!l
024
MC/DC0%0/1
$USAGE_CONF->{"type_map"} // {"int", "i", "key", "k", "num", "n", "str", "s"}
$USAGE_CONF->{"type_map"}
Truth table50%1/2
A
A= $USAGE_CONF->{"type_map"}
Aresult
11
00
44 24 ▶ my $type = $types->{ $k } // $NUL; # Prefered behaviour
Condition33%1/3
$types->{$k} // $NUL
l!l&&r!l&&!r
2400
MC/DC0%0/2
$types->{$k} // $NUL
$types->{$k} $NUL
Truth table33%1/3
A // B
A= $types->{$k}B= $NUL
ABresult
1X1
011
000
45  
46 24 return $type;
47 18 };
48  
49 48 ▶my $_parse_assignment = sub {
Subroutine __ANON__: called CC=12 SCAR=27.4
50 48 ▶ my $assign_spec = shift; $assign_spec or return $NUL;
Branch: 1 branch
BranchTrueFalseTotal
$assign_spec or return $NUL 32 16 48
51  
52 32 ▶ length $assign_spec < 2 and return $NUL; # Empty, ! or +
Branch: 1 branch
BranchTrueFalseTotal
length $assign_spec < 2 and return $NUL 12 20 32
53  
54 20 my $argument = substr $assign_spec, 1, 2;
55 20 my $result = $_types->( 'str' );
56  
57 20 ▶ if ($argument eq 'i' or $argument eq 'o') { $result = $_types->( 'int' ) }
Branch: 2 branches
BranchTrueFalseTotal
if ($argument eq 'i' or $argument eq 'o') { } 4 16 20
elsif ($argument eq 'f') { } 0 16 16
Condition66%2/3
$argument eq "i" or $argument eq "o"
l!l&&r!l&&!r
4016
MC/DC50%1/2
$argument eq "i" or $argument eq "o"
$argument eq "i" $argument eq "o"
Truth table66%2/3
A or B
A= $argument eq "i"B= $argument eq "o"
ABresult
1X1
011
000
58 0 elsif ($argument eq 'f') { $result = $_types->( 'num' ) }
59  
60 20 ▶ if (length $assign_spec > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $assign_spec > 2) 4 16 20
61 4 my $desttype = substr $assign_spec, 2, 1;
62  
63 # Imply it can be repeated
64 4 ▶ if ($desttype eq '@') { $result .= '...' }
Branch: 2 branches
BranchTrueFalseTotal
if ($desttype eq '@') { } 4 0 4
elsif ($desttype eq '%') { } 0 0 0
65 elsif ($desttype eq '%') {
66 0 ▶ $result = $result ? $_types->( 'key' )."=${result}..." : $NUL;
Branch: 1 branch
BranchTrueFalseTotal
$result ? : 0 0 0
67 }
68 }
69  
70 20 ▶ substr $assign_spec, 0, 1 eq ':' and return "[=${result}]";
Branch: 1 branch
BranchTrueFalseTotal
and return "[=$result]" 0 20 20
71 # With leading space so it can just blindly be appended.
72 20 ▶ return $result ? " $result" : $NUL;
Branch: 1 branch
BranchTrueFalseTotal
$result ? : 20 0 20
73 18 };
74  
75 24 ▶my $_assemble_spec = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
76 24 my ($length, $spec) = @_;
77  
78 24 my $stripped = [ Getopt::Long::Descriptive->_strip_assignment( $spec ) ];
79 24 my $assign = $_parse_assignment->( $stripped->[ 1 ] );
80 my $plain = join $SPC, reverse
81 24 ▶ map { length > 1 ? "--${_}${assign}" : "-${_}${assign}" }
Branch: 1 branch
BranchTrueFalseTotal
length $_ > 1 ? : 24 2 26
82 split m{ [|] }mx, $stripped->[ 0 ];
83 24 my $pad = $SPC x ($length - length $plain);
84 24 ▶ my $highlight = $USAGE_CONF->{highlight} // 'bold';
Condition50%1/2
$USAGE_CONF->{"highlight"} // "bold"
l!l
024
MC/DC0%0/1
$USAGE_CONF->{"highlight"} // "bold"
$USAGE_CONF->{"highlight"}
Truth table50%1/2
A
A= $USAGE_CONF->{"highlight"}
Aresult
11
00
85  
86 24 ▶ $highlight eq 'none' and return $plain.$pad; # Old behaviour
Branch: 1 branch
BranchTrueFalseTotal
$highlight eq "none" and return $plain . $pad 0 24 24
87  
88 24 $assign = color( $highlight ).$assign.color( 'reset' );
89  
90 my $markedup = join $SPC, reverse
91 24 ▶ map { length > 1 ? "--${_}${assign}" : "-${_}${assign}" }
Branch: 1 branch
BranchTrueFalseTotal
length $_ > 1 ? : 24 2 26
92 split m{ [|] }mx, $stripped->[ 0 ];
93  
94 24 return $markedup.$pad; # Prefered behaviour works well with short types
95 18 };
96  
97 24 ▶my $_option_length = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
98 24 my $fullspec = shift;
99 24 my $number_opts = 1;
100 24 my $last_pos = 0;
101 24 my $number_shortopts = 0;
102 24 my ($spec, $assign)
103 = Getopt::Long::Descriptive->_strip_assignment( $fullspec );
104 24 my $length = length $spec;
105 24 my $arglen = length $_parse_assignment->( $assign );
106 # Spacing rules:
107 # For short options we want 1 space (for '-'), for long options 2
108 # spaces (for '--'). Then one space for separating the options,
109 # but we here abuse that $spec has a '|' char for that.
110  
111 # For options that take arguments, we want 2 spaces for mandatory
112 # options ('=X') and 4 for optional arguments ('[=X]'). Note we
113 # consider {N,M} cases as "single argument" atm.
114  
115 # Count the number of "variants" (e.g. "long|s" has two variants)
116 24 ▶ while ($spec =~ m{ [|] }gmx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec =~ / [|] /gmx) 2 24 26
117 2 $number_opts++;
118 2 ▶ (pos( $spec ) - $last_pos) == 2 and $number_shortopts++;
Branch: 1 branch
BranchTrueFalseTotal
pos($spec) - $last_pos == 2 and ++$number_shortopts 0 2 2
119 2 $last_pos = pos( $spec );
120 }
121  
122 # Was the last option a "short" one?
123 # Getopt::Long::Descriptive has a 2 here and thats wrong
124 24 ▶ ($length - $last_pos) == 1 and $number_shortopts++;
Branch: 1 branch
BranchTrueFalseTotal
$length - $last_pos == 1 and ++$number_shortopts 2 22 24
125 # We got $number_opts options, each with an argument length of
126 # $arglen. Plus each option (after the first) needs 3 a char
127 # spacing. $length gives us the total length of all options and 1
128 # char spacing per option (after the first). It does not account
129 # for argument length and we want (at least) one additional char
130 # for space before the description. So the result should be:
131 24 my $number_longopts = $number_opts - $number_shortopts;
132 24 my $total_arglen = $number_opts * $arglen;
133 24 my $total_optsep = 2 * $number_longopts + $number_shortopts;
134 24 my $total = $length + $total_optsep + $total_arglen;
135  
136 24 return $total;
137 18 };
138  
139 # Public methods
140 2 ▶sub option_text {
Subroutine option_text: called CC=10 SCAR=27.0
Pod documented
141 2 my $self = shift;
142 2 ▶ my @options = @{ $self->{options} // [] };
Condition50%1/2
$self->{"options"} // []
l!l
20
MC/DC0%0/1
$self->{"options"} // []
$self->{"options"}
Truth table50%1/2
A
A= $self->{"options"}
Aresult
11
00
143 2 my @specs = map { $_->{spec} } grep { $_->{desc} ne 'spacer' } @options;
144 2 ▶ my $length = max( map { $_option_length->( $_ ) } @specs ) || 0;
Condition50%1/2
&max(map({&$_option_length($_);} @specs)) || 0
l!l
20
MC/DC0%0/1
&max(map({&$_option_length($_);} @specs)) || 0
&max(map({&$_option_length($_);} @specs))
Truth table50%1/2
A
A= &max(map({&$_option_length($_);} @specs))
Aresult
11
00
145 2 my $tab = $SPC x $_tabstop->(); # Originally an actual tab char
146 2 my $spec_fmt = "${tab}%-${length}s";
147 2 my $string = $NUL;
148  
149 2 ▶ while (defined (my $opt = shift @options)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $opt = shift @options)) 24 2 26
150 24 my $spec = $opt->{spec}; my $desc = $opt->{desc};
151  
152 0 ▶ if ($desc eq 'spacer') { $string .= sprintf "${spec_fmt}\n", $spec; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($desc eq "spacer") 0 24 24
153  
154 24 ▶ if (exists $opt->{constraint}->{default} and $self->{show_defaults}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt->{"constraint"}{"default"} and $self->{"show_defaults"}) 0 24 24
Condition33%1/3
exists $opt->{"constraint"}{"default"} and $self->{"show_defaults"}
!ll&&!rl&&r
2400
MC/DC0%0/2
exists $opt->{"constraint"}{"default"} and $self->{"show_defaults"}
exists $opt->{"constraint"}{"default"} $self->{"show_defaults"}
Truth table33%1/3
A and B
A= exists $opt->{"constraint"}{"default"}B= $self->{"show_defaults"}
ABresult
0X0
100
111
155 0 ▶ my $default = $opt->{constraint}->{default} // '[undef]';
Condition0%0/2
$opt->{"constraint"}{"default"} // "[undef]"
l!l
00
MC/DC0%0/1
$opt->{"constraint"}{"default"} // "[undef]"
$opt->{"constraint"}{"default"}
Truth table0%0/2
A
A= $opt->{"constraint"}{"default"}
Aresult
11
00
156  
157 0 ▶ length $default or $default = '[null]';
Branch: 1 branch
BranchTrueFalseTotal
length $default or $default = "[null]" 0 0 0
158 # Add the default to the description before splitting into lines
159 0 $desc .= " (default value: ${default})";
160 }
161  
162 24 my @desc = $_split_description->( $length, $desc );
163  
164 24 $spec = $_assemble_spec->( $length, $spec );
165 24 $string .= sprintf "${tab}${spec} %s\n", shift @desc;
166  
167 24 for my $line (@desc) {
168 0 $string .= $tab.($SPC x ( $length + 2 ))."${line}\n";
169 }
170 }
171  
172 2 return $string;
173 }
174  
175 4 ▶sub usage_conf {
Subroutine usage_conf: called CC=3 SCAR=11.4
Pod documented
176 4 ▶ my ($self, $v) = @_; defined $v or return $USAGE_CONF;
Branch: 1 branch
BranchTrueFalseTotal
defined $v or return $USAGE_CONF 4 0 4
177  
178 4 ▶ ref $v eq 'HASH' or die 'Usage configuration must be a hash reference';
Branch: 1 branch
BranchTrueFalseTotal
ref $v eq "HASH" or die "Usage configuration must be a hash reference" 4 0 4
179  
180 4 return $USAGE_CONF = $v;
181 }
182  
183 18 1;
184  
185 __END__
« Usul/File.pm ↑ Summary Usul/TraitFor/OutputLogging.pm »