| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Getopt::Lazy; |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
require 5.006; |
|
4
|
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
27551
|
use version; |
|
|
1
|
|
|
|
|
3890
|
|
|
|
1
|
|
|
|
|
8
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = qv('0.0.6'); |
|
7
|
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
164
|
use strict; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
45
|
|
|
9
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
|
1
|
|
|
|
|
7
|
|
|
|
1
|
|
|
|
|
48
|
|
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 NAME |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
Getopt::Lazy - Yet another lazy, minimal way of using Getopt::Long |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
use Getopt::Lazy |
|
18
|
|
|
|
|
|
|
'help|h' => 'Show this help screen', |
|
19
|
|
|
|
|
|
|
'verbose|v' => 'Show verbose output', |
|
20
|
|
|
|
|
|
|
'output|o=s' => ["[FILE] Send the output to FILE", 'getopt.out'], |
|
21
|
|
|
|
|
|
|
'output-encoding=s' => ['[ENCODING] Specify the output encoding', 'utf8'], |
|
22
|
|
|
|
|
|
|
-summary => 'a simple example usage of Getopt::Lazy', |
|
23
|
|
|
|
|
|
|
-usage => '%c %o file1 [file2 ..]', |
|
24
|
|
|
|
|
|
|
; |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
getopt; |
|
27
|
|
|
|
|
|
|
print usage and exit 1 unless @ARGV; |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
Getting tired of the digging the same tedious "getopt" things for every script |
|
32
|
|
|
|
|
|
|
that you write? This module works for you! |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=head2 Without Getopt::Lazy |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
Normally we started a script this way: |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
use File::Basename; |
|
39
|
|
|
|
|
|
|
use Getopt::Long; |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub usage { |
|
42
|
|
|
|
|
|
|
my $msg = shift; |
|
43
|
|
|
|
|
|
|
my $cmd = basename $0; |
|
44
|
|
|
|
|
|
|
print $msg, "\n" if defined $msg; |
|
45
|
|
|
|
|
|
|
print <<__USAGE__; |
|
46
|
|
|
|
|
|
|
$cmd - Yet another tool for whatever you like |
|
47
|
|
|
|
|
|
|
Usage: $cmd [options...] file [more-file] |
|
48
|
|
|
|
|
|
|
Options: |
|
49
|
|
|
|
|
|
|
--boolean,-b Turn on the function A |
|
50
|
|
|
|
|
|
|
--string, -s STRING Specify the name of blahblah (defaults 'blahblah') |
|
51
|
|
|
|
|
|
|
--another-string, -as STRING Specify an alias for blahblah (defaults 'blahblah') |
|
52
|
|
|
|
|
|
|
... |
|
53
|
|
|
|
|
|
|
__USAGE__ |
|
54
|
|
|
|
|
|
|
} |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
my $boolean = 0; |
|
57
|
|
|
|
|
|
|
my $string = 'blahblah'; |
|
58
|
|
|
|
|
|
|
my $another_string = 'blahblah'; |
|
59
|
|
|
|
|
|
|
... |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
GetOptions( |
|
62
|
|
|
|
|
|
|
'boolean|b' => \$boolean, |
|
63
|
|
|
|
|
|
|
'string|s=s' => \$string, |
|
64
|
|
|
|
|
|
|
'another-string|as=s' => \$string, |
|
65
|
|
|
|
|
|
|
... |
|
66
|
|
|
|
|
|
|
); |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
usage and exit unless @ARGV; |
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=head2 With Getopt::Lazy |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
...or, being a little bit "lazier" than usual: |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
use Getopt::Lazy |
|
75
|
|
|
|
|
|
|
'boolean|b' => 'Turn on the function A', |
|
76
|
|
|
|
|
|
|
'string|s=s' => ['[STRING] Specify the name of blahblah' => 'blahblah'], |
|
77
|
|
|
|
|
|
|
'another-string|as=s' => ['[STRING] Specify an alias for blahblah' => 'blahblah'], |
|
78
|
|
|
|
|
|
|
-summary => 'Yet another tool for whatever you like', |
|
79
|
|
|
|
|
|
|
-usage => '%c %o file1 [more-file]', |
|
80
|
|
|
|
|
|
|
; |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
getopt; |
|
83
|
|
|
|
|
|
|
print usage and exit 1 unless @ARGV; |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head2 What We've Got? |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
The usage() will display the following help screen for you: |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
$ lazy |
|
90
|
|
|
|
|
|
|
lazy - Yet another tool for whatever you like |
|
91
|
|
|
|
|
|
|
Usage: lazy [options..] file1 [more-file] |
|
92
|
|
|
|
|
|
|
Options: |
|
93
|
|
|
|
|
|
|
--another-string, -as STRING Specify an alias for blahblah (default: blahblah) |
|
94
|
|
|
|
|
|
|
--boolean Turn on the function A |
|
95
|
|
|
|
|
|
|
--string, -s STRING Specify the name of blahblah (default: blahblah) |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head2 Detail Usage |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
Basically the Getopt::Lazy does two things for you: 1) spawning a variable for |
|
101
|
|
|
|
|
|
|
every given option, and 2) generating GNU-style help message. (Guess I was too |
|
102
|
|
|
|
|
|
|
lazy to put this down in details. See for yourself!) If you have other even |
|
103
|
|
|
|
|
|
|
lazier ways of doing the same thing, be sure to let me know! |
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head1 INTERFACE |
|
106
|
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=cut |
|
108
|
|
|
|
|
|
|
|
|
109
|
1
|
|
|
1
|
|
6
|
use Carp; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
267
|
|
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
our @ISA = qw/Exporter/; |
|
112
|
|
|
|
|
|
|
our @EXPORT = qw/GetOptions/; |
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
our %opt = (); |
|
115
|
|
|
|
|
|
|
our %usage = (); |
|
116
|
|
|
|
|
|
|
our %conf = (); |
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=over 2 |
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=item show_help |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
Show the help screen |
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=back |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=cut |
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
sub show_help { |
|
129
|
1
|
|
|
1
|
|
8
|
use File::Basename; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
114
|
|
|
130
|
1
|
|
|
1
|
|
1991
|
use Text::Wrap; |
|
|
1
|
|
|
|
|
4493
|
|
|
|
1
|
|
|
|
|
693
|
|
|
131
|
|
|
|
|
|
|
|
|
132
|
0
|
|
|
0
|
1
|
0
|
my $msg = shift; |
|
133
|
0
|
|
0
|
|
|
0
|
my $cmd = $conf{cmd} || basename $0; |
|
134
|
0
|
|
|
|
|
0
|
my $summary = $conf{summary}; |
|
135
|
0
|
|
0
|
|
|
0
|
my $usage = $conf{usage} || '%c %o'; |
|
136
|
0
|
|
|
|
|
0
|
$usage =~ s/\%c\b/$cmd/g; |
|
137
|
0
|
|
|
|
|
0
|
$usage =~ s/\%o\b/[options..]/g; |
|
138
|
|
|
|
|
|
|
|
|
139
|
0
|
0
|
|
|
|
0
|
print $msg, "\n" if defined $msg; |
|
140
|
0
|
0
|
|
|
|
0
|
print "$cmd - $summary\n" if defined $summary; |
|
141
|
0
|
0
|
|
|
|
0
|
print "usage: $usage\n" if defined $usage; |
|
142
|
0
|
0
|
|
|
|
0
|
return unless keys %usage; |
|
143
|
|
|
|
|
|
|
|
|
144
|
0
|
|
|
|
|
0
|
print "options:\n"; |
|
145
|
0
|
|
|
|
|
0
|
my $size = 8 * int (((reverse sort { $a <=> $b } map length $_, keys %usage)[0] + 8) / 8); |
|
|
0
|
|
|
|
|
0
|
|
|
146
|
0
|
|
|
|
|
0
|
for (sort keys %usage) { |
|
147
|
0
|
|
|
|
|
0
|
printf "\t%-${size}s%s\n", $_, $usage{$_}; |
|
148
|
|
|
|
|
|
|
} |
|
149
|
|
|
|
|
|
|
|
|
150
|
0
|
|
|
|
|
0
|
1; |
|
151
|
|
|
|
|
|
|
} |
|
152
|
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
sub import { |
|
154
|
1
|
|
|
1
|
|
10
|
my $pkg = shift; |
|
155
|
1
|
|
|
|
|
3
|
my %o = @_; |
|
156
|
|
|
|
|
|
|
|
|
157
|
1
|
0
|
|
0
|
|
5
|
$o{"&help"} = ["Show this help screen", sub { show_help and exit }]; |
|
|
0
|
|
|
|
|
0
|
|
|
158
|
|
|
|
|
|
|
|
|
159
|
1
|
|
|
|
|
4
|
for (keys %o) { |
|
160
|
1
|
50
|
|
|
|
5
|
m/^-(\w+)$/ and do { $conf{$1} = $o{$_}; next; }; |
|
|
0
|
|
|
|
|
0
|
|
|
|
0
|
|
|
|
|
0
|
|
|
161
|
|
|
|
|
|
|
|
|
162
|
1
|
|
|
|
|
10
|
my ($type, $spec, $name) = m/^([\&\@\%\$])?((.+?)(?:\|.*)?(?:\=.*)?)$/; |
|
163
|
1
|
|
|
|
|
2
|
my $guess = undef; |
|
164
|
1
|
|
|
|
|
9
|
(my $var = $name) =~ s/-/_/g; |
|
165
|
1
|
|
50
|
|
|
7
|
push @EXPORT, ($type || $guess || '$') . $var; |
|
166
|
|
|
|
|
|
|
|
|
167
|
1
|
|
|
|
|
3
|
my $item = "--$name"; |
|
168
|
1
|
50
|
|
|
|
5
|
my ($desc, @def) = ref $o{$_} eq 'ARRAY' ? @{$o{$_}}: $o{$_}; |
|
|
1
|
|
|
|
|
3
|
|
|
169
|
1
|
50
|
|
|
|
5
|
$spec =~ /\|(\w+)=/ and $item .= ", -$1"; |
|
170
|
1
|
50
|
|
|
|
4
|
$desc =~ s/^\[([A-Z_-]+)\]\s*// and $item .= " $1"; |
|
171
|
1
|
|
|
|
|
5
|
$usage{$item} = $desc; |
|
172
|
|
|
|
|
|
|
|
|
173
|
1
|
|
|
1
|
|
16
|
no strict 'refs'; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
290
|
|
|
174
|
|
|
|
|
|
|
(not defined $type or $type eq '$') and do { |
|
175
|
0
|
0
|
|
|
|
0
|
$usage{$item} .= " (default: $def[0])" if $def[0]; |
|
176
|
0
|
|
|
|
|
0
|
${"$var"} = shift @def; |
|
|
0
|
|
|
|
|
0
|
|
|
177
|
0
|
|
|
|
|
0
|
$opt{$spec} = *{"$var"}{SCALAR}; |
|
|
0
|
|
|
|
|
0
|
|
|
178
|
|
|
|
|
|
|
} or $type eq '@' and do { |
|
179
|
0
|
0
|
|
|
|
0
|
$usage{$item} .= " (default: ".join(',', @def).")" if @def > 0; |
|
180
|
0
|
|
|
|
|
0
|
@{"$var"} = (@def); |
|
|
0
|
|
|
|
|
0
|
|
|
181
|
0
|
|
|
|
|
0
|
$opt{$spec} = *{"$var"}{ARRAY}; |
|
|
0
|
|
|
|
|
0
|
|
|
182
|
|
|
|
|
|
|
} or $type eq '%' and do { |
|
183
|
0
|
|
|
|
|
0
|
%{"$var"} = (@def); |
|
|
0
|
|
|
|
|
0
|
|
|
184
|
0
|
|
|
|
|
0
|
$opt{$spec} = *{"$var"}{HASH}; |
|
|
0
|
|
|
|
|
0
|
|
|
185
|
1
|
50
|
50
|
|
|
20
|
} or $type eq '&' and do { |
|
|
|
|
33
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
186
|
1
|
|
|
|
|
2
|
my $code = shift @def; |
|
187
|
1
|
|
|
|
|
6
|
$opt{$spec} = $code; |
|
188
|
|
|
|
|
|
|
}; |
|
189
|
|
|
|
|
|
|
} |
|
190
|
|
|
|
|
|
|
|
|
191
|
1
|
|
|
|
|
138
|
$pkg->export_to_level(1, undef, @EXPORT); |
|
192
|
|
|
|
|
|
|
} |
|
193
|
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=over 2 |
|
195
|
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=item GetOptions |
|
197
|
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
Make Getopt::Long work! |
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=back |
|
201
|
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
=cut |
|
203
|
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
sub GetOptions { |
|
205
|
0
|
|
|
0
|
1
|
|
my %o = @_; |
|
206
|
|
|
|
|
|
|
|
|
207
|
1
|
|
|
1
|
|
1483
|
use Getopt::Long (); |
|
|
1
|
|
|
|
|
15438
|
|
|
|
1
|
|
|
|
|
137
|
|
|
208
|
0
|
|
|
|
|
|
Getopt::Long::GetOptions %opt; |
|
209
|
|
|
|
|
|
|
|
|
210
|
0
|
0
|
|
|
|
|
return unless defined $o{-autohelp}; |
|
211
|
0
|
0
|
|
|
|
|
my $show_help = ref $o{-autohelp} eq "CODE"? $o{-autohelp}->(): scalar $o{-autohelp}; |
|
212
|
0
|
0
|
0
|
|
|
|
show_help and exit if $show_help; |
|
213
|
|
|
|
|
|
|
} |
|
214
|
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
1; # Magic true value required at end of module |
|
217
|
|
|
|
|
|
|
__END__ |