blib/lib/Getopt/Easy.pm

filter: Statementstmt 95.0% 57 / 60 Branchbran 92.1% 35 / 38 Conditioncond 87.5% 7 / 8 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 4 / 4 Podpod 0.0% 0 / 1 totaltotal 92.2% 107 / 116 CC 24 SCAR 30.8
CC
24
Cov
93%
get_options
24.3
BEGIN
1.0
BEGIN
1.0
CRAP 21.7
↑ Summary
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Getopt::Easy;
5 1 our $VERSION = 0.1;
6  
7 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 our @ISA = qw/Exporter/;
9 1 our %O;
10 1 our @EXPORT = qw/get_options %O/;
11  
12 8 ▶sub get_options {
Subroutine get_options: called CC=24 SCAR=31.9
Pod undocumented
13 8 my ($optstr, $usage, $helpchars) = @_;
14  
15 8 ▶ $helpchars ||= "";
Condition50%1/2
$helpchars ||= ""
l!l
08
MC/DC0%0/1
$helpchars ||= ""
$helpchars
Truth table50%1/2
A
A= $helpchars
Aresult
11
00
16 8 $optstr =~ s/^\s*//;
17 8 my $err = 0;
18 8 my (%options, %valid);
19 8 my ($l, $word);
20 8 for (split(/\s+/, $optstr)) {
21 22 ▶ ($l, $word) = /^(.)-(.*)$/
Branch: 1 branch
BranchTrueFalseTotal
() = /^(.)-(.*)$/ or die "${_}: syntax error - must be like this: l-length\n" 21 1 22
22 or die "$_: syntax error - must be like this: l-length\n";
23 #
24 # check for the = sign.
25 # there are two different uses of it
26 #
27 21 ▶ if ($word =~ s/=$//) {
Branch: 2 branches
BranchTrueFalseTotal
if ($word =~ s/=$//) { } 8 13 21
elsif ($word =~ s/=(.+)//) { } 5 8 13
28 8 $l .= '=';
29 } elsif ($word =~ s/=(.+)//) {
30 5 $l .= '=';
31 5 $valid{$word} = $1;
32 }
33 21 $options{$l} = $word;
34 21 ▶ $O{$word} = ($l =~ /=/)? "": 0 unless exists $O{$word};
Branch: 2 branches
BranchTrueFalseTotal
unless exists $Getopt::Easy::O{$word} 6 15 21
$l =~ /=/ ? : 4 2 6
35 }
36 7 ▶ exit if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 7 7
37 #
38 # with %options and %valid and %O and $helpchars initialized properly
39 # we are now ready to examine @ARGV
40 #
41 7 my ($arg, $let, $val);
42 7 $err = "";
43 7 ▶ ARGV: while (@ARGV and $ARGV[0] =~ s/^-//) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV and $ARGV[0] =~ s/^-//) 12 6 18
Condition100%3/3
@ARGV and $ARGV[0] =~ s/^-//
!ll&&!rl&&r
5112
MC/DC100%2/2
@ARGV and $ARGV[0] =~ s/^-//
@ARGV $ARGV[0] =~ s/^-//
Truth table100%3/3
A and B
A= @ARGVB= $ARGV[0] =~ s/^-//
ABresult
0X0
100
111
44 12 $arg = shift @ARGV;
45 12 ▶ last if $arg eq "-"; # stop processing options
Branch: 1 branch
BranchTrueFalseTotal
if $arg eq "-" 1 11 12
46 11 ▶ while ($arg =~ s/^(.)//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ s/^(.)//) 11 9 20
47 11 $let = $1;
48 11 ▶ if (index($helpchars, $let) >= 0) { # help
Branch: 3 branches
BranchTrueFalseTotal
if (index($helpchars, $let) != -1) { } 0 11 11
elsif (exists $options{$let}) { } 2 9 11
elsif (exists $options{"$let="}) { } 7 2 9
49 0 require "Pod/Text.pm";
50 0 Pod::Text->new->parse_from_file($0);
51 0 exit;
52 } elsif (exists $options{$let}) { # boolean
53 2 $O{$options{$let}} = 1;
54 } elsif (exists $options{"$let="}) { # with value
55 7 ▶ if ($arg eq "" and not $arg = shift @ARGV) {
Branch: 2 branches
BranchTrueFalseTotal
if ($arg eq '' and not $arg = shift @ARGV) { } 1 6 7
elsif ($arg =~ /^-/) { } 1 5 6
Condition100%3/3
$arg eq "" and not $arg = shift @ARGV
!ll&&!rl&&r
241
MC/DC100%2/2
$arg eq "" and not $arg = shift @ARGV
$arg eq "" not $arg = shift @ARGV
Truth table100%3/3
A and not(B)
A= $arg eq ""B= not $arg = shift @ARGV
ABresult
0X0
100
111
56 1 $err .= "missing argument for -$let\n";
57 1 next ARGV;
58 } elsif ($arg =~ /^-/) {
59 1 $err .= "value $arg for -$let begins with a dash\n";
60 1 next ARGV;
61 } else {
62 5 $O{$options{"$let="}} = $arg;
63 5 $arg = "";
64 }
65 5 ▶ if (my $v = $valid{$options{"$let="}}) { # debugging style
Branch: 1 branch
BranchTrueFalseTotal
if (my $v = $valid{$options{"$let="}}) 2 3 5
66 #
67 # $v now contains the only valid options for -$let
68 #
69 2 my $opts = $O{$options{"$let="}};
70 2 $opts =~ s/[$v]//g; # remove the good ones
71 2 ▶ if ($opts) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts) 1 1 2
72 1 ▶ my $plural = (length($opts) > 1)? "s": "";
Branch: 1 branch
BranchTrueFalseTotal
length $opts > 1 ? : 1 0 1
73 1 $err .= "for -$let: illegal option$plural: ".
74 "$opts, valid ones are: $v\n";
75 }
76 }
77 } else {
78 2 $err .= "unknown option: -$let\n";
79 }
80 }
81 }
82 7 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 5 2 7
83 5 ▶ if ($usage) {
Branch: 1 branch
BranchTrueFalseTotal
if ($usage) 1 4 5
84 #
85 # make sure there is a newline
86 # else we'll get "at line ..."
87 #
88 1 chomp $usage;
89 1 $err .= "$usage\n";
90 }
91 5 die $err;
92 }
93 }
94  
95 1 1;
96  
97 =head1 NAME
98  
99 Getopt::Easy - parses command line options in a simple but capable way.
100  
101 =head1 SYNOPSIS
102  
103 use Getopt::Easy;
104  
105 get_options "v-verbose f-fname= D-debug=uSX",
106 "usage => "usage: prog [-v] [-f fname] [-D [uSX]] [-H]",
107 "H";
108  
109 print "reading $O{fname}\n" if $O{verbose};
110 print "SQL: $sql\n" if $O{debug} =~ /S/;
111  
112 =head1 DESCRIPTION
113  
114 Perl puts the command line parameters in the array @ARGV
115 allowing the user to examine and manipulate it like any
116 other array. There is a long tradition of putting optional
117 single character flags (preceded by a dash) in front of
118 other parameters like so:
119  
120 % ls -ltr *.h *.c
121 % tar -tvf all.tar
122 % ps -ax -U jsmith
123  
124 Many Getopt::* modules exist to help with the
125 parsing of these flags out of @ARGV.
126 For the author, Getopt::Std was visually too cryptic and
127 Getopt::Long was too large and complex for most normal applications.
128 Getopt::Easy is small, easy to understand, and provides a visual clarity.
129  
130 There are two things exported: get_options() and %O.
131  
132 get_options has 1 required parameter and 2 optional ones.
133 The first is a string describing the kind of options that
134 are expected. It is a space separated list of terms like this:
135  
136 get_options "v-verbose f-fname=";
137  
138 If the -v option is given on the command
139 line %O{verbose} will be set to 1 (true).
140 If the -f option is given then another argument is expected
141 which will be assigned to $O{fname}.
142  
143 Before parsing @ARGV, $O{verbose} will be initialized to 0 (false) and
144 $O{fname} to "" (unless they already have a value).
145  
146 If you give an unknown option get_options() will complain and exit:
147  
148 % prog -vX
149 unknown option: -X
150 %
151  
152 These conventions are implemented by Getopt::Easy:
153  
154 =over 4
155  
156 =item *
157  
158 The options can come in any order.
159  
160 =item *
161  
162 Multiple boolean options can be bundled together.
163  
164 =item *
165  
166 A command line argument of '--' will cause argument parsing to stop
167 so you can parse the rest of the options yourself.
168  
169 =item *
170  
171 Parsed arguments are removed from @ARGV.
172  
173 =back
174  
175 These invocations are equivalent:
176  
177 % prog -v -f infile
178 % prog -f infile -v # different order
179 % prog -v -finfile
180 % prog -vf infile
181 % prog -vfinfile
182  
183 This shows that the space between -f and infile is optional
184 and that you I<can> bundle -f with -v but -f must be
185 the I<last> option in the bundle.
186  
187 The optional second parameter to get_options() is
188 a usage message to be printed when an illegal option is given.
189  
190 get_options "v-verbose f-fname=",
191 "usage: prog [-v] [-f fname]";
192  
193 Now if an unknown option is given, the same
194 error message will be printed, as above, followed
195 by the usage message.
196
197 % prog -vX
198 unknown option: -X
199 usage: prog [-v] [-f fname]
200 %
201  
202 =head2 HELP
203  
204 Sometimes the usage message is not enough and the
205 user needs more detailed and elaborate help. This is
206 where the 3rd optional parameter comes in.
207  
208 get_options "v-verbose f-fname=",
209 "usage: prog [-v] [-f fname] [-H]",
210 "H";
211  
212 Giving the -H option will cause the POD for the module
213 to be echoed to STDOUT - as if the user had typed
214 'perldoc prog'. See 'perldoc perlpod'.
215  
216 =head2 DEBUGGING
217  
218 There are various ways to implement a debugging option:
219  
220 GOOD:
221  
222 get_options "d-debug";
223  
224 print "val = $val\n" if $O{debug};
225  
226 BETTER:
227  
228 get_options "d-debug=";
229  
230 print "SQL = $sql\n" if $O{debug} >= 2;
231 print "val = $val\n" if $O{debug} >= 3;
232  
233 With this method there are various I<levels> of debugging.
234 Unfortunately, they often end up ranging from
235 'not enough' to 'too much' :(.
236  
237 BEST:
238  
239 get_options "d-debug=eSvL";
240  
241 print "SQL = $sql\n" if $O{debug} =~ /S/;
242 print "val = $val\n" if $O{debug }=~ /v/;
243
244 With this kind of term the letters after the equal sign '=' are the
245 debugging options that are valid. Now the user can choose exactly
246 what kind of debugging output they wish to see.
247  
248 % prog -d SL
249  
250 Giving an illegal debugging option will
251 result in an error message:
252  
253 % prog -deXSf
254 for -d: illegal options: Xf, valid ones are: eSvL
255 %
256  
257 =head1 ACCESS ELSEWHERE
258  
259 If you want access to the %O hash from other files simply put:
260  
261 use Getopt::Easy;
262  
263 at the top of those files; the %O hash will again be exported into the
264 current package. You need to have:
265  
266 get_options ...;
267  
268 only once in the main file before anyone needs to look at the %O hash.
269  
270 =head1 STRICT
271  
272 It is easy to misspell a key for the %O hash. Tie::StrictHash
273 can help with this:
274  
275 use GetOpt::Easy;
276 use Tie::StrictHash;
277  
278 get_options "v-verbose f-fname=";
279 strict_hash %O;
280  
281 print "file name is $O{filename}\n";
282  
283 This will give a fatal error message:
284  
285 key 'filename' does not exist at prog line 6
286  
287 =head1 SEE ALSO
288  
289 Config::Easy allows configuration file entries
290 to be overidden with command line arguments.
291  
292 Tie::StrictHash protects against misspelling of key names.
293  
294 Date::Simple is an elegant way of dealing with dates.
295  
296 =head1 AUTHOR
297  
298 Jon Bjornstad <jon@icogitate.com>
↑ Summary