line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Getopt::Lazy; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
require 5.006; |
4
|
|
|
|
|
|
|
|
5
|
2
|
|
|
2
|
|
43628
|
use version; |
|
2
|
|
|
|
|
4093
|
|
|
2
|
|
|
|
|
12
|
|
6
|
|
|
|
|
|
|
our $VERSION = qv('0.0.7'); |
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
158
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
37
|
|
9
|
2
|
|
|
2
|
|
11
|
use warnings; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
81
|
|
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
|
|
|
|
|
|
|
GetOptions; |
27
|
|
|
|
|
|
|
show_help and exit 1 unless @ARGV; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=head1 DESCRIPTION |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
Got tired of the same tedious "getopt" things in every script |
32
|
|
|
|
|
|
|
you wrote? This module works for you! |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=head2 Without Getopt::Lazy |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
Normally your script would go like this: |
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
|
|
|
|
|
|
|
The same thing using Getopt::Lazy: |
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
|
|
|
|
|
|
|
GetOptions; |
83
|
|
|
|
|
|
|
show_help and exit 1 unless @ARGV; |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head2 What We've Got? |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
show_help() simply prints out this help screen: |
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
|
|
|
|
|
|
|
Getopt::Lazy does two things for you: 1) spawning a variable for |
101
|
|
|
|
|
|
|
every given option, and 2) generating GNU-style help messages. |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head1 INTERFACE |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=cut |
106
|
|
|
|
|
|
|
|
107
|
2
|
|
|
2
|
|
11
|
use Carp; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
296
|
|
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
our @ISA = qw/Exporter/; |
110
|
|
|
|
|
|
|
our @EXPORT = qw/GetOptions show_help/; |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
our %opt = (); |
113
|
|
|
|
|
|
|
our %usage = (); |
114
|
|
|
|
|
|
|
our %conf = (); |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=over 2 |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=item show_help |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
Show the help screen |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=back |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=cut |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
sub show_help { |
127
|
2
|
|
|
2
|
|
11
|
use File::Basename; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
175
|
|
128
|
2
|
|
|
2
|
|
3567
|
use Text::Wrap; |
|
2
|
|
|
|
|
6005
|
|
|
2
|
|
|
|
|
1236
|
|
129
|
|
|
|
|
|
|
|
130
|
0
|
|
|
0
|
1
|
0
|
my $msg = shift; |
131
|
0
|
|
0
|
|
|
0
|
my $cmd = $conf{cmd} || basename $0; |
132
|
0
|
|
|
|
|
0
|
my $summary = $conf{summary}; |
133
|
0
|
|
0
|
|
|
0
|
my $usage = $conf{usage} || '%c %o'; |
134
|
0
|
|
|
|
|
0
|
$usage =~ s/\%c\b/$cmd/g; |
135
|
0
|
|
|
|
|
0
|
$usage =~ s/\%o\b/[options..]/g; |
136
|
|
|
|
|
|
|
|
137
|
0
|
0
|
|
|
|
0
|
print $msg, "\n" if defined $msg; |
138
|
0
|
0
|
|
|
|
0
|
print "$cmd - $summary\n" if defined $summary; |
139
|
0
|
0
|
|
|
|
0
|
print "usage: $usage\n" if defined $usage; |
140
|
0
|
0
|
|
|
|
0
|
return unless keys %usage; |
141
|
|
|
|
|
|
|
|
142
|
0
|
|
|
|
|
0
|
print "options:\n"; |
143
|
0
|
|
|
|
|
0
|
my $size = 8 * int (((reverse sort { $a <=> $b } map length $_, keys %usage)[0] + 8) / 8); |
|
0
|
|
|
|
|
0
|
|
144
|
0
|
|
|
|
|
0
|
for (sort keys %usage) { |
145
|
0
|
|
|
|
|
0
|
printf "\t%-${size}s%s\n", $_, $usage{$_}; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
0
|
|
|
|
|
0
|
1; |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
sub import { |
152
|
2
|
|
|
2
|
|
19
|
my $pkg = shift; |
153
|
2
|
|
|
|
|
4
|
my %o = @_; |
154
|
|
|
|
|
|
|
|
155
|
2
|
0
|
|
0
|
|
13
|
$o{"&help"} = ["Show this help screen", sub { show_help and exit }]; |
|
0
|
|
|
|
|
0
|
|
156
|
|
|
|
|
|
|
|
157
|
2
|
|
|
|
|
8
|
for (keys %o) { |
158
|
2
|
50
|
|
|
|
11
|
m/^-(\w+)$/ and do { $conf{$1} = $o{$_}; next; }; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
159
|
|
|
|
|
|
|
|
160
|
2
|
|
|
|
|
19
|
my ($type, $spec, $name) = m/^([\&\@\%\$])?((.+?)(?:\|.*)?(?:\=.*)?)$/; |
161
|
2
|
|
|
|
|
5
|
my $guess = undef; |
162
|
2
|
|
|
|
|
14
|
(my $var = $name) =~ s/-/_/g; |
163
|
2
|
|
50
|
|
|
13
|
push @EXPORT, ($type || $guess || '$') . $var; |
164
|
|
|
|
|
|
|
|
165
|
2
|
|
|
|
|
5
|
my $item = "--$name"; |
166
|
2
|
50
|
|
|
|
11
|
my ($desc, @def) = ref $o{$_} eq 'ARRAY' ? @{$o{$_}}: $o{$_}; |
|
2
|
|
|
|
|
7
|
|
167
|
2
|
50
|
|
|
|
8
|
$spec =~ /\|(\w+)=/ and $item .= ", -$1"; |
168
|
2
|
50
|
|
|
|
9
|
$desc =~ s/^\[([A-Z_-]+)\]\s*// and $item .= " $1"; |
169
|
2
|
|
|
|
|
4
|
$usage{$item} = $desc; |
170
|
|
|
|
|
|
|
|
171
|
2
|
|
|
2
|
|
16
|
no strict 'refs'; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
651
|
|
172
|
|
|
|
|
|
|
(not defined $type or $type eq '$') and do { |
173
|
0
|
0
|
|
|
|
0
|
$usage{$item} .= " (default: $def[0])" if $def[0]; |
174
|
0
|
|
|
|
|
0
|
${"$var"} = shift @def; |
|
0
|
|
|
|
|
0
|
|
175
|
0
|
|
|
|
|
0
|
$opt{$spec} = *{"$var"}{SCALAR}; |
|
0
|
|
|
|
|
0
|
|
176
|
|
|
|
|
|
|
} or $type eq '@' and do { |
177
|
0
|
0
|
|
|
|
0
|
$usage{$item} .= " (default: ".join(',', @def).")" if @def > 0; |
178
|
0
|
|
|
|
|
0
|
@{"$var"} = (@def); |
|
0
|
|
|
|
|
0
|
|
179
|
0
|
|
|
|
|
0
|
$opt{$spec} = *{"$var"}{ARRAY}; |
|
0
|
|
|
|
|
0
|
|
180
|
|
|
|
|
|
|
} or $type eq '%' and do { |
181
|
0
|
|
|
|
|
0
|
%{"$var"} = (@def); |
|
0
|
|
|
|
|
0
|
|
182
|
0
|
|
|
|
|
0
|
$opt{$spec} = *{"$var"}{HASH}; |
|
0
|
|
|
|
|
0
|
|
183
|
2
|
50
|
50
|
|
|
40
|
} or $type eq '&' and do { |
|
|
|
33
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
33
|
|
|
|
|
184
|
2
|
|
|
|
|
5
|
my $code = shift @def; |
185
|
2
|
|
|
|
|
8
|
$opt{$spec} = $code; |
186
|
|
|
|
|
|
|
}; |
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
|
189
|
2
|
|
|
|
|
1870
|
$pkg->export_to_level(1, undef, @EXPORT); |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=over 2 |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=item GetOptions |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
Make Getopt::Long work! |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=back |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=cut |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
sub GetOptions { |
203
|
0
|
|
|
0
|
1
|
|
my %o = @_; |
204
|
|
|
|
|
|
|
|
205
|
2
|
|
|
2
|
|
2363
|
use Getopt::Long (); |
|
2
|
|
|
|
|
28281
|
|
|
2
|
|
|
|
|
254
|
|
206
|
0
|
|
|
|
|
|
Getopt::Long::GetOptions %opt; |
207
|
|
|
|
|
|
|
|
208
|
0
|
0
|
|
|
|
|
return unless defined $o{-autohelp}; |
209
|
0
|
0
|
|
|
|
|
my $show_help = ref $o{-autohelp} eq "CODE"? $o{-autohelp}->(): scalar $o{-autohelp}; |
210
|
0
|
0
|
0
|
|
|
|
show_help and exit if $show_help; |
211
|
|
|
|
|
|
|
} |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
1; # Magic true value required at end of module |
215
|
|
|
|
|
|
|
__END__ |