blib/lib/Getopt/Casual.pm

filter: Statementstmt 93.3% 28 / 30 Branchbran 86.6% 26 / 30 Conditioncond 88.8% 8 / 9 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 5 / 5 Podpod 0.0% 0 / 2 totaltotal 87.6% 71 / 81 CC 19 SCAR 22.6
CC
19
Cov
90%
casual
12.0
clean_argv
9.3
BEGIN
1.0
CRAP 9.6
↑ Summary
1 package Getopt::Casual;
2  
3 =pod
4  
5 =head1 NAME
6  
7 Getopt::Casual - A casual replacement for other Getopt modules and C<-s>.
8  
9 =head1 SYNOPSIS
10  
11 use Getopt::Casual;
12  
13 print $_, ' = ', $ARGV{ $_ }, "\n" for keys %ARGV
14 if $ARGV{ '--demo' };
15  
16 (see F<example.pl>)
17  
18 #-- Using import() to create casual defaults.
19  
20 perl C<->e 'use Getopt::Casual qw/ --debug=2 C<-l> C<-t> /;
21 print "$_ = $ARGV{ $_ }\n" for keys %ARGV' C<-t> foo
22  
23 --debug = 2
24 -t = foo
25 -l = 1
26  
27 =head1 DESCRIPTION
28  
29 The Getopt::Casual module simplifies the manipulation of command line
30 arguments in what should be a familiar way to most UNIX command line
31 utility users. The following basic rules explain the assumptions that
32 the C<&casual()> makes for either C<&import()> or C<@ARGV> command
33 line processing:
34  
35 1) Arguments can be single characters or and combination of
36 characters, although depending on your shell, some characters will
37 be interpreted by the shell.
38  
39 2) Arguments that begin with a '-' followed by another item in
40 @ARGV, which can include spaces if the string is enclosed by
41 quotes or double quotes, will have the value of that string.
42 See Rule 3.
43  
44 3) Arguments that begin with a '-' followed by another argument in
45 @ARGV that begins with a '-', including quoted strings that
46 contain spaces, will have a value of 1.
47  
48 4) Arguments that do not begin with a '-' will have a value of one.
49 When preceded by an odd number of arguments that begin with a dash,
50 this string is a value of the previous command line argument.
51  
52 5) Arguments that begin with a '--' have a value of one. (See Rule 7)
53  
54 6) The string '--' will terminate command line processing.
55  
56 7) If the string contains an '=', the part of the string preceding
57 the first '=' will be a key of %ARGV and the value will be the
58 part following the first '=' until the end of that element of @ARGV.
59  
60 8) All arguments of the script can be found as either a key or a
61 value of %ARGV.
62  
63 9) @ARGV will contain only the arguments that meet the following criteria.
64  
65 a) All arguments after a '--' will be contained in @ARGV unless
66 one of the next two criteria are met first.
67  
68 b) All arguments after the last occurance of an argument that
69 begins with a '-' and that arguments value.
70  
71 -OR-
72  
73 All arguments after the last occurance of an argument that
74 contains an '='.
75  
76 The same set of rules apply to the arguments you pass the import()
77 subroutine.
78  
79 =head1 EXAMPLES
80  
81 See the included program called F<example.pl>.
82  
83 =head1 BUGS
84  
85 If you find one, please tell me or supply a patch.
86  
87 =cut
88  
89 3 ▶use strict qw/ vars subs /;
Subroutine BEGIN: called CC=1 SCAR=0.0
90 3 ▶use vars qw/ $VERSION @ISA /;
Subroutine BEGIN: called CC=1 SCAR=0.0
91  
92 #-- $Id: Casual.pm,v 1.3 2001/04/12 20:45:37 daniel Exp $
93 3 $VERSION = "0.13.1";
94  
95 3 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
96  
97 3 my $self = shift;
98  
99 3 &casual( @_ );
100 3 &casual( @ARGV );
101 3 &clean_argv;
102  
103 };
104  
105 6 ▶sub casual {
Subroutine casual: called CC=12 SCAR=24.9
Pod undocumented
106  
107 #-- $i: Points to the position in the array we are currently at.
108 #-- The benefit of this type of for loop is that we can point
109 #-- $i out of sequence.
110 #-- $_: Used for regexps like /^-/.
111 #-- $next: Points to the next element in the array, not the
112 #-- position. Used for forward look ups.
113 6 ▶ for (my $i = 0; $_ = $_[ $i ], my $next = $_[ $i + 1 ], $i < @_; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ = $_[$i], my $next = $_[$i + 1], $i < @_) 40 6 46
114  
115 #-- $skip: If $_ begin with a '-' and/or $next is digits, then
116 #-- we will assume that $next is the value of $ARGV{ $_ }.
117 40 my $skip;
118  
119 #-- $dash: There just has to be a better way to do this.
120 #-- If $_ begins with a -, return '-', else return ''.
121 40 my ($dash) = /^(-|)/;
122  
123 #-- If there is an equals sign in the argument, it is assumed that
124 #-- anything before the first equals sign is the key and anything
125 #-- after is the value.
126 40 ▶ next if s/^([^=]+)=(.*)/$ARGV{ $1 } = $2/e;
Branch: 1 branch
BranchTrueFalseTotal
if s/^([^=]+)=(.*)/$ARGV{$1} = $2;/e 6 34 40
127  
128 #-- Stop processing arguments.
129 34 ▶ last if /^--$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^--$/ 0 34 34
130  
131 34 ▶ $ARGV{ $_[ do { s#([^-])#$ARGV{ $dash . $1 } = 1
Branch: 4 branches
BranchTrueFalseTotal
defined $next ? : 29 5 34
/^--/ ? : 3 26 29
$next =~ /^-/ || !/^-/ && $next =~ /\D/ ? : 17 9 26
unless /^--/ 29 5 34
Condition100%6/6
$next =~ /^-/ or not /^-/ and $next =~ /\D/
l!l&&r!l&&!r
1259
not /^-/ and $next =~ /\D/
!ll&&!rl&&r
545
MC/DC100%3/3
$next =~ /^-/ or not /^-/ and $next =~ /\D/
$next =~ /^-/ not /^-/ $next =~ /\D/
Truth table100%4/4
A or not(B) and C
A= $next =~ /^-/B= not /^-/C= $next =~ /\D/
ABCresult
1XX1
0111
00X0
0100
132 94 ▶ unless exists $ARGV{ $dash . $1 }#eg unless /^--/; $i } ] } =
Branch: 1 branch
BranchTrueFalseTotal
unless exists $ARGV{$dash . $1} 34 60 94
133 defined $next ? /^--/ ? 1 : $next =~ /^-/ ||
134 (!/^-/ && $next =~ /\D/) ? 1 : ($skip = $next) : 1;
135  
136 #-- Go to next key if the next value is actually a value.
137 34 ▶ $i++ if defined $skip;
Branch: 1 branch
BranchTrueFalseTotal
if defined $skip 9 25 34
138  
139 }
140  
141 }
142  
143 3 ▶sub clean_argv {
Subroutine clean_argv: called CC=8 SCAR=22.3
Pod undocumented
144  
145 #-- RJK: No script options should remain in @ARGV.
146 3 ▶ while (@ARGV) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV) 5 1 6
147  
148 #-- Stop shifting off of @ARGV when '--' is found.
149 5 ▶ if ($ARGV[ 0 ] eq '--') {
Branch: 3 branches
BranchTrueFalseTotal
if ($ARGV[0] eq '--') { } 0 5 5
elsif ($ARGV[0] =~ /=/ or $ARGV[0] =~ /^--/) { } 1 4 5
elsif ($ARGV[0] =~ /^-/) { } 2 2 4
150 0 shift @ARGV;
151 0 last;
152  
153 #-- If there is an /=/ or a /^--/, then only this argument should
154 #-- be removed from @ARGV.
155 ▶ } elsif ($ARGV[ 0 ] =~ /=/ || $ARGV[ 0 ] =~ /^--/) {
Condition66%2/3
$ARGV[0] =~ /=/ or $ARGV[0] =~ /^--/
l!l&&r!l&&!r
014
MC/DC50%1/2
$ARGV[0] =~ /=/ or $ARGV[0] =~ /^--/
$ARGV[0] =~ /=/ $ARGV[0] =~ /^--/
Truth table66%2/3
A or B
A= $ARGV[0] =~ /=/B= $ARGV[0] =~ /^--/
ABresult
1X1
011
000
156 1 shift @ARGV;
157  
158 #-- If the argument begins with /^-/, remove the argument and
159 #-- any values if they are found.
160 } elsif ($ARGV[ 0 ] =~ /^-/) {
161 2 ▶ defined $ARGV[ 1 ] ? $ARGV[ 1 ] =~ /^-/ ? shift @ARGV :
Branch: 2 branches
BranchTrueFalseTotal
defined $ARGV[1] ? : 2 0 2
$ARGV[1] =~ /^-/ ? : 0 2 2
162 splice @ARGV, 0, 2 : shift @ARGV;
163  
164 #-- If none of these conditions are met, stop, the rest of the
165 #-- items in @ARGV are arguments.
166 } else {
167 2 last;
168 }
169  
170 }
171  
172 }
173  
174  
175 =pod
176  
177 =head1 SEE ALSO
178  
179 L<Getopt::Std>, L<Getopt::Long>
180  
181 =head1 NOTES
182  
183 There has been some doubt as to whether or not this was useful enough
184 to have to remember to tote it with you to every system on which you
185 had command line perl scripts. The obvious advantage of the core
186 modules is that they are wherever perl is installed. If portablity
187 is really a key issue, use the core modules.
188  
189 =head2 AUTHOR
190  
191 Daniel M. Lipton <photo@tiac.net>
192  
193 =head2 Contributors
194  
195 Patrick M. Jordan <patrick@antistatic.com>
196 Ronald J. Kimball <rjk@linguist.dartmouth.edu>
197 Andrew N. Hicox <Andrew N. Hicox>
198  
199 =cut
200  
201 3 1;
↑ Summary