line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Getopt::App; |
2
|
7
|
|
|
7
|
|
1631799
|
use feature qw(:5.16); |
|
7
|
|
|
|
|
61
|
|
|
7
|
|
|
|
|
1119
|
|
3
|
7
|
|
|
7
|
|
51
|
use strict; |
|
7
|
|
|
|
|
16
|
|
|
7
|
|
|
|
|
164
|
|
4
|
7
|
|
|
7
|
|
63
|
use warnings; |
|
7
|
|
|
|
|
13
|
|
|
7
|
|
|
|
|
187
|
|
5
|
7
|
|
|
7
|
|
42
|
use utf8; |
|
7
|
|
|
|
|
12
|
|
|
7
|
|
|
|
|
60
|
|
6
|
|
|
|
|
|
|
|
7
|
7
|
|
|
7
|
|
279
|
use Carp qw(croak); |
|
7
|
|
|
|
|
13
|
|
|
7
|
|
|
|
|
421
|
|
8
|
7
|
|
|
7
|
|
5732
|
use Getopt::Long (); |
|
7
|
|
|
|
|
74418
|
|
|
7
|
|
|
|
|
233
|
|
9
|
7
|
|
|
7
|
|
59
|
use List::Util qw(first); |
|
7
|
|
|
|
|
14
|
|
|
7
|
|
|
|
|
11583
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = '0.13'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our ($OPT_COMMENT_RE, $OPTIONS, $SUBCOMMAND, $SUBCOMMANDS, %APPS) = (qr{\s+\#\s+}); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
our $call_maybe = sub { |
16
|
|
|
|
|
|
|
my ($app, $m) = (shift, shift); |
17
|
|
|
|
|
|
|
local $Getopt::App::APP_CLASS; |
18
|
|
|
|
|
|
|
$m = $app->can($m) || __PACKAGE__->can("_$m"); |
19
|
|
|
|
|
|
|
return $m ? $app->$m(@_) : undef; |
20
|
|
|
|
|
|
|
}; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub bundle { |
23
|
1
|
|
|
1
|
1
|
7063
|
my ($class, $script, $OUT) = (@_, \*STDOUT); |
24
|
1
|
|
|
|
|
3
|
my ($package, @script); |
25
|
|
|
|
|
|
|
|
26
|
1
|
50
|
|
|
|
45
|
open my $SCRIPT, '<', $script or croak "Can't read $script: $!"; |
27
|
1
|
|
|
|
|
37
|
while (my $line = readline $SCRIPT) { |
28
|
2
|
100
|
|
|
|
20
|
if ($line =~ m!^\s*package\s+\S+\s*;!) { # look for app class name |
|
|
50
|
|
|
|
|
|
29
|
1
|
|
|
|
|
3
|
$package .= $line; |
30
|
1
|
|
|
|
|
5
|
last; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
elsif ($. == 1) { # look for hashbang |
33
|
1
|
50
|
|
|
|
6
|
$line =~ m/^#!/ ? print {$OUT} $line : do { print {$OUT} "#!$^X\n"; push @script, $line }; |
|
1
|
|
|
|
|
19
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
else { |
36
|
0
|
|
|
|
|
0
|
push @script, $line; |
37
|
0
|
0
|
|
|
|
0
|
last if $line =~ m!^[^#]+;!; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
1
|
|
|
|
|
8
|
my $out_line = ''; |
42
|
1
|
50
|
|
|
|
52
|
open my $SELF, '<', __FILE__ or croak "Can't read Getopt::App: $!"; |
43
|
1
|
|
|
|
|
42
|
while (my $line = readline $SELF) { |
44
|
287
|
100
|
|
|
|
1666
|
next if $line =~ m!(?:\bVERSION\s|^\s*$)!; # TODO: Should version get skipped? |
45
|
234
|
100
|
|
|
|
459
|
next if $line =~ m!^sub bundle\s\{! .. $line =~ m!^}$!; # skip bundle() |
46
|
191
|
100
|
|
|
|
298
|
last if $line =~ m!^1;\s*$!; # do not include POD |
47
|
|
|
|
|
|
|
|
48
|
190
|
|
|
|
|
235
|
chomp $line; |
49
|
190
|
100
|
|
|
|
338
|
if ($line =~ m!^sub\s!) { |
|
|
100
|
|
|
|
|
|
50
|
16
|
100
|
|
|
|
30
|
print {$OUT} $out_line, "\n" if $out_line; |
|
1
|
|
|
|
|
4
|
|
51
|
16
|
100
|
|
|
|
50
|
$line =~ m!\}$! ? print {$OUT} $line, "\n" : ($out_line = $line); |
|
2
|
|
|
|
|
7
|
|
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
elsif ($line =~ m!^}$!) { |
54
|
14
|
|
|
|
|
23
|
print {$OUT} $out_line, $line, "\n"; |
|
14
|
|
|
|
|
37
|
|
55
|
14
|
|
|
|
|
40
|
$out_line = ''; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
else { |
58
|
160
|
|
|
|
|
413
|
$line =~ s!^[ ]{2,}!!; # remove leading white space |
59
|
160
|
|
|
|
|
252
|
$line =~ s!\#\s.*!!; # remove comments |
60
|
160
|
|
|
|
|
538
|
$out_line .= $line; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
1
|
|
|
|
|
2
|
print {$OUT} qq(BEGIN{\$INC{'Getopt/App.pm'}='BUNDLED'}\n); |
|
1
|
|
|
|
|
4
|
|
65
|
1
|
|
50
|
|
|
2
|
print {$OUT} +($package || "package main\n"); |
|
1
|
|
|
|
|
4
|
|
66
|
1
|
|
|
|
|
1
|
print {$OUT} @script; |
|
1
|
|
|
|
|
4
|
|
67
|
1
|
|
|
|
|
4
|
print {$OUT} $_ while readline $SCRIPT; |
|
2
|
|
|
|
|
40
|
|
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub capture { |
71
|
25
|
|
|
25
|
1
|
62647
|
my ($app, $argv) = @_; |
72
|
|
|
|
|
|
|
|
73
|
25
|
|
|
|
|
4127
|
require File::Temp; |
74
|
25
|
|
|
|
|
89446
|
my ($STDOUT_CAPTURE, $STDERR_CAPTURE) = (File::Temp->new, File::Temp->new); |
75
|
25
|
50
|
|
|
|
18972
|
open my $STDOUT_ORIG, '>&STDOUT' or die "Can't remember original STDOUT: $!"; |
76
|
25
|
50
|
|
|
|
503
|
open my $STDERR_ORIG, '>&STDERR' or die "Can't remember original STDERR: $!"; |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
my $restore = sub { |
79
|
25
|
50
|
|
25
|
|
606
|
open STDERR, '>&', fileno($STDERR_ORIG) or die "Can't restore STDERR: $!"; |
80
|
25
|
50
|
|
|
|
491
|
open STDOUT, '>&', fileno($STDOUT_ORIG) or die "Can't restore STDOUT: $!"; |
81
|
25
|
50
|
|
|
|
125
|
die $_[0] if $_[0]; |
82
|
25
|
|
|
|
|
178
|
}; |
83
|
|
|
|
|
|
|
|
84
|
25
|
50
|
|
|
|
651
|
open STDOUT, '>&', fileno($STDOUT_CAPTURE) or $restore->("Can't capture STDOUT: $!"); |
85
|
25
|
50
|
|
|
|
532
|
open STDERR, '>&', fileno($STDERR_CAPTURE) or $restore->("Can't capture STDERR: $!"); |
86
|
|
|
|
|
|
|
|
87
|
25
|
|
|
|
|
67
|
my $exit_value; |
88
|
25
|
100
|
100
|
|
|
43
|
unless (eval { $exit_value = $app->($argv || [@ARGV]); 1; }) { |
|
25
|
|
|
|
|
148
|
|
|
21
|
|
|
|
|
138
|
|
89
|
4
|
|
|
|
|
188
|
print STDERR $@; |
90
|
4
|
|
|
|
|
54
|
$exit_value = int $!; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
25
|
|
|
|
|
180
|
STDERR->flush; |
94
|
25
|
|
|
|
|
124
|
STDOUT->flush; |
95
|
25
|
|
|
|
|
72
|
$restore->(); |
96
|
25
|
|
|
|
|
202
|
seek $STDERR_CAPTURE, 0, 0; |
97
|
25
|
|
|
|
|
174
|
seek $STDOUT_CAPTURE, 0, 0; |
98
|
|
|
|
|
|
|
|
99
|
25
|
|
|
|
|
1761
|
return [join('', <$STDOUT_CAPTURE>), join('', <$STDERR_CAPTURE>), $exit_value]; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub extract_usage { |
103
|
4
|
|
|
4
|
1
|
40
|
my %pod2usage; |
104
|
4
|
|
|
|
|
13
|
$pod2usage{'-sections'} = shift; |
105
|
4
|
|
33
|
|
|
25
|
$pod2usage{'-input'} = shift || (caller)[1]; |
106
|
4
|
100
|
|
|
|
26
|
$pod2usage{'-verbose'} = 99 if $pod2usage{'-sections'}; |
107
|
|
|
|
|
|
|
|
108
|
4
|
|
|
|
|
1154
|
require Pod::Usage; |
109
|
4
|
|
|
2
|
|
77354
|
open my $USAGE, '>', \my $usage; |
|
2
|
|
|
|
|
13
|
|
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
19
|
|
110
|
4
|
|
|
|
|
1641
|
Pod::Usage::pod2usage(-exitval => 'noexit', -output => $USAGE, %pod2usage); |
111
|
4
|
|
|
|
|
13710
|
close $USAGE; |
112
|
|
|
|
|
|
|
|
113
|
4
|
|
100
|
|
|
31
|
$usage //= ''; |
114
|
4
|
100
|
|
|
|
26
|
$usage =~ s!^(.*?)\n!!s if $pod2usage{'-sections'}; |
115
|
4
|
|
|
|
|
17
|
$usage =~ s!^Usage:\n\s+([A-Z])!$1!s; # Remove "Usage" header if SYNOPSIS has a description |
116
|
4
|
|
|
|
|
20
|
$usage =~ s!^ !!gm; |
117
|
|
|
|
|
|
|
|
118
|
4
|
|
100
|
|
|
27
|
return join '', $usage, _usage_for_subcommands($SUBCOMMANDS || []), _usage_for_options($OPTIONS || []); |
|
|
|
50
|
|
|
|
|
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub import { |
122
|
20
|
|
|
20
|
|
18962
|
my ($class, @flags) = @_; |
123
|
20
|
|
|
|
|
53
|
my $caller = caller; |
124
|
|
|
|
|
|
|
|
125
|
20
|
|
|
|
|
433
|
$_->import for qw(strict warnings utf8); |
126
|
20
|
|
|
|
|
1321
|
feature->import(':5.16'); |
127
|
|
|
|
|
|
|
|
128
|
20
|
|
|
|
|
43
|
my $skip_default; |
129
|
7
|
|
|
7
|
|
62
|
no strict qw(refs); |
|
7
|
|
|
|
|
19
|
|
|
7
|
|
|
|
|
17551
|
|
130
|
20
|
|
|
|
|
81
|
while (my $flag = shift @flags) { |
131
|
10
|
100
|
|
|
|
78
|
if ($flag eq '-capture') { |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
132
|
6
|
|
|
|
|
15
|
*{"$caller\::capture"} = \&capture; |
|
6
|
|
|
|
|
36
|
|
133
|
6
|
|
|
|
|
22
|
$skip_default = 1; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
elsif ($flag eq '-complete') { |
136
|
2
|
|
|
|
|
447
|
require Getopt::App::Complete; |
137
|
2
|
|
|
|
|
12
|
*{"$caller\::generate_completion_script"} = \&Getopt::App::Complete::generate_completion_script; |
|
2
|
|
|
|
|
23
|
|
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
elsif ($flag eq '-signatures') { |
140
|
0
|
|
|
|
|
0
|
require experimental; |
141
|
0
|
|
|
|
|
0
|
experimental->import(qw(signatures)); |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
elsif ($flag !~ /^-/) { |
144
|
2
|
100
|
|
|
|
228
|
croak "package definition required - cannot extend main with $flag!" if $caller eq 'main'; |
145
|
1
|
50
|
|
|
|
105
|
croak "require $flag FAIL $@" unless eval "require $flag;1"; |
146
|
1
|
|
|
|
|
8
|
push @{"${caller}::ISA"}, $flag; |
|
1
|
|
|
|
|
15
|
|
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
19
|
100
|
|
|
|
11580
|
unless ($skip_default) { |
151
|
13
|
50
|
|
|
|
165
|
*{"$caller\::extract_usage"} = \&extract_usage unless $caller->can('extract_usage'); |
|
13
|
|
|
|
|
64
|
|
152
|
13
|
50
|
|
|
|
83
|
*{"$caller\::new"} = \&new unless $caller->can('new'); |
|
13
|
|
|
|
|
41
|
|
153
|
13
|
|
|
|
|
25
|
*{"$caller\::run"} = \&run; |
|
13
|
|
|
|
|
1364
|
|
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
sub new { |
158
|
51
|
|
|
51
|
1
|
2504
|
my $class = shift; |
159
|
51
|
100
|
33
|
|
|
271
|
bless @_ ? @_ > 1 ? {@_} : {%{$_[0]}} : {}, ref $class || $class; |
|
1
|
100
|
|
|
|
8
|
|
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
sub run { |
163
|
59
|
|
|
59
|
1
|
312
|
my @rules = @_; |
164
|
59
|
|
66
|
|
|
211
|
my $class = $Getopt::App::APP_CLASS || caller; |
165
|
47
|
|
|
47
|
|
8094
|
return sub { local $Getopt::App::APP_CLASS = $class; run(@_, @rules) } |
|
47
|
|
|
|
|
127
|
|
166
|
59
|
100
|
66
|
|
|
352
|
if !$Getopt::App::APP_CLASS and defined wantarray; |
167
|
|
|
|
|
|
|
|
168
|
47
|
|
|
|
|
80
|
my $cb = pop @rules; |
169
|
47
|
50
|
|
|
|
151
|
my $argv = ref $rules[0] eq 'ARRAY' ? shift @rules : [@ARGV]; |
170
|
47
|
|
|
|
|
104
|
local $OPTIONS = [@rules]; |
171
|
|
|
|
|
|
|
|
172
|
47
|
|
|
|
|
174
|
my $app = $class->new; |
173
|
47
|
100
|
66
|
|
|
178
|
return $app->$call_maybe('getopt_complete_reply') if defined $ENV{COMP_POINT} and defined $ENV{COMP_LINE}; |
174
|
|
|
|
|
|
|
|
175
|
37
|
|
|
|
|
103
|
$app->$call_maybe(getopt_pre_process_argv => $argv); |
176
|
|
|
|
|
|
|
|
177
|
37
|
|
|
|
|
126
|
local $SUBCOMMANDS = $app->$call_maybe('getopt_subcommands'); |
178
|
37
|
100
|
|
|
|
510
|
my $exit_value = $SUBCOMMANDS ? _subcommand_run_maybe($app, $SUBCOMMANDS, $argv) : undef; |
179
|
33
|
100
|
|
|
|
106
|
return _exit($app, $exit_value) if defined $exit_value; |
180
|
24
|
|
|
|
|
65
|
return _run($app, \@rules, $argv, $cb); |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
|
183
|
10
|
|
|
10
|
|
46
|
sub _getopt_complete_reply { Getopt::App::Complete::complete_reply(@_) } |
184
|
|
|
|
|
|
|
|
185
|
24
|
|
|
24
|
|
83
|
sub _getopt_configure {qw(bundling no_auto_abbrev no_ignore_case pass_through require_order)} |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
sub _getopt_load_subcommand { |
188
|
6
|
|
|
6
|
|
18
|
my ($app, $subcommand, $argv) = @_; |
189
|
6
|
50
|
|
|
|
18
|
return $subcommand->[1] if ref $subcommand->[1] eq 'CODE'; |
190
|
|
|
|
|
|
|
|
191
|
6
|
|
|
|
|
20
|
($@, $!) = ('', 0); |
192
|
6
|
100
|
|
|
|
2615
|
croak "Unable to load subcommand $subcommand->[0]: $@ ($!)" unless my $code = do $subcommand->[1]; |
193
|
5
|
|
|
|
|
31
|
return $code; |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
sub _getopt_post_process_argv { |
197
|
23
|
|
|
23
|
|
54
|
my ($app, $argv, $state) = @_; |
198
|
23
|
50
|
|
|
|
60
|
return unless $state->{valid}; |
199
|
23
|
100
|
100
|
|
|
105
|
return unless $argv->[0] and $argv->[0] =~ m!^-!; |
200
|
1
|
|
|
|
|
3
|
$! = 1; |
201
|
1
|
|
|
|
|
15
|
die "Invalid argument or argument order: @$argv\n"; |
202
|
|
|
|
|
|
|
} |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
sub _getopt_unknown_subcommand { |
205
|
1
|
|
|
1
|
|
7
|
my ($app, $argv) = @_; |
206
|
1
|
|
|
|
|
3
|
$! = 2; |
207
|
1
|
|
|
|
|
12
|
die "Unknown subcommand: $argv->[0]\n"; |
208
|
|
|
|
|
|
|
} |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
sub _exit { |
211
|
33
|
|
|
33
|
|
179
|
my ($app, $exit_value) = @_; |
212
|
33
|
|
100
|
|
|
74
|
$exit_value = $app->$call_maybe(getopt_post_process_exit_value => $exit_value) // $exit_value; |
213
|
33
|
100
|
100
|
|
|
203
|
$exit_value = 0 unless $exit_value and $exit_value =~ m!^\d{1,3}$!; |
214
|
33
|
100
|
|
|
|
77
|
$exit_value = 255 unless $exit_value < 255; |
215
|
33
|
50
|
|
|
|
69
|
exit $exit_value unless $Getopt::App::APP_CLASS; |
216
|
33
|
|
|
|
|
301
|
return $exit_value; |
217
|
|
|
|
|
|
|
} |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
sub _run { |
220
|
26
|
|
|
26
|
|
58
|
my ($app, $rules, $argv, $cb) = @_; |
221
|
26
|
|
|
|
|
229
|
s!$OPT_COMMENT_RE.*$!! for @$rules; |
222
|
|
|
|
|
|
|
|
223
|
26
|
|
|
|
|
63
|
my @configure = $app->$call_maybe('getopt_configure'); |
224
|
26
|
|
|
|
|
121
|
my $prev = Getopt::Long::Configure(@configure); |
225
|
26
|
100
|
|
|
|
1977
|
my $valid = Getopt::Long::GetOptionsFromArray($argv, $app, @$rules) ? 1 : 0; |
226
|
26
|
|
|
|
|
5912
|
Getopt::Long::Configure($prev); |
227
|
26
|
|
|
|
|
516
|
$app->$call_maybe(getopt_post_process_argv => $argv, {valid => $valid}); |
228
|
24
|
100
|
|
|
|
251
|
return _exit($app, $valid ? $app->$cb(@$argv) : 1); |
229
|
|
|
|
|
|
|
} |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
sub _subcommand_run { |
232
|
13
|
|
|
13
|
|
42
|
my ($app, $subcommand, $argv) = @_; |
233
|
13
|
|
|
|
|
18
|
local $Getopt::App::SUBCOMMAND = $subcommand; |
234
|
|
|
|
|
|
|
|
235
|
13
|
|
|
|
|
76
|
my $method = $app->can($subcommand->[1]); |
236
|
13
|
100
|
|
|
|
42
|
return _run($app, [@$OPTIONS], [@$argv[1 .. $#$argv]], $method) if $method; |
237
|
|
|
|
|
|
|
|
238
|
11
|
100
|
|
|
|
35
|
unless ($APPS{$subcommand->[1]}) { |
239
|
6
|
|
|
|
|
18
|
$APPS{$subcommand->[1]} = $app->$call_maybe(getopt_load_subcommand => $subcommand, $argv); |
240
|
5
|
50
|
|
|
|
30
|
croak "$subcommand->[0] did not return a code ref" unless ref $APPS{$subcommand->[1]} eq 'CODE'; |
241
|
|
|
|
|
|
|
} |
242
|
|
|
|
|
|
|
|
243
|
10
|
|
|
|
|
52
|
return $APPS{$subcommand->[1]}->([@$argv[1 .. $#$argv]]); |
244
|
|
|
|
|
|
|
} |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
sub _subcommand_run_maybe { |
247
|
17
|
|
|
17
|
|
35
|
my ($app, $subcommands, $argv) = @_; |
248
|
17
|
100
|
100
|
|
|
103
|
return undef unless $argv->[0] and $argv->[0] =~ m!^\w!; |
249
|
|
|
|
|
|
|
return $app->$call_maybe(getopt_unknown_subcommand => $argv) |
250
|
14
|
100
|
|
46
|
|
95
|
unless my $subcommand = first { $_->[0] eq $argv->[0] } @$subcommands; |
|
46
|
|
|
|
|
92
|
|
251
|
10
|
|
|
|
|
46
|
return _subcommand_run($app, $subcommand, $argv); |
252
|
|
|
|
|
|
|
} |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
sub _usage_for_options { |
255
|
4
|
|
|
4
|
|
11
|
my ($rules) = @_; |
256
|
4
|
100
|
|
|
|
57
|
return '' unless @$rules; |
257
|
|
|
|
|
|
|
|
258
|
3
|
|
|
|
|
6
|
my ($len, @options) = (0); |
259
|
3
|
|
|
|
|
8
|
for (@$rules) { |
260
|
9
|
|
|
|
|
48
|
my @o = split $OPT_COMMENT_RE, $_, 2; |
261
|
9
|
|
|
|
|
38
|
$o[0] =~ s/(=[si][@%]?|\!|\+)$//; |
262
|
9
|
100
|
|
|
|
32
|
$o[0] = join ', ', map { length($_) == 1 ? "-$_" : "--$_" } sort { length($b) <=> length($a) } split /\|/, $o[0]; |
|
11
|
|
|
|
|
48
|
|
|
2
|
|
|
|
|
9
|
|
263
|
9
|
|
100
|
|
|
31
|
$o[1] //= ''; |
264
|
|
|
|
|
|
|
|
265
|
9
|
|
|
|
|
14
|
my $l = length $o[0]; |
266
|
9
|
100
|
|
|
|
20
|
$len = $l if $l > $len; |
267
|
9
|
|
|
|
|
19
|
push @options, \@o; |
268
|
|
|
|
|
|
|
} |
269
|
|
|
|
|
|
|
|
270
|
3
|
|
|
|
|
20
|
return "Options:\n" . join('', map { sprintf " %-${len}s %s\n", @$_ } @options) . "\n"; |
|
9
|
|
|
|
|
189
|
|
271
|
|
|
|
|
|
|
} |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
sub _usage_for_subcommands { |
274
|
4
|
|
|
4
|
|
11
|
my ($subcommands) = @_; |
275
|
4
|
100
|
|
|
|
24
|
return '' unless @$subcommands; |
276
|
|
|
|
|
|
|
|
277
|
1
|
|
|
|
|
3
|
my ($len, @cmds) = (0); |
278
|
1
|
|
|
|
|
3
|
for my $s (@$subcommands) { |
279
|
6
|
|
|
|
|
12
|
my $l = length $s->[0]; |
280
|
6
|
100
|
|
|
|
11
|
$len = $l if $l > $len; |
281
|
6
|
|
50
|
|
|
19
|
push @cmds, [$s->[0], $s->[2] // '']; |
282
|
|
|
|
|
|
|
} |
283
|
|
|
|
|
|
|
|
284
|
1
|
|
|
|
|
3
|
return "Subcommands:\n" . join('', map { sprintf " %-${len}s %s\n", @$_ } @cmds) . "\n"; |
|
6
|
|
|
|
|
26
|
|
285
|
|
|
|
|
|
|
} |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
1; |
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
=encoding utf8 |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
=head1 NAME |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
Getopt::App - Write and test your script with ease |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
=head1 SYNOPSIS |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
=head2 The script file |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
#!/usr/bin/env perl |
300
|
|
|
|
|
|
|
package My::Script; |
301
|
|
|
|
|
|
|
use Getopt::App -complete, -signatures; |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
# See "APPLICATION METHODS" |
304
|
|
|
|
|
|
|
sub getopt_post_process_argv ($app, $argv, $state) { ... } |
305
|
|
|
|
|
|
|
sub getopt_configure ($app) { ... } |
306
|
|
|
|
|
|
|
|
307
|
|
|
|
|
|
|
# run() must be the last statement in the script |
308
|
|
|
|
|
|
|
run( |
309
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
# Specify your Getopt::Long options and optionally a help text |
311
|
|
|
|
|
|
|
'h|help # Output help', |
312
|
|
|
|
|
|
|
'v+ # Verbose output', |
313
|
|
|
|
|
|
|
'name=s # Specify a name', |
314
|
|
|
|
|
|
|
'completion-script # Print autocomplete script', |
315
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
# Here is the main sub that will run the script |
317
|
|
|
|
|
|
|
sub ($app, @extra) { |
318
|
|
|
|
|
|
|
return print generate_completion_script() if $app->{'completion-script'}; |
319
|
|
|
|
|
|
|
return print extract_usage() if $app->{h}; |
320
|
|
|
|
|
|
|
say $app->{name} // 'no name'; # Access command line options |
321
|
|
|
|
|
|
|
return 42; # Reture value is used as exit code |
322
|
|
|
|
|
|
|
} |
323
|
|
|
|
|
|
|
); |
324
|
|
|
|
|
|
|
|
325
|
|
|
|
|
|
|
=head2 Running the script |
326
|
|
|
|
|
|
|
|
327
|
|
|
|
|
|
|
The example script above can be run like any other script: |
328
|
|
|
|
|
|
|
|
329
|
|
|
|
|
|
|
$ my-script --name superwoman; # prints "superwoman" |
330
|
|
|
|
|
|
|
$ echo $? # 42 |
331
|
|
|
|
|
|
|
|
332
|
|
|
|
|
|
|
=head2 Testing |
333
|
|
|
|
|
|
|
|
334
|
|
|
|
|
|
|
use Test::More; |
335
|
|
|
|
|
|
|
use Cwd qw(abs_path); |
336
|
|
|
|
|
|
|
use Getopt::App -capture; |
337
|
|
|
|
|
|
|
|
338
|
|
|
|
|
|
|
# Sourcing the script returns a callback |
339
|
|
|
|
|
|
|
my $app = do(abs_path('./bin/myapp')); |
340
|
|
|
|
|
|
|
|
341
|
|
|
|
|
|
|
# The callback can be called with any @ARGV |
342
|
|
|
|
|
|
|
subtest name => sub { |
343
|
|
|
|
|
|
|
my $got = capture($app, [qw(--name superwoman)]); |
344
|
|
|
|
|
|
|
is $got->[0], "superwoman\n", 'stdout'; |
345
|
|
|
|
|
|
|
is $got->[1], '', 'stderr'; |
346
|
|
|
|
|
|
|
is $got->[2], 42, 'exit value'; |
347
|
|
|
|
|
|
|
}; |
348
|
|
|
|
|
|
|
|
349
|
|
|
|
|
|
|
done_testing; |
350
|
|
|
|
|
|
|
|
351
|
|
|
|
|
|
|
=head2 Subcommands |
352
|
|
|
|
|
|
|
|
353
|
|
|
|
|
|
|
#!/usr/bin/env perl |
354
|
|
|
|
|
|
|
# Define a package to avoid mixing methods after loading the subcommand script |
355
|
|
|
|
|
|
|
package My::App::main; |
356
|
|
|
|
|
|
|
use Getopt::App -complete; |
357
|
|
|
|
|
|
|
|
358
|
|
|
|
|
|
|
# getopt_subcommands() is called by Getopt::App |
359
|
|
|
|
|
|
|
sub getopt_subcommands { |
360
|
|
|
|
|
|
|
my $app = shift; |
361
|
|
|
|
|
|
|
|
362
|
|
|
|
|
|
|
return [ |
363
|
|
|
|
|
|
|
['find', '/path/to/subcommand/find.pl', 'Find things'], |
364
|
|
|
|
|
|
|
['update', '/path/to/subcommand/update.pl', 'Update things'], |
365
|
|
|
|
|
|
|
]; |
366
|
|
|
|
|
|
|
} |
367
|
|
|
|
|
|
|
|
368
|
|
|
|
|
|
|
# run() is only called if there are no matching sub commands |
369
|
|
|
|
|
|
|
run( |
370
|
|
|
|
|
|
|
'h # Print help', |
371
|
|
|
|
|
|
|
'completion-script # Print autocomplete script', |
372
|
|
|
|
|
|
|
sub { |
373
|
|
|
|
|
|
|
my ($app, @args) = @_; |
374
|
|
|
|
|
|
|
return print generate_completion_script() if $app->{'completion-script'}; |
375
|
|
|
|
|
|
|
return print extract_usage(); |
376
|
|
|
|
|
|
|
} |
377
|
|
|
|
|
|
|
); |
378
|
|
|
|
|
|
|
|
379
|
|
|
|
|
|
|
See L and L |
380
|
|
|
|
|
|
|
for more details. |
381
|
|
|
|
|
|
|
|
382
|
|
|
|
|
|
|
=head1 DESCRIPTION |
383
|
|
|
|
|
|
|
|
384
|
|
|
|
|
|
|
L is a module that helps you structure your scripts and integrates |
385
|
|
|
|
|
|
|
L with a very simple API. In addition it makes it very easy to |
386
|
|
|
|
|
|
|
test your script, since the script file can be sourced without actually being |
387
|
|
|
|
|
|
|
run. |
388
|
|
|
|
|
|
|
|
389
|
|
|
|
|
|
|
L also supports infinite nested L |
390
|
|
|
|
|
|
|
and a method for L this module with your script to prevent |
391
|
|
|
|
|
|
|
depending on a module from CPAN. |
392
|
|
|
|
|
|
|
|
393
|
|
|
|
|
|
|
=head1 APPLICATION METHODS |
394
|
|
|
|
|
|
|
|
395
|
|
|
|
|
|
|
These methods are optional, but can be defined in your script to override the |
396
|
|
|
|
|
|
|
default behavior. |
397
|
|
|
|
|
|
|
|
398
|
|
|
|
|
|
|
=head2 getopt_complete_reply |
399
|
|
|
|
|
|
|
|
400
|
|
|
|
|
|
|
$app->getopt_complete_reply; |
401
|
|
|
|
|
|
|
|
402
|
|
|
|
|
|
|
This method will be called instead of the L callback when the |
403
|
|
|
|
|
|
|
C and C environment variables are set. The default |
404
|
|
|
|
|
|
|
implementation will call L. |
405
|
|
|
|
|
|
|
|
406
|
|
|
|
|
|
|
See also "Completion" under L. |
407
|
|
|
|
|
|
|
|
408
|
|
|
|
|
|
|
=head2 getopt_configure |
409
|
|
|
|
|
|
|
|
410
|
|
|
|
|
|
|
@configure = $app->getopt_configure; |
411
|
|
|
|
|
|
|
|
412
|
|
|
|
|
|
|
This method can be defined if you want L to be set up |
413
|
|
|
|
|
|
|
differently. The default return value is: |
414
|
|
|
|
|
|
|
|
415
|
|
|
|
|
|
|
qw(bundling no_auto_abbrev no_ignore_case pass_through require_order) |
416
|
|
|
|
|
|
|
|
417
|
|
|
|
|
|
|
Note that the default "pass_through" item is to enable the default |
418
|
|
|
|
|
|
|
L to croak on invalid arguments, since |
419
|
|
|
|
|
|
|
L will by default just warn to STDERR about unknown arguments. |
420
|
|
|
|
|
|
|
|
421
|
|
|
|
|
|
|
=head2 getopt_load_subcommand |
422
|
|
|
|
|
|
|
|
423
|
|
|
|
|
|
|
$code = $app->getopt_load_subcommand($subcommand, [@ARGV]); |
424
|
|
|
|
|
|
|
|
425
|
|
|
|
|
|
|
Takes the subcommand found in the L list and the command |
426
|
|
|
|
|
|
|
line arguments and must return a CODE block. The default implementation is |
427
|
|
|
|
|
|
|
simply: |
428
|
|
|
|
|
|
|
|
429
|
|
|
|
|
|
|
$code = do($subcommand->[1]); |
430
|
|
|
|
|
|
|
|
431
|
|
|
|
|
|
|
=head2 getopt_post_process_argv |
432
|
|
|
|
|
|
|
|
433
|
|
|
|
|
|
|
$bool = $app->getopt_post_process_argv([@ARGV], {%state}); |
434
|
|
|
|
|
|
|
|
435
|
|
|
|
|
|
|
This method can be used to post process the options. C<%state> contains a key |
436
|
|
|
|
|
|
|
"valid" which is true or false, depending on the return value from |
437
|
|
|
|
|
|
|
L. |
438
|
|
|
|
|
|
|
|
439
|
|
|
|
|
|
|
This method can C and optionally set C<$!> to avoid calling the function |
440
|
|
|
|
|
|
|
passed to L. |
441
|
|
|
|
|
|
|
|
442
|
|
|
|
|
|
|
The default behavior is to check if the first item in C<$argv> starts with a |
443
|
|
|
|
|
|
|
hyphen, and C with an error message if so: |
444
|
|
|
|
|
|
|
|
445
|
|
|
|
|
|
|
Invalid argument or argument order: @$argv\n |
446
|
|
|
|
|
|
|
|
447
|
|
|
|
|
|
|
=head2 getopt_post_process_exit_value |
448
|
|
|
|
|
|
|
|
449
|
|
|
|
|
|
|
$exit_value = $app->getopt_post_process_exit_value($exit_value); |
450
|
|
|
|
|
|
|
|
451
|
|
|
|
|
|
|
A method to be called after the L function has been called. |
452
|
|
|
|
|
|
|
C<$exit_value> holds the return value from L which could be any value, |
453
|
|
|
|
|
|
|
not just 0-255. This value can then be changed to change the exit value from |
454
|
|
|
|
|
|
|
the program. |
455
|
|
|
|
|
|
|
|
456
|
|
|
|
|
|
|
sub getopt_post_process_exit_value ($app, $exit_value) { |
457
|
|
|
|
|
|
|
return int(1 + rand 10); |
458
|
|
|
|
|
|
|
} |
459
|
|
|
|
|
|
|
|
460
|
|
|
|
|
|
|
=head2 getopt_pre_process_argv |
461
|
|
|
|
|
|
|
|
462
|
|
|
|
|
|
|
$app->getopt_pre_process_argv($argv); |
463
|
|
|
|
|
|
|
|
464
|
|
|
|
|
|
|
This method can be defined to pre-process C<$argv> before it is passed on to |
465
|
|
|
|
|
|
|
L. Example: |
466
|
|
|
|
|
|
|
|
467
|
|
|
|
|
|
|
sub getopt_pre_process_argv ($app, $argv) { |
468
|
|
|
|
|
|
|
$app->{first_non_option} = shift @$argv if @$argv and $argv->[0] =~ m!^[a-z]!; |
469
|
|
|
|
|
|
|
} |
470
|
|
|
|
|
|
|
|
471
|
|
|
|
|
|
|
This method can C and optionally set C<$!> to avoid calling the actual |
472
|
|
|
|
|
|
|
L function. |
473
|
|
|
|
|
|
|
|
474
|
|
|
|
|
|
|
=head2 getopt_subcommands |
475
|
|
|
|
|
|
|
|
476
|
|
|
|
|
|
|
$subcommands = $app->getopt_subcommands; |
477
|
|
|
|
|
|
|
|
478
|
|
|
|
|
|
|
This method must be defined in the script to enable sub commands. The return |
479
|
|
|
|
|
|
|
value must be either C to disable subcommands or an array-ref of |
480
|
|
|
|
|
|
|
array-refs like this: |
481
|
|
|
|
|
|
|
|
482
|
|
|
|
|
|
|
[["subname", "/abs/path/to/sub-command-script", "help text"], ...] |
483
|
|
|
|
|
|
|
|
484
|
|
|
|
|
|
|
The first element in each array-ref "subname" will be matched against the first |
485
|
|
|
|
|
|
|
argument passed to the script, and when matched the "sub-command-script" will |
486
|
|
|
|
|
|
|
be sourced and run inside the same perl process. The sub command script must |
487
|
|
|
|
|
|
|
also use L for this to work properly. |
488
|
|
|
|
|
|
|
|
489
|
|
|
|
|
|
|
The sub-command will have C<$Getopt::App::SUBCOMMAND> set to the item found in |
490
|
|
|
|
|
|
|
the list. |
491
|
|
|
|
|
|
|
|
492
|
|
|
|
|
|
|
Instead of specifying a path, it is also possible to specify a method name, in |
493
|
|
|
|
|
|
|
case you want to include the sub commands inside the current script. Example: |
494
|
|
|
|
|
|
|
|
495
|
|
|
|
|
|
|
[["foo", "command_foo", "help text"], ...] |
496
|
|
|
|
|
|
|
|
497
|
|
|
|
|
|
|
See L for a working |
498
|
|
|
|
|
|
|
example. |
499
|
|
|
|
|
|
|
|
500
|
|
|
|
|
|
|
=head2 getopt_unknown_subcommand |
501
|
|
|
|
|
|
|
|
502
|
|
|
|
|
|
|
$exit_value = $app->getopt_unknown_subcommand($argv); |
503
|
|
|
|
|
|
|
|
504
|
|
|
|
|
|
|
Will be called when L is defined but C<$argv> does not |
505
|
|
|
|
|
|
|
match an item in the list. Default behavior is to C with an error message: |
506
|
|
|
|
|
|
|
|
507
|
|
|
|
|
|
|
Unknown subcommand: $argv->[0]\n |
508
|
|
|
|
|
|
|
|
509
|
|
|
|
|
|
|
Returning C instead of dying or a number (0-255) will cause the L |
510
|
|
|
|
|
|
|
callback to be called. |
511
|
|
|
|
|
|
|
|
512
|
|
|
|
|
|
|
=head1 EXPORTED FUNCTIONS |
513
|
|
|
|
|
|
|
|
514
|
|
|
|
|
|
|
=head2 capture |
515
|
|
|
|
|
|
|
|
516
|
|
|
|
|
|
|
use Getopt::App -capture; |
517
|
|
|
|
|
|
|
my $app = do '/path/to/bin/myapp'; |
518
|
|
|
|
|
|
|
my $array_ref = capture($app, [@ARGV]); # [$stdout, $stderr, $exit_value] |
519
|
|
|
|
|
|
|
|
520
|
|
|
|
|
|
|
Used to run an C<$app> and capture STDOUT, STDERR and the exit value in that |
521
|
|
|
|
|
|
|
order in C<$array_ref>. This function will also capture C. C<$@> will be |
522
|
|
|
|
|
|
|
set and captured in the second C<$array_ref> element, and C<$exit_value> will |
523
|
|
|
|
|
|
|
be set to C<$!>. |
524
|
|
|
|
|
|
|
|
525
|
|
|
|
|
|
|
This function is a very slimmed down alternative to L. |
526
|
|
|
|
|
|
|
The main reason why L exists in this package is that if something |
527
|
|
|
|
|
|
|
inside the C<$app> throws an exception, then it will be part of the captured |
528
|
|
|
|
|
|
|
C<$stderr> instead of making C throw an exception. |
529
|
|
|
|
|
|
|
|
530
|
|
|
|
|
|
|
L is however more robust than this function, so please |
531
|
|
|
|
|
|
|
try L out in case you find an edge case. |
532
|
|
|
|
|
|
|
|
533
|
|
|
|
|
|
|
=head2 extract_usage |
534
|
|
|
|
|
|
|
|
535
|
|
|
|
|
|
|
# Default to "SYNOPSIS" from current file |
536
|
|
|
|
|
|
|
my $str = extract_usage($section, $file); |
537
|
|
|
|
|
|
|
my $str = extract_usage($section); |
538
|
|
|
|
|
|
|
my $str = extract_usage(); |
539
|
|
|
|
|
|
|
|
540
|
|
|
|
|
|
|
Will extract a C<$section> from POD C<$file> and append command line option |
541
|
|
|
|
|
|
|
descriptions when called from inside of L. Command line options can |
542
|
|
|
|
|
|
|
optionally have a description with "spaces-hash-spaces-description", like this: |
543
|
|
|
|
|
|
|
|
544
|
|
|
|
|
|
|
run( |
545
|
|
|
|
|
|
|
'o|option # Some description', |
546
|
|
|
|
|
|
|
'v|verbose # Enable verbose output', |
547
|
|
|
|
|
|
|
sub { |
548
|
|
|
|
|
|
|
... |
549
|
|
|
|
|
|
|
}, |
550
|
|
|
|
|
|
|
); |
551
|
|
|
|
|
|
|
|
552
|
|
|
|
|
|
|
This function will I be exported if a function with the same name already |
553
|
|
|
|
|
|
|
exists in the script. |
554
|
|
|
|
|
|
|
|
555
|
|
|
|
|
|
|
=head2 new |
556
|
|
|
|
|
|
|
|
557
|
|
|
|
|
|
|
my $obj = new($class, %args); |
558
|
|
|
|
|
|
|
my $obj = new($class, \%args); |
559
|
|
|
|
|
|
|
|
560
|
|
|
|
|
|
|
This function is exported into the caller package so we can construct a new |
561
|
|
|
|
|
|
|
object: |
562
|
|
|
|
|
|
|
|
563
|
|
|
|
|
|
|
my $app = Application::Class->new(\%args); |
564
|
|
|
|
|
|
|
|
565
|
|
|
|
|
|
|
This function will I be exported if a function with the same name already |
566
|
|
|
|
|
|
|
exists in the script. |
567
|
|
|
|
|
|
|
|
568
|
|
|
|
|
|
|
=head2 run |
569
|
|
|
|
|
|
|
|
570
|
|
|
|
|
|
|
# Run a code block on valid @ARGV |
571
|
|
|
|
|
|
|
run(@rules, sub ($app, @extra) { ... }); |
572
|
|
|
|
|
|
|
|
573
|
|
|
|
|
|
|
# For testing |
574
|
|
|
|
|
|
|
my $cb = run(@rules, sub ($app, @extra) { ... }); |
575
|
|
|
|
|
|
|
my $exit_value = $cb->([@ARGV]); |
576
|
|
|
|
|
|
|
|
577
|
|
|
|
|
|
|
L can be used to call a callback when valid command line options is |
578
|
|
|
|
|
|
|
provided. On invalid arguments, warnings will be issued and the program exit |
579
|
|
|
|
|
|
|
with C<$?> set to 1. |
580
|
|
|
|
|
|
|
|
581
|
|
|
|
|
|
|
C<$app> inside the callback is a hash blessed to the caller package. The keys |
582
|
|
|
|
|
|
|
in the hash are the parsed command line options, while C<@extra> is the extra |
583
|
|
|
|
|
|
|
unparsed command line options. |
584
|
|
|
|
|
|
|
|
585
|
|
|
|
|
|
|
C<@rules> are the same options as L can take. Example: |
586
|
|
|
|
|
|
|
|
587
|
|
|
|
|
|
|
# app.pl -vv --name superwoman -o OptX cool beans |
588
|
|
|
|
|
|
|
run(qw(h|help v+ name=s o=s@), sub ($app, @extra) { |
589
|
|
|
|
|
|
|
die "No help here" if $app->{h}; |
590
|
|
|
|
|
|
|
warn $app->{v}; # 2 |
591
|
|
|
|
|
|
|
warn $app->{name}; # "superwoman" |
592
|
|
|
|
|
|
|
warn @{$app->{o}}; # "OptX" |
593
|
|
|
|
|
|
|
warn @extra; # "cool beans" |
594
|
|
|
|
|
|
|
return 0; # Used as exit code |
595
|
|
|
|
|
|
|
}); |
596
|
|
|
|
|
|
|
|
597
|
|
|
|
|
|
|
In the example above, C<@extra> gets populated, since there is a non-flag value |
598
|
|
|
|
|
|
|
"cool" after a list of valid command line options. |
599
|
|
|
|
|
|
|
|
600
|
|
|
|
|
|
|
=head1 METHODS |
601
|
|
|
|
|
|
|
|
602
|
|
|
|
|
|
|
=head2 bundle |
603
|
|
|
|
|
|
|
|
604
|
|
|
|
|
|
|
Getopt::App->bundle($path_to_script); |
605
|
|
|
|
|
|
|
Getopt::App->bundle($path_to_script, $fh); |
606
|
|
|
|
|
|
|
|
607
|
|
|
|
|
|
|
This method can be used to combine L and C<$path_to_script> into a |
608
|
|
|
|
|
|
|
a single script that does not need to have L installed from CPAN. |
609
|
|
|
|
|
|
|
This is for example useful for sysadmin scripts that otherwise only depends on |
610
|
|
|
|
|
|
|
core Perl modules. |
611
|
|
|
|
|
|
|
|
612
|
|
|
|
|
|
|
The script will be printed to C<$fh>, which defaults to C. |
613
|
|
|
|
|
|
|
|
614
|
|
|
|
|
|
|
Example usage: |
615
|
|
|
|
|
|
|
|
616
|
|
|
|
|
|
|
perl -MGetopt::App -e'Getopt::App->bundle(shift)' ./src/my-script.pl > ./bin/my-script; |
617
|
|
|
|
|
|
|
|
618
|
|
|
|
|
|
|
=head2 import |
619
|
|
|
|
|
|
|
|
620
|
|
|
|
|
|
|
use Getopt::App; |
621
|
|
|
|
|
|
|
use Getopt::App 'My::Script::Base', -signatures; |
622
|
|
|
|
|
|
|
use Getopt::App -capture; |
623
|
|
|
|
|
|
|
|
624
|
|
|
|
|
|
|
=over 2 |
625
|
|
|
|
|
|
|
|
626
|
|
|
|
|
|
|
=item * Default |
627
|
|
|
|
|
|
|
|
628
|
|
|
|
|
|
|
use Getopt::App; |
629
|
|
|
|
|
|
|
|
630
|
|
|
|
|
|
|
Passing in no flags will export the default functions L, |
631
|
|
|
|
|
|
|
L and L. In addition it will save you from a lot of typing, since |
632
|
|
|
|
|
|
|
it will also import the following: |
633
|
|
|
|
|
|
|
|
634
|
|
|
|
|
|
|
use strict; |
635
|
|
|
|
|
|
|
use warnings; |
636
|
|
|
|
|
|
|
use utf8; |
637
|
|
|
|
|
|
|
use feature ':5.16'; |
638
|
|
|
|
|
|
|
|
639
|
|
|
|
|
|
|
=item * Completion |
640
|
|
|
|
|
|
|
|
641
|
|
|
|
|
|
|
use Getopt::App -complete; |
642
|
|
|
|
|
|
|
|
643
|
|
|
|
|
|
|
Same as L, but will also load L and import |
644
|
|
|
|
|
|
|
L. |
645
|
|
|
|
|
|
|
|
646
|
|
|
|
|
|
|
=item * Signatures |
647
|
|
|
|
|
|
|
|
648
|
|
|
|
|
|
|
use Getopt::App -signatures; |
649
|
|
|
|
|
|
|
|
650
|
|
|
|
|
|
|
Same as L, but will also import L. This |
651
|
|
|
|
|
|
|
requires Perl 5.20+. |
652
|
|
|
|
|
|
|
|
653
|
|
|
|
|
|
|
=item * Class name |
654
|
|
|
|
|
|
|
|
655
|
|
|
|
|
|
|
package My::Script::Foo; |
656
|
|
|
|
|
|
|
use Getopt::App 'My::Script'; |
657
|
|
|
|
|
|
|
|
658
|
|
|
|
|
|
|
Same as L but will also make C inherit from |
659
|
|
|
|
|
|
|
L. Note that a package definition is required. |
660
|
|
|
|
|
|
|
|
661
|
|
|
|
|
|
|
=item * Capture |
662
|
|
|
|
|
|
|
|
663
|
|
|
|
|
|
|
use Getopt::App -capture; |
664
|
|
|
|
|
|
|
|
665
|
|
|
|
|
|
|
This will only export L. |
666
|
|
|
|
|
|
|
|
667
|
|
|
|
|
|
|
=back |
668
|
|
|
|
|
|
|
|
669
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
670
|
|
|
|
|
|
|
|
671
|
|
|
|
|
|
|
This library is free software. You can redistribute it and/or modify it under |
672
|
|
|
|
|
|
|
the same terms as Perl itself. |
673
|
|
|
|
|
|
|
|
674
|
|
|
|
|
|
|
=head1 AUTHOR |
675
|
|
|
|
|
|
|
|
676
|
|
|
|
|
|
|
Jan Henning Thorsen - C |
677
|
|
|
|
|
|
|
|
678
|
|
|
|
|
|
|
=cut |