Spec/Argument.pm

filter: Statementstmt 95.0% 77 / 81 Branchbran 91.3% 42 / 46 Conditioncond 88.8% 16 / 18 MC/DCmcdc 81.8% 9 / 11 Subroutinesub 100% 5 / 5 Podpod 100% 2 / 2 totaltotal 92.6% 151 / 163 CC 32 SCAR 28.5
CC
32
Cov
93%
from_dsl
22.6
common
11.0
BEGIN
1.0
CRAP 17.3
« Spec.pm ↑ Summary Spec/Completion/Fish.pm »
1 # ABSTRACT: App::Spec objects representing command line options or parameters
2 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 package App::Spec::Argument;
5  
6 12 our $VERSION = 'v0.15.1'; # TRIAL VERSION
7  
8 12 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 12 has name => ( is => 'ro' );
11 12 has type => ( is => 'ro' );
12 12 has multiple => ( is => 'ro' );
13 12 has mapping => ( is => 'ro' );
14 12 has required => ( is => 'ro' );
15 12 has unique => ( is => 'ro' );
16 12 has summary => ( is => 'ro' );
17 12 has description => ( is => 'ro' );
18 12 has default => ( is => 'ro' );
19 12 has completion => ( is => 'ro' );
20 12 has enum => ( is => 'ro' );
21 12 has values => ( is => 'ro' );
22  
23 893 ▶sub common {
Subroutine common: called CC=11 SCAR=24.0
Pod documented
24 893 my ($class, %args) = @_;
25 893 my %dsl;
26 893 ▶ if (defined $args{spec}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"spec"}) 20 873 893
27 20 %dsl = $class->from_dsl(delete $args{spec});
28 }
29 893 my $description = $args{description};
30 893 my $summary = $args{summary};
31 893 ▶ $summary //= '';
Condition100%2/2
$summary //= ""
l!l
80588
MC/DC100%1/1
$summary //= ""
$summary
Truth table100%2/2
A
A= $summary
Aresult
11
00
32 893 ▶ $description //= '';
Condition100%2/2
$description //= ""
l!l
106787
MC/DC100%1/1
$description //= ""
$description
Truth table100%2/2
A
A= $description
Aresult
11
00
33 893 ▶ my $type = $args{type} // 'string';
Condition100%2/2
$args{"type"} // "string"
l!l
661232
MC/DC100%1/1
$args{"type"} // "string"
$args{"type"}
Truth table100%2/2
A
A= $args{"type"}
Aresult
11
00
34 my %hash = (
35 name => $args{name},
36 summary => $summary,
37 description => $description,
38 type => $type,
39 multiple => $args{multiple} ? 1 : 0,
40 mapping => $args{mapping} ? 1 : 0,
41 required => $args{required} ? 1 : 0,
42 unique => $args{unique} ? 1 : 0,
43 default => $args{default},
44 completion => $args{completion},
45 enum => $args{enum},
46 values => $args{values},
47 893 ▶ %dsl,
Branch: 4 branches
BranchTrueFalseTotal
$args{'multiple'} ? : 152 741 893
$args{'mapping'} ? : 30 863 893
$args{'required'} ? : 248 645 893
$args{'unique'} ? : 29 864 893
48 );
49 893 ▶ not defined $hash{ $_ } and delete $hash{ $_ } for keys %hash;
Branch: 1 branch
BranchTrueFalseTotal
defined $hash{$_} or delete $hash{$_} 7707 3013 10720
50 893 return %hash;
51 }
52  
53 12 my $name_re = qr{[\w-]+};
54  
55 20 ▶sub from_dsl {
Subroutine from_dsl: called CC=22 SCAR=31.2
Pod documented
56 20 my ($class, $dsl) = @_;
57 20 my %hash;
58  
59 my $name;
60 20 my $type = "flag";
61 20 my $multiple;
62 20 $dsl =~ s/^\s+//;
63  
64 20 ▶ if ($dsl =~ s/^\+//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dsl =~ s/^\+//) 1 19 20
65 1 my $required = 1;
66 1 $hash{required} = $required;
67 }
68  
69 20 ▶ if ($dsl =~ s/^ ($name_re) //x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dsl =~ s/^ ($name_re) //x) { } 20 0 20
70 20 $name = $1;
71 20 $hash{name} = $name;
72 }
73 else {
74 0 die "invalid spec: '$dsl'";
75 }
76  
77 20 my @aliases;
78 20 ▶ while ($dsl =~ s/^ \| (\w) //x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dsl =~ s/^ \| (\w) //x) 4 20 24
79 4 push @aliases, $1;
80 }
81 20 ▶ if (@aliases) {
Branch: 1 branch
BranchTrueFalseTotal
if (@aliases) 4 16 20
82 4 $hash{aliases} = \@aliases;
83 }
84  
85 20 my $getopt_type = '';
86 20 ▶ if ($dsl =~ s/^=//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dsl =~ s/^=//) 7 13 20
87 # not a flag, default string
88 7 $type = "string";
89 7 ▶ if ($dsl =~ s/^([isf])//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dsl =~ s/^([isf])//) 5 2 7
90 5 $getopt_type = $1;
91 5 ▶ if ($getopt_type eq "i") {
Branch: 3 branches
BranchTrueFalseTotal
if ($getopt_type eq 'i') { } 1 4 5
elsif ($getopt_type eq 'f') { } 1 3 4
elsif ($getopt_type eq 's') { } 3 0 3
92 1 $type = "integer";
93 }
94 elsif ($getopt_type eq "f") {
95 1 $type = "float";
96 }
97 elsif ($getopt_type eq "s") {
98 }
99 else {
100 0 die "Option $name: type $getopt_type not supported";
101 }
102 }
103 }
104  
105 20 ▶ if ($type eq 'flag' and $dsl =~ s/^\+//) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'flag' and $dsl =~ s/^\+//) { } 1 19 20
elsif ($type eq 'string' and $dsl =~ s/^\@//) { } 1 18 19
elsif ($type eq 'string' and $dsl =~ s/^\%//) { } 1 31 32
Condition100%3/3
$type eq "flag" and $dsl =~ s/^\+//
!ll&&!rl&&r
7121
MC/DC100%2/2
$type eq "flag" and $dsl =~ s/^\+//
$type eq "flag" $dsl =~ s/^\+//
Truth table100%3/3
A and B
A= $type eq "flag"B= $dsl =~ s/^\+//
ABresult
0X0
100
111
106 # incremental flag
107 1 $multiple = 1;
108 1 $hash{multiple} = 1;
109 }
110 ▶ elsif ($type eq 'string' and $dsl =~ s/^\@//) {
Condition100%3/3
$type eq "string" and $dsl =~ s/^\@//
!ll&&!rl&&r
1441
MC/DC100%2/2
$type eq "string" and $dsl =~ s/^\@//
$type eq "string" $dsl =~ s/^\@//
Truth table100%3/3
A and B
A= $type eq "string"B= $dsl =~ s/^\@//
ABresult
0X0
100
111
111 1 $hash{multiple} = 1;
112 }
113 ▶ elsif ($type eq 'string' and $dsl =~ s/^\%//) {
Condition100%3/3
$type eq "string" and $dsl =~ s/^\%//
!ll&&!rl&&r
1431
MC/DC100%2/2
$type eq "string" and $dsl =~ s/^\%//
$type eq "string" $dsl =~ s/^\%//
Truth table100%3/3
A and B
A= $type eq "string"B= $dsl =~ s/^\%//
ABresult
0X0
100
111
114 1 $hash{multiple} = 1;
115 1 $hash{mapping} = 1;
116 }
117  
118 20 $dsl =~ s/^\s+//;
119  
120 20 ▶ while ($dsl =~ s/^\s*([=+])(\S+)//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dsl =~ s/^\s*([=+])(\S+)//) 3 20 23
121 3 ▶ if ($1 eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq '+') { } 2 1 3
122 2 $type = $2;
123 2 ▶ if ($getopt_type and $type ne $getopt_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($getopt_type and $type ne $getopt_type) 0 2 2
Condition33%1/3
$getopt_type and $type ne $getopt_type
!ll&&!rl&&r
200
MC/DC0%0/2
$getopt_type and $type ne $getopt_type
$getopt_type $type ne $getopt_type
Truth table33%1/3
A and B
A= $getopt_typeB= $type ne $getopt_type
ABresult
0X0
100
111
124 0 die "Explicit type '$type' conflicts with getopt type '$getopt_type'";
125 }
126 }
127 else {
128 1 $hash{default} = $2;
129 }
130 }
131  
132 20 ▶ if ($dsl =~ s/^\s*--\s*(.*)//s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dsl =~ s/^\s*--\s*(.*)//s) 13 7 20
133 # TODO only summary should be supported
134 13 $hash{summary} = $1;
135 }
136  
137 20 ▶ if (length $dsl) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $dsl) 0 20 20
138 0 die "Invalid spec: trailing '$dsl'";
139 }
140  
141 20 $hash{type} = $type;
142 20 return %hash;
143 }
144  
145 12 1;
146  
147 =pod
148  
149 =head1 NAME
150  
151 App::Spec::Argument - App::Spec objects representing command line options or parameters
152  
153 =head1 SYNOPSIS
154  
155 =head1 EXAMPLES
156  
157 Options can be defined in a verbose way via key value pairs, but you can also
158 use a shorter syntax.
159  
160 The idea comes from Ingy's L<http://www.schematype.org/>.
161  
162 The first item of the string is the name of the option using a syntax
163 very similar to the one from L<Getopt::Long>.
164  
165 Then you can optionally define a type, a default value and a summary.
166  
167 You can see a list of supported syntax in this example from C<t/data/12.dsl.yaml>:
168  
169 =for comment
170 START INLINE t/data/12.dsl.yaml
171  
172 ---
173 # version with short dsl syntax
174 name: myapp
175 appspec: { version: 0.001 }
176 class: App::Spec::Example::MyApp
177 title: My Very Cool App
178 options:
179 - spec: foo --Foo
180 - spec: verbose|v+ --be verbose
181 - spec: +req --Some required flag
182 - spec: number=i --integer option
183 - spec: number2|n= +integer --integer option
184 - spec: fnumber=f --float option
185 - spec: fnumber2|f= +float --float option
186 - spec: date|d=s =today
187 - spec: items=s@ --multi option
188 - spec: set=s% --multiple key=value pairs
189
190 ---
191 # version with verbose syntax
192 name: myapp
193 appspec: { version: 0.001 }
194 class: App::Spec::Example::MyApp
195 title: My Very Cool App
196 options:
197 - name: foo
198 type: flag
199 summary: Foo
200 - name: verbose
201 summary: be verbose
202 type: flag
203 multiple: true
204 aliases: ["v"]
205 - name: req
206 summary: Some required flag
207 required: true
208 type: flag
209 - name: number
210 summary: integer option
211 type: integer
212 - name: number2
213 summary: integer option
214 type: integer
215 aliases: ["n"]
216 - name: fnumber
217 summary: float option
218 type: float
219 - name: fnumber2
220 summary: float option
221 type: float
222 aliases: ["f"]
223 - name: date
224 type: string
225 default: today
226 aliases: ["d"]
227 - name: items
228 type: string
229 multiple: true
230 summary: multi option
231 - name: set
232 type: string
233 multiple: true
234 mapping: true
235 summary: multiple key=value pairs
236
237  
238  
239 =for comment
240 STOP INLINE
241  
242 =head1 METHODS
243  
244 =over 4
245  
246 =item common
247  
248 Builds a hash with the given hashref and fills in some defaults.
249  
250 my %hash = $class->common($args);
251  
252 =item from_dsl
253  
254 Builds a hash from the dsl string
255  
256 %dsl = $class->from_dsl("verbose|v+ --Be verbose");
257  
258  
259 =item name, type, multiple, required, unique, summary, description, default, completion, enum, values, mapping
260  
261 Attributes which represent the ones from the spec.
262  
263 =back
264  
265 =cut
« Spec.pm ↑ Summary Spec/Completion/Fish.pm »