line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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) |
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. |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=head1 BUGS |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
If you find one, please tell me or supply a patch. |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=cut |
88
|
|
|
|
|
|
|
|
89
|
3
|
|
|
3
|
|
2027
|
use strict qw/ vars subs /; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
109
|
|
90
|
3
|
|
|
3
|
|
15
|
use vars qw/ $VERSION @ISA /; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
1729
|
|
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
#-- $Id: Casual.pm,v 1.3 2001/04/12 20:45:37 daniel Exp $ |
93
|
|
|
|
|
|
|
$VERSION = "0.13.1"; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub import { |
96
|
|
|
|
|
|
|
|
97
|
3
|
|
|
3
|
|
22
|
my $self = shift; |
98
|
|
|
|
|
|
|
|
99
|
3
|
|
|
|
|
9
|
&casual( @_ ); |
100
|
3
|
|
|
|
|
10
|
&casual( @ARGV ); |
101
|
3
|
|
|
|
|
11
|
&clean_argv; |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
}; |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
sub casual { |
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
|
|
|
6
|
0
|
28
|
for (my $i = 0; $_ = $_[ $i ], my $next = $_[ $i + 1 ], $i < @_; $i++) { |
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
|
|
|
|
|
38
|
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
|
|
|
|
|
111
|
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
|
100
|
|
|
|
99
|
next if s/^([^=]+)=(.*)/$ARGV{ $1 } = $2/e; |
|
6
|
|
|
|
|
44
|
|
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
#-- Stop processing arguments. |
129
|
34
|
50
|
|
|
|
61
|
last if /^--$/; |
130
|
|
|
|
|
|
|
|
131
|
34
|
100
|
66
|
|
|
181
|
$ARGV{ $_[ do { s#([^-])#$ARGV{ $dash . $1 } = 1 |
|
34
|
100
|
|
|
|
125
|
|
|
94
|
100
|
|
|
|
478
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
132
|
34
|
|
|
|
|
77
|
unless exists $ARGV{ $dash . $1 }#eg unless /^--/; $i } ] } = |
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
|
100
|
|
|
|
156
|
$i++ if defined $skip; |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
sub clean_argv { |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
#-- RJK: No script options should remain in @ARGV. |
146
|
3
|
|
|
3
|
0
|
1737
|
while (@ARGV) { |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
#-- Stop shifting off of @ARGV when '--' is found. |
149
|
5
|
50
|
66
|
|
|
44
|
if ($ARGV[ 0 ] eq '--') { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
150
|
0
|
|
|
|
|
0
|
shift @ARGV; |
151
|
0
|
|
|
|
|
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 ] =~ /^--/) { |
156
|
1
|
|
|
|
|
3
|
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
|
50
|
|
|
|
18
|
defined $ARGV[ 1 ] ? $ARGV[ 1 ] =~ /^-/ ? shift @ARGV : |
|
|
50
|
|
|
|
|
|
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
|
|
|
|
|
8
|
last; |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=pod |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=head1 SEE ALSO |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
L, L |
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 |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=head2 Contributors |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
Patrick M. Jordan |
196
|
|
|
|
|
|
|
Ronald J. Kimball |
197
|
|
|
|
|
|
|
Andrew N. Hicox |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=cut |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
1; |