line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Getopt::Alt; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# Created on: 2009-07-17 07:40:56 |
4
|
|
|
|
|
|
|
# Create by: Ivan Wills |
5
|
|
|
|
|
|
|
# $Id$ |
6
|
|
|
|
|
|
|
# $Revision$, $HeadURL$, $Date$ |
7
|
|
|
|
|
|
|
# $Revision$, $Source$, $Date$ |
8
|
|
|
|
|
|
|
|
9
|
7
|
|
|
7
|
|
512500
|
use Moose; |
|
7
|
|
|
|
|
3360068
|
|
|
7
|
|
|
|
|
50
|
|
10
|
7
|
|
|
7
|
|
53587
|
use warnings; |
|
7
|
|
|
|
|
15
|
|
|
7
|
|
|
|
|
247
|
|
11
|
7
|
|
|
7
|
|
5033
|
use version; |
|
7
|
|
|
|
|
14267
|
|
|
7
|
|
|
|
|
42
|
|
12
|
7
|
|
|
7
|
|
682
|
use Carp; |
|
7
|
|
|
|
|
16
|
|
|
7
|
|
|
|
|
603
|
|
13
|
7
|
|
|
7
|
|
3769
|
use English qw/ -no_match_vars /; |
|
7
|
|
|
|
|
27167
|
|
|
7
|
|
|
|
|
44
|
|
14
|
7
|
|
|
7
|
|
2692
|
use List::Util qw/uniq/; |
|
7
|
|
|
|
|
22
|
|
|
7
|
|
|
|
|
428
|
|
15
|
7
|
|
|
7
|
|
4141
|
use Getopt::Alt::Option qw/build_option/; |
|
7
|
|
|
|
|
37
|
|
|
7
|
|
|
|
|
37
|
|
16
|
7
|
|
|
7
|
|
4495
|
use Getopt::Alt::Exception; |
|
7
|
|
|
|
|
16
|
|
|
7
|
|
|
|
|
159
|
|
17
|
7
|
|
|
7
|
|
42
|
use Try::Tiny; |
|
7
|
|
|
|
|
15
|
|
|
7
|
|
|
|
|
391
|
|
18
|
7
|
|
|
7
|
|
6451
|
use Path::Tiny; |
|
7
|
|
|
|
|
81084
|
|
|
7
|
|
|
|
|
371
|
|
19
|
7
|
|
|
7
|
|
3842
|
use Config::Any; |
|
7
|
|
|
|
|
62210
|
|
|
7
|
|
|
|
|
246
|
|
20
|
7
|
|
|
7
|
|
3976
|
use File::HomeDir; |
|
7
|
|
|
|
|
40849
|
|
|
7
|
|
|
|
|
779
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
use overload ( |
23
|
1
|
|
|
1
|
|
49
|
'@{}' => sub { $_[0]->files }, |
24
|
6
|
|
|
6
|
|
1976
|
'bool' => sub { 1 }, |
25
|
7
|
|
|
7
|
|
1463
|
); |
|
7
|
|
|
|
|
20
|
|
|
7
|
|
|
|
|
75
|
|
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
Moose::Exporter->setup_import_methods( |
28
|
|
|
|
|
|
|
as_is => [qw/get_options/], |
29
|
|
|
|
|
|
|
); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
our $VERSION = version->new('0.5.4'); |
32
|
|
|
|
|
|
|
our $EXIT = 1; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
has options => ( |
35
|
|
|
|
|
|
|
is => 'rw', |
36
|
|
|
|
|
|
|
isa => 'Str', |
37
|
|
|
|
|
|
|
default => 'Getopt::Alt::Dynamic', |
38
|
|
|
|
|
|
|
); |
39
|
|
|
|
|
|
|
has opt => ( |
40
|
|
|
|
|
|
|
is => 'rw', |
41
|
|
|
|
|
|
|
isa => 'Getopt::Alt::Dynamic', |
42
|
|
|
|
|
|
|
clearer => 'clear_opt', |
43
|
|
|
|
|
|
|
); |
44
|
|
|
|
|
|
|
has default => ( |
45
|
|
|
|
|
|
|
is => 'rw', |
46
|
|
|
|
|
|
|
isa => 'HashRef', |
47
|
|
|
|
|
|
|
default => sub {{}}, |
48
|
|
|
|
|
|
|
); |
49
|
|
|
|
|
|
|
has files => ( |
50
|
|
|
|
|
|
|
is => 'rw', |
51
|
|
|
|
|
|
|
isa => 'ArrayRef[Str]', |
52
|
|
|
|
|
|
|
default => sub {[]}, |
53
|
|
|
|
|
|
|
); |
54
|
|
|
|
|
|
|
has bundle => ( |
55
|
|
|
|
|
|
|
is => 'rw', |
56
|
|
|
|
|
|
|
isa => 'Bool', |
57
|
|
|
|
|
|
|
default => 1, |
58
|
|
|
|
|
|
|
); |
59
|
|
|
|
|
|
|
has ignore_case => ( |
60
|
|
|
|
|
|
|
is => 'rw', |
61
|
|
|
|
|
|
|
isa => 'Bool', |
62
|
|
|
|
|
|
|
default => 1, |
63
|
|
|
|
|
|
|
); |
64
|
|
|
|
|
|
|
has help_package => ( |
65
|
|
|
|
|
|
|
is => 'rw', |
66
|
|
|
|
|
|
|
isa => 'Str', |
67
|
|
|
|
|
|
|
); |
68
|
|
|
|
|
|
|
has help_packages => ( |
69
|
|
|
|
|
|
|
is => 'rw', |
70
|
|
|
|
|
|
|
isa => 'HashRef[Str]', |
71
|
|
|
|
|
|
|
); |
72
|
|
|
|
|
|
|
has help_pre => ( |
73
|
|
|
|
|
|
|
is => 'rw', |
74
|
|
|
|
|
|
|
isa => 'CodeRef', |
75
|
|
|
|
|
|
|
); |
76
|
|
|
|
|
|
|
has help_post => ( |
77
|
|
|
|
|
|
|
is => 'rw', |
78
|
|
|
|
|
|
|
isa => 'CodeRef', |
79
|
|
|
|
|
|
|
); |
80
|
|
|
|
|
|
|
has helper => ( |
81
|
|
|
|
|
|
|
is => 'rw', |
82
|
|
|
|
|
|
|
isa => 'Bool', |
83
|
|
|
|
|
|
|
); |
84
|
|
|
|
|
|
|
has cmd => ( |
85
|
|
|
|
|
|
|
is => 'rw', |
86
|
|
|
|
|
|
|
isa => 'Str', |
87
|
|
|
|
|
|
|
clearer => 'clear_cmd', |
88
|
|
|
|
|
|
|
documentation => 'The found sub-command', |
89
|
|
|
|
|
|
|
); |
90
|
|
|
|
|
|
|
has sub_command => ( |
91
|
|
|
|
|
|
|
is => 'rw', |
92
|
|
|
|
|
|
|
predicate => 'has_sub_command', |
93
|
|
|
|
|
|
|
documentation => <<'DOC', |
94
|
|
|
|
|
|
|
if true (== 1) processing of args stops at first non-defined parameter, if |
95
|
|
|
|
|
|
|
a HASH ref the keys are assumed to be the allowed sub commands and the values |
96
|
|
|
|
|
|
|
are assumed to be parameters to passed to get_options where the generated |
97
|
|
|
|
|
|
|
options will be a sub object of generated options object. Finally if this |
98
|
|
|
|
|
|
|
is a sub ref it will be called with self and the rest of ARGV |
99
|
|
|
|
|
|
|
DOC |
100
|
|
|
|
|
|
|
); |
101
|
|
|
|
|
|
|
has aliases => ( |
102
|
|
|
|
|
|
|
is => 'rw', |
103
|
|
|
|
|
|
|
isa => 'HashRef[ArrayRef]', |
104
|
|
|
|
|
|
|
default => sub {{}}, |
105
|
|
|
|
|
|
|
documentation => 'Stores the list of aliases sub-commands can have', |
106
|
|
|
|
|
|
|
); |
107
|
|
|
|
|
|
|
has default_sub_command => ( |
108
|
|
|
|
|
|
|
is => 'rw', |
109
|
|
|
|
|
|
|
isa => 'Str', |
110
|
|
|
|
|
|
|
predicate => 'has_default_sub_command', |
111
|
|
|
|
|
|
|
); |
112
|
|
|
|
|
|
|
has auto_complete => ( |
113
|
|
|
|
|
|
|
is => 'rw', |
114
|
|
|
|
|
|
|
isa => 'CodeRef', |
115
|
|
|
|
|
|
|
predicate => 'has_auto_complete', |
116
|
|
|
|
|
|
|
); |
117
|
|
|
|
|
|
|
has auto_complete_shortener => ( |
118
|
|
|
|
|
|
|
is => 'rw', |
119
|
|
|
|
|
|
|
isa => 'CodeRef', |
120
|
|
|
|
|
|
|
predicate => 'has_auto_complete_shortener', |
121
|
|
|
|
|
|
|
); |
122
|
|
|
|
|
|
|
has name => ( |
123
|
|
|
|
|
|
|
is => 'rw', |
124
|
|
|
|
|
|
|
isa => 'Str', |
125
|
|
|
|
|
|
|
default => sub { path($0)->basename }, |
126
|
|
|
|
|
|
|
); |
127
|
|
|
|
|
|
|
has config => ( |
128
|
|
|
|
|
|
|
is => 'rw', |
129
|
|
|
|
|
|
|
isa => 'HashRef', |
130
|
|
|
|
|
|
|
predicate => 'has_config', |
131
|
|
|
|
|
|
|
); |
132
|
|
|
|
|
|
|
has conf_prefix => ( |
133
|
|
|
|
|
|
|
is => 'rw', |
134
|
|
|
|
|
|
|
isa => 'Str', |
135
|
|
|
|
|
|
|
default => '.', |
136
|
|
|
|
|
|
|
); |
137
|
|
|
|
|
|
|
has conf_section => ( |
138
|
|
|
|
|
|
|
is => 'rw', |
139
|
|
|
|
|
|
|
isa => 'Str', |
140
|
|
|
|
|
|
|
predicate => 'has_conf_section', |
141
|
|
|
|
|
|
|
); |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
my $count = 1; |
144
|
|
|
|
|
|
|
around BUILDARGS => sub { |
145
|
|
|
|
|
|
|
my ($orig, $class, @params) = @_; |
146
|
|
|
|
|
|
|
my %param; |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
if (ref $params[0] eq 'HASH' && ref $params[1] eq 'ARRAY') { |
149
|
|
|
|
|
|
|
%param = %{ $params[0] }; |
150
|
|
|
|
|
|
|
@params = @{ $params[1] }; |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
elsif ( !%param && ref $params[0] eq 'HASH' ) { |
153
|
|
|
|
|
|
|
%param = %{ shift @params }; |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
if ( !exists $param{helper} || $param{helper} ) { |
157
|
|
|
|
|
|
|
unshift @params, ( |
158
|
|
|
|
|
|
|
'help', |
159
|
|
|
|
|
|
|
'man', |
160
|
|
|
|
|
|
|
'version', |
161
|
|
|
|
|
|
|
'auto_complete|auto-complete=i', |
162
|
|
|
|
|
|
|
'auto_complete_list|auto-complete-list!', |
163
|
|
|
|
|
|
|
); |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
if ( @params ) { |
167
|
|
|
|
|
|
|
$param{options} = _build_option_class( |
168
|
|
|
|
|
|
|
$param{options} || 'Getopt::Alt::Dynamic', |
169
|
|
|
|
|
|
|
@params, |
170
|
|
|
|
|
|
|
); |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
if (0 && $param{sub_command} && ref $param{sub_command} eq 'HASH') { |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
# build up all the sub command options |
175
|
|
|
|
|
|
|
for my $sub (keys %{ $param{sub_command} }) { |
176
|
|
|
|
|
|
|
$param{sub_command}{$sub} = _build_option_class( |
177
|
|
|
|
|
|
|
$param{options}, |
178
|
|
|
|
|
|
|
$param{sub_command}{$sub}, |
179
|
|
|
|
|
|
|
); |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
return $class->$orig(%param); |
185
|
|
|
|
|
|
|
}; |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
sub _build_option_class { |
188
|
34
|
|
|
34
|
|
142
|
my ($base_class, @params) = @_; |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
# construct a class of options passing |
191
|
34
|
|
|
|
|
131
|
my $class_name = 'Getopt::Alt::Dynamic::A' . $count++; |
192
|
34
|
|
|
|
|
341
|
my $option_class = Moose::Meta::Class->create( |
193
|
|
|
|
|
|
|
$class_name, |
194
|
|
|
|
|
|
|
superclasses => [ $base_class ], |
195
|
|
|
|
|
|
|
); |
196
|
|
|
|
|
|
|
|
197
|
34
|
|
|
|
|
86945
|
while ( my $option = shift @params ) { |
198
|
203
|
|
|
|
|
2782
|
build_option($option_class, $option); |
199
|
|
|
|
|
|
|
} |
200
|
|
|
|
|
|
|
|
201
|
33
|
|
|
|
|
609
|
return $class_name; |
202
|
|
|
|
|
|
|
} |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
sub BUILD { |
205
|
34
|
|
|
34
|
1
|
70492
|
my ($self) = @_; |
206
|
|
|
|
|
|
|
|
207
|
34
|
|
|
|
|
1068
|
my $basename = $self->name; |
208
|
34
|
|
|
|
|
912
|
my $prefix = $self->conf_prefix; |
209
|
34
|
|
|
|
|
363
|
my $conf = Config::Any->load_stems({ |
210
|
|
|
|
|
|
|
stems => [ |
211
|
|
|
|
|
|
|
"$prefix$basename", |
212
|
|
|
|
|
|
|
File::HomeDir->my_home . "/$prefix$basename", "/etc/$basename", |
213
|
|
|
|
|
|
|
], |
214
|
|
|
|
|
|
|
use_ext => 1, |
215
|
|
|
|
|
|
|
}); |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
$conf = { |
218
|
2
|
|
|
|
|
13
|
map { %$_ } |
219
|
2
|
|
|
|
|
11
|
map { values %$_ } |
220
|
34
|
|
|
|
|
639692
|
reverse @{ $conf } |
|
34
|
|
|
|
|
153
|
|
221
|
|
|
|
|
|
|
}; |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
# perlcritic is confused here combining hashes is not the same as comma separated arguments |
224
|
34
|
|
|
|
|
102
|
$self->default({ %{$self->default}, %$conf, }); ## no critic |
|
34
|
|
|
|
|
1352
|
|
225
|
34
|
|
|
|
|
999
|
$self->config($conf); |
226
|
|
|
|
|
|
|
|
227
|
34
|
100
|
|
|
|
139
|
if ($conf->{aliases}) { |
228
|
1
|
|
|
|
|
2
|
for my $alias (keys %{ $conf->{aliases} }) { |
|
1
|
|
|
|
|
5
|
|
229
|
1
|
|
|
|
|
33
|
$self->aliases->{$alias} = [ split /\s+/xms, $conf->{aliases}{$alias} ]; |
230
|
|
|
|
|
|
|
} |
231
|
|
|
|
|
|
|
} |
232
|
|
|
|
|
|
|
|
233
|
34
|
|
|
|
|
173
|
return; |
234
|
|
|
|
|
|
|
} |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
sub get_options { |
237
|
15
|
|
|
15
|
1
|
11101
|
my @args = @_; |
238
|
15
|
|
|
|
|
46
|
my $caller = caller; |
239
|
|
|
|
|
|
|
|
240
|
15
|
100
|
100
|
|
|
97
|
if ( @args > 2 && ref $args[0] eq 'HASH' && ref $args[1] ne 'ARRAY' ) { |
|
|
|
66
|
|
|
|
|
241
|
3
|
|
|
|
|
6
|
my $options = shift @args; |
242
|
3
|
|
|
|
|
14
|
@args = ( { default => $options}, [ @args ] ); |
243
|
|
|
|
|
|
|
} |
244
|
|
|
|
|
|
|
|
245
|
15
|
|
|
|
|
35
|
my $self; |
246
|
|
|
|
|
|
|
try { |
247
|
15
|
|
|
15
|
|
849
|
$self = __PACKAGE__->new(@args); |
248
|
|
|
|
|
|
|
|
249
|
14
|
50
|
33
|
|
|
526
|
$self->help_package($caller) if !$self->help_package || $self->help_package eq __PACKAGE__; |
250
|
|
|
|
|
|
|
|
251
|
14
|
|
|
|
|
74
|
$self->process(); |
252
|
|
|
|
|
|
|
} |
253
|
|
|
|
|
|
|
catch { |
254
|
7
|
100
|
66
|
7
|
|
93284
|
if ( ref $_ && ref $_ eq 'Getopt::Alt::Exception' && $_->help ) { |
|
|
|
100
|
|
|
|
|
255
|
3
|
|
|
|
|
26
|
die $_; |
256
|
|
|
|
|
|
|
} |
257
|
|
|
|
|
|
|
|
258
|
4
|
|
|
|
|
45
|
warn $_; |
259
|
4
|
|
|
|
|
2604
|
$self = __PACKAGE__->new(); |
260
|
|
|
|
|
|
|
|
261
|
4
|
50
|
33
|
|
|
914
|
$self->help_package($caller) if !$self->help_package || $self->help_package eq __PACKAGE__; |
262
|
|
|
|
|
|
|
|
263
|
4
|
|
|
|
|
38
|
$self->_show_help(1); |
264
|
15
|
|
|
|
|
133
|
}; |
265
|
|
|
|
|
|
|
|
266
|
8
|
50
|
|
|
|
234
|
return if !defined $self; |
267
|
|
|
|
|
|
|
|
268
|
8
|
50
|
|
|
|
222
|
return wantarray ? ( $self->opt, $self->cmd, $self ) : $self->opt; |
269
|
|
|
|
|
|
|
} |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
sub process { |
272
|
87
|
|
|
87
|
1
|
3204
|
my ($self, @args) = @_; |
273
|
87
|
|
|
|
|
166
|
my $passed_args = scalar @args; |
274
|
87
|
100
|
|
|
|
296
|
@args = $passed_args ? @args : @ARGV; |
275
|
87
|
|
|
|
|
2969
|
$self->clear_opt; |
276
|
87
|
|
|
|
|
9242
|
$self->clear_cmd; |
277
|
87
|
|
|
|
|
2097
|
$self->files([]); |
278
|
|
|
|
|
|
|
|
279
|
87
|
|
|
|
|
216
|
my @args_orig = @args; |
280
|
87
|
|
|
|
|
2337
|
my $class = $self->options; |
281
|
87
|
|
|
|
|
163
|
$self->opt( $class->new( %{ $self->default } ) ); |
|
87
|
|
|
|
|
1994
|
|
282
|
87
|
|
|
|
|
180
|
my @errors; |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
ARG: |
285
|
87
|
|
|
|
|
288
|
while (my $arg = shift @args) { |
286
|
94
|
|
|
|
|
163
|
my $action = ''; |
287
|
|
|
|
|
|
|
try { |
288
|
94
|
|
|
94
|
|
4157
|
my ($long, $short, $arg_data); |
289
|
94
|
100
|
|
|
|
597
|
if ( $arg =~ /^-- (\w[^=\s]+) (?:= (.*) )?/xms ) { |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
290
|
33
|
|
|
|
|
101
|
$long = $1; |
291
|
33
|
|
|
|
|
60
|
$arg_data = $2; |
292
|
|
|
|
|
|
|
} |
293
|
|
|
|
|
|
|
elsif ( $arg =~ /^- (\w) =? (.*)/xms ) { |
294
|
55
|
|
|
|
|
152
|
$short = $1; |
295
|
55
|
|
|
|
|
119
|
$arg_data = $2; |
296
|
|
|
|
|
|
|
} |
297
|
|
|
|
|
|
|
elsif ( $arg eq '--' ) { |
298
|
0
|
0
|
0
|
|
|
0
|
if ( $self->auto_complete && $self->opt->auto_complete && |
|
|
|
0
|
|
|
|
|
299
|
|
|
|
|
|
|
path($0)->basename eq path($args[0])->basename |
300
|
|
|
|
|
|
|
) { |
301
|
0
|
|
|
|
|
0
|
shift @args; |
302
|
|
|
|
|
|
|
} |
303
|
|
|
|
|
|
|
|
304
|
0
|
0
|
0
|
|
|
0
|
if ( $self->opt->auto_complete |
|
|
|
0
|
|
|
|
|
305
|
|
|
|
|
|
|
&& $self->sub_command |
306
|
|
|
|
|
|
|
&& $self->has_auto_complete_shortener |
307
|
|
|
|
|
|
|
) { |
308
|
0
|
|
|
|
|
0
|
@args = $self->auto_complete_shortener->($self, @args); |
309
|
|
|
|
|
|
|
} |
310
|
0
|
|
|
|
|
0
|
push @{ $self->files }, @args; |
|
0
|
|
|
|
|
0
|
|
311
|
0
|
|
|
|
|
0
|
die "last\n"; |
312
|
|
|
|
|
|
|
} |
313
|
|
|
|
|
|
|
else { |
314
|
6
|
|
|
|
|
55
|
push @{ $self->files }, $arg; |
|
6
|
|
|
|
|
230
|
|
315
|
6
|
100
|
|
|
|
152
|
die $self->sub_command ? "last\n" : "next\n"; |
316
|
|
|
|
|
|
|
} |
317
|
|
|
|
|
|
|
|
318
|
88
|
|
|
|
|
270
|
my ($opt, $new_value) = $self->best_option( $long, $short ); |
319
|
83
|
100
|
|
|
|
238
|
if (defined $new_value) { |
320
|
3
|
|
|
|
|
86
|
$long = $opt->name; |
321
|
3
|
|
|
|
|
6
|
$short = undef; |
322
|
3
|
|
|
|
|
13
|
($arg_data) = $arg =~ /^--?(\d+)$/; |
323
|
|
|
|
|
|
|
} |
324
|
83
|
|
|
|
|
1892
|
$opt->value( $self->opt->{ $opt->name } ); |
325
|
|
|
|
|
|
|
|
326
|
83
|
|
|
|
|
412
|
my ($value, $used) = $opt->process( $long, $short, $arg_data, \@args ); |
327
|
78
|
|
|
|
|
2123
|
my $opt_name = $opt->name; |
328
|
78
|
50
|
33
|
|
|
1730
|
if ( $self->opt->auto_complete && $opt_name eq 'auto_complete_list' ) { |
329
|
0
|
|
|
|
|
0
|
print join ' ', $self->list_options; |
330
|
0
|
0
|
|
|
|
0
|
$EXIT ? exit 0 : return; |
331
|
|
|
|
|
|
|
} |
332
|
78
|
|
|
|
|
2060
|
$self->opt->{$opt->name} = $value; |
333
|
|
|
|
|
|
|
|
334
|
78
|
100
|
100
|
|
|
317
|
if ( !$used && $short && defined $arg_data && length $arg_data ) { |
|
|
|
66
|
|
|
|
|
|
|
|
100
|
|
|
|
|
335
|
2
|
|
|
|
|
49
|
unshift @args, '-' . $arg_data; |
336
|
|
|
|
|
|
|
} |
337
|
78
|
0
|
33
|
|
|
2241
|
if ($self->has_conf_section |
|
|
|
33
|
|
|
|
|
|
|
|
0
|
|
|
|
|
338
|
|
|
|
|
|
|
&& $self->conf_section |
339
|
|
|
|
|
|
|
&& $self->conf_section eq $opt_name |
340
|
|
|
|
|
|
|
&& @args_orig |
341
|
|
|
|
|
|
|
) { |
342
|
|
|
|
|
|
|
$self->opt( |
343
|
|
|
|
|
|
|
$class->new( |
344
|
0
|
|
|
|
|
0
|
%{ $self->default }, |
345
|
0
|
|
|
|
|
0
|
%{ $self->config->{$self->conf_section}{$value} }, |
|
0
|
|
|
|
|
0
|
|
346
|
|
|
|
|
|
|
) |
347
|
|
|
|
|
|
|
); |
348
|
|
|
|
|
|
|
# restart the process |
349
|
0
|
|
|
|
|
0
|
@args = @args_orig; |
350
|
0
|
|
|
|
|
0
|
@args_orig = (); |
351
|
|
|
|
|
|
|
} |
352
|
|
|
|
|
|
|
} |
353
|
|
|
|
|
|
|
catch { |
354
|
16
|
100
|
|
16
|
|
44954
|
if ( $_ eq "next\n" ) { |
|
|
100
|
|
|
|
|
|
355
|
2
|
|
|
|
|
9
|
$action = 'next'; |
356
|
|
|
|
|
|
|
} |
357
|
|
|
|
|
|
|
elsif ( $_ eq "last\n" ) { |
358
|
|
|
|
|
|
|
# last means we have found a sub command we should see if it is an alias |
359
|
4
|
50
|
|
|
|
143
|
if ($self->aliases->{$arg}) { |
360
|
0
|
|
|
|
|
0
|
$self->files->[-1] = shift @{ $self->aliases->{$arg} }; |
|
0
|
|
|
|
|
0
|
|
361
|
0
|
|
|
|
|
0
|
my @new_args = @{ $self->aliases->{$arg} }; |
|
0
|
|
|
|
|
0
|
|
362
|
0
|
|
|
|
|
0
|
unshift @args, @new_args; |
363
|
|
|
|
|
|
|
} |
364
|
|
|
|
|
|
|
|
365
|
4
|
|
|
|
|
19
|
$action = 'last'; |
366
|
|
|
|
|
|
|
} |
367
|
|
|
|
|
|
|
else { |
368
|
10
|
100
|
66
|
|
|
69
|
$_ = $_->[0] if ref $_ eq 'ARRAY' && @$_ == 1; |
369
|
|
|
|
|
|
|
|
370
|
10
|
50
|
33
|
|
|
349
|
if ( $self->has_auto_complete && $self->opt->auto_complete ) { |
371
|
0
|
|
|
|
|
0
|
push @errors, $_; |
372
|
|
|
|
|
|
|
} |
373
|
|
|
|
|
|
|
else { |
374
|
10
|
|
|
|
|
72
|
die $_; |
375
|
|
|
|
|
|
|
} |
376
|
|
|
|
|
|
|
} |
377
|
94
|
|
|
|
|
973
|
}; |
378
|
84
|
100
|
|
|
|
2594
|
next if $action eq 'next'; |
379
|
82
|
100
|
|
|
|
334
|
last if $action eq 'last'; |
380
|
|
|
|
|
|
|
} |
381
|
|
|
|
|
|
|
|
382
|
77
|
100
|
|
|
|
2440
|
if ( $self->has_sub_command ) { |
383
|
4
|
50
|
33
|
|
|
9
|
shift @{ $self->files } if @{ $self->files } && $self->files->[0] eq '--'; |
|
0
|
|
|
|
|
0
|
|
|
4
|
|
|
|
|
88
|
|
384
|
|
|
|
|
|
|
|
385
|
4
|
50
|
33
|
|
|
10
|
if ( ! @{ $self->files } && @args ) { |
|
4
|
|
|
|
|
93
|
|
386
|
0
|
|
|
|
|
0
|
$self->files([ @args ]); |
387
|
|
|
|
|
|
|
} |
388
|
|
|
|
|
|
|
|
389
|
4
|
50
|
33
|
|
|
95
|
$self->cmd( shift @{ $self->files } ) if ! $self->cmd && @{ $self->files }; |
|
4
|
|
|
|
|
90
|
|
|
4
|
|
|
|
|
88
|
|
390
|
|
|
|
|
|
|
} |
391
|
77
|
100
|
66
|
|
|
570
|
if ( !$passed_args && $self->files ) { |
392
|
13
|
|
|
|
|
41
|
@ARGV = ( @{ $self->files }, @args ); ## no critic |
|
13
|
|
|
|
|
294
|
|
393
|
|
|
|
|
|
|
} |
394
|
|
|
|
|
|
|
|
395
|
77
|
100
|
|
|
|
2138
|
if ( $self->has_sub_command ) { |
396
|
4
|
100
|
33
|
|
|
89
|
if ( ref $self->sub_command eq 'HASH' |
|
|
50
|
66
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
33
|
|
|
|
|
397
|
|
|
|
|
|
|
&& ( |
398
|
|
|
|
|
|
|
! $self->has_auto_complete |
399
|
|
|
|
|
|
|
|| ( $self->cmd && $self->sub_command->{ $self->cmd } ) |
400
|
|
|
|
|
|
|
) |
401
|
|
|
|
|
|
|
) { |
402
|
3
|
50
|
|
|
|
73
|
if ( ! $self->sub_command->{$self->cmd} ) { |
403
|
0
|
|
|
|
|
0
|
warn 'Unknown command "' . $self->cmd . "\"!\n"; |
404
|
0
|
0
|
|
|
|
0
|
die Getopt::Alt::Exception->new( |
405
|
|
|
|
|
|
|
message => "Unknown command '$self->cmd'", |
406
|
|
|
|
|
|
|
help => 1, |
407
|
|
|
|
|
|
|
) if !$self->help_package; |
408
|
0
|
|
|
|
|
0
|
$self->_show_help(1, 'Unknown command "' . $self->cmd . "\"!\n"); |
409
|
|
|
|
|
|
|
} |
410
|
|
|
|
|
|
|
|
411
|
3
|
50
|
|
|
|
70
|
if ( ref $self->sub_command->{$self->cmd} eq 'ARRAY' ) { |
412
|
|
|
|
|
|
|
# make a copy of the sub command |
413
|
3
|
|
|
|
|
8
|
my $sub = [ @{$self->sub_command->{$self->cmd}} ]; |
|
3
|
|
|
|
|
67
|
|
414
|
|
|
|
|
|
|
# check the style |
415
|
3
|
100
|
66
|
|
|
28
|
my $options = @$sub == 2 |
416
|
|
|
|
|
|
|
&& ref $sub->[0] eq 'HASH' |
417
|
|
|
|
|
|
|
&& ref $sub->[1] eq 'ARRAY' ? shift @$sub : {}; |
418
|
3
|
100
|
|
|
|
14
|
my $opt_args = %$options ? $sub->[0] : $sub; |
419
|
|
|
|
|
|
|
|
420
|
|
|
|
|
|
|
# build sub command object |
421
|
|
|
|
|
|
|
my $sub_obj = Getopt::Alt->new( |
422
|
|
|
|
|
|
|
{ |
423
|
|
|
|
|
|
|
helper => $self->helper, |
424
|
3
|
|
|
|
|
74
|
%{ $options }, ## no critic |
425
|
|
|
|
|
|
|
options => $self->options, # inherit this objects options |
426
|
3
|
100
|
|
|
|
78
|
default => { %{ $self->opt }, %{ $options->{default} || {} } }, |
|
3
|
|
|
|
|
66
|
|
|
3
|
|
|
|
|
37
|
|
427
|
|
|
|
|
|
|
}, |
428
|
|
|
|
|
|
|
$opt_args |
429
|
|
|
|
|
|
|
); |
430
|
3
|
|
|
|
|
47
|
local @ARGV = (); |
431
|
3
|
50
|
|
|
|
83
|
if ( $self->opt->auto_complete ) { |
432
|
0
|
|
|
|
|
0
|
push @args, '--auto-complete', $self->opt->auto_complete, '--'; |
433
|
|
|
|
|
|
|
} |
434
|
3
|
|
|
|
|
45
|
$sub_obj->process(@args); |
435
|
3
|
|
|
|
|
73
|
$self->opt( $sub_obj->opt ); |
436
|
3
|
|
|
|
|
73
|
$self->files( $sub_obj->files ); |
437
|
|
|
|
|
|
|
} |
438
|
|
|
|
|
|
|
} |
439
|
|
|
|
|
|
|
elsif ( $self->sub_command =~ /^[A-Z].*::$/ |
440
|
|
|
|
|
|
|
&& ( |
441
|
|
|
|
|
|
|
! $self->has_auto_complete |
442
|
|
|
|
|
|
|
|| ( $self->cmd && $self->sub_command->{ $self->cmd } ) |
443
|
|
|
|
|
|
|
) |
444
|
|
|
|
|
|
|
) { |
445
|
|
|
|
|
|
|
# object based subcommands |
446
|
0
|
|
0
|
|
|
0
|
my $run = $self->sub_module_method || 'run'; |
447
|
|
|
|
|
|
|
} |
448
|
|
|
|
|
|
|
} |
449
|
|
|
|
|
|
|
|
450
|
77
|
100
|
|
|
|
2145
|
if ( $self->help_package ) { |
451
|
11
|
100
|
33
|
|
|
242
|
if ( $self->opt->{version} ) { |
|
|
100
|
66
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
452
|
1
|
|
|
|
|
10
|
my ($name) = $PROGRAM_NAME =~ m{^.*/(.*?)$}mxs; |
453
|
1
|
50
|
|
|
|
6
|
my $version = defined $main::VERSION ? $main::VERSION : 'undef'; |
454
|
1
|
|
|
|
|
43
|
die Getopt::Alt::Exception->new( |
455
|
|
|
|
|
|
|
message => "$name Version = $version\n", |
456
|
|
|
|
|
|
|
help => 1, |
457
|
|
|
|
|
|
|
); |
458
|
|
|
|
|
|
|
} |
459
|
|
|
|
|
|
|
elsif ( $self->opt->{man} ) { |
460
|
1
|
|
|
|
|
6
|
$self->_show_help(2); |
461
|
|
|
|
|
|
|
} |
462
|
|
|
|
|
|
|
elsif ( $self->opt->{help} ) { |
463
|
1
|
|
|
|
|
6
|
$self->_show_help(1); |
464
|
|
|
|
|
|
|
} |
465
|
|
|
|
|
|
|
elsif ( $self->auto_complete && $self->opt->auto_complete ) { |
466
|
0
|
|
|
|
|
0
|
$self->complete(\@errors); |
467
|
|
|
|
|
|
|
} |
468
|
|
|
|
|
|
|
elsif ( $self->sub_command && ! $self->cmd ) { |
469
|
0
|
|
|
|
|
0
|
$self->_show_help(1); |
470
|
|
|
|
|
|
|
} |
471
|
|
|
|
|
|
|
} |
472
|
|
|
|
|
|
|
|
473
|
74
|
|
|
|
|
243
|
return $self; |
474
|
|
|
|
|
|
|
} |
475
|
|
|
|
|
|
|
|
476
|
|
|
|
|
|
|
sub complete { |
477
|
4
|
|
|
4
|
1
|
10645
|
my ($self, $errors) = @_; |
478
|
|
|
|
|
|
|
|
479
|
4
|
100
|
66
|
|
|
154
|
if ( $self->sub_command && ref $self->sub_command && !$self->cmd ) { |
|
|
50
|
66
|
|
|
|
|
|
|
|
33
|
|
|
|
|
480
|
2
|
|
|
|
|
6
|
my $cmd = shift @ARGV; |
481
|
2
|
100
|
|
|
|
8
|
my @sub_command = grep { $cmd ? /$cmd/ : 1 } sort keys %{ $self->sub_command }; |
|
4
|
|
|
|
|
27
|
|
|
2
|
|
|
|
|
47
|
|
482
|
2
|
|
|
|
|
119
|
print join ' ', @sub_command; |
483
|
|
|
|
|
|
|
} |
484
|
|
|
|
|
|
|
elsif ( $ARGV[-1] && $ARGV[-1] =~ /^-/xms ) { |
485
|
2
|
|
|
|
|
6
|
my $cmd = $ARGV[-1]; |
486
|
2
|
50
|
|
|
|
7
|
print join ' ', grep { $cmd ? /^$cmd/ : 1 } sort $self->list_options; |
|
14
|
|
|
|
|
139
|
|
487
|
|
|
|
|
|
|
} |
488
|
|
|
|
|
|
|
else { |
489
|
|
|
|
|
|
|
# run the auto complete method |
490
|
0
|
|
|
|
|
0
|
$self->auto_complete->($self, $self->opt->auto_complete, $errors); |
491
|
|
|
|
|
|
|
} |
492
|
|
|
|
|
|
|
|
493
|
|
|
|
|
|
|
# exit here as auto complete should stop processing |
494
|
4
|
50
|
|
|
|
39
|
return $EXIT ? exit 0 : undef; |
495
|
|
|
|
|
|
|
} |
496
|
|
|
|
|
|
|
|
497
|
|
|
|
|
|
|
sub list_options { |
498
|
4
|
|
|
4
|
1
|
192
|
my ($self) = @_; |
499
|
4
|
|
|
|
|
8
|
my @names; |
500
|
|
|
|
|
|
|
|
501
|
4
|
|
|
|
|
110
|
my $meta = $self->options->meta; |
502
|
|
|
|
|
|
|
|
503
|
4
|
|
|
|
|
151
|
for my $name ( $meta->get_attribute_list ) { |
504
|
21
|
|
|
|
|
100
|
my $opt = $meta->get_attribute($name); |
505
|
21
|
|
|
|
|
143
|
for my $name (@{ $opt->names }) { |
|
21
|
|
|
|
|
638
|
|
506
|
|
|
|
|
|
|
|
507
|
|
|
|
|
|
|
# skip auto-complete commands (they are hidden options) |
508
|
33
|
100
|
|
|
|
52
|
next if grep {$name eq $_} |
|
132
|
|
|
|
|
257
|
|
509
|
|
|
|
|
|
|
qw/auto_complete auto-complete auto_complete_list auto-complete-list/; |
510
|
21
|
|
|
|
|
53
|
push @names, $name |
511
|
|
|
|
|
|
|
} |
512
|
|
|
|
|
|
|
} |
513
|
|
|
|
|
|
|
|
514
|
|
|
|
|
|
|
return map { |
515
|
21
|
100
|
|
|
|
93
|
length $_ == 1 ? "-$_" : "--$_" |
516
|
|
|
|
|
|
|
} |
517
|
4
|
|
|
|
|
24
|
uniq sort { lc $a cmp lc $b } @names; |
|
30
|
|
|
|
|
73
|
|
518
|
|
|
|
|
|
|
} |
519
|
|
|
|
|
|
|
|
520
|
|
|
|
|
|
|
sub best_option { |
521
|
94
|
|
|
94
|
1
|
238
|
my ($self, $long, $short, $has_no) = @_; |
522
|
|
|
|
|
|
|
|
523
|
94
|
100
|
100
|
|
|
267
|
if ($has_no && $long) { |
524
|
3
|
|
|
|
|
12
|
$long =~ s/^no-//xms; |
525
|
|
|
|
|
|
|
} |
526
|
|
|
|
|
|
|
|
527
|
94
|
|
|
|
|
2552
|
my $meta = $self->options->meta; |
528
|
|
|
|
|
|
|
|
529
|
94
|
|
|
|
|
2062
|
for my $name ( $meta->get_attribute_list ) { |
530
|
826
|
|
|
|
|
2630
|
my $opt = $meta->get_attribute($name); |
531
|
|
|
|
|
|
|
|
532
|
826
|
100
|
100
|
|
|
14190
|
return ($opt, undef) if $long && $opt->name eq $long; |
533
|
|
|
|
|
|
|
|
534
|
796
|
|
|
|
|
1039
|
for my $name (@{ $opt->names }) { |
|
796
|
|
|
|
|
21785
|
|
535
|
1440
|
50
|
66
|
|
|
3130
|
return ($opt, undef) if defined $long && $name eq $long; |
536
|
1440
|
100
|
100
|
|
|
4157
|
return ($opt, undef) if defined $short && $name eq $short; |
537
|
|
|
|
|
|
|
} |
538
|
|
|
|
|
|
|
} |
539
|
|
|
|
|
|
|
|
540
|
14
|
100
|
100
|
|
|
149
|
if (($long && $long =~ /^\d+$/xms) || (defined $short && $short =~ /^\d$/xms)) { |
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
541
|
3
|
|
|
|
|
71
|
$meta = $self->opt->meta; |
542
|
3
|
|
|
|
|
64
|
for my $name ( $meta->get_attribute_list ) { |
543
|
6
|
|
|
|
|
40
|
my $opt = $meta->get_attribute($name); |
544
|
6
|
100
|
100
|
|
|
223
|
return ($opt, $long || $short) if $opt->number; |
545
|
|
|
|
|
|
|
} |
546
|
|
|
|
|
|
|
} |
547
|
|
|
|
|
|
|
|
548
|
11
|
100
|
|
|
|
63
|
return $self->best_option($long, $short, 1) if !$has_no; |
549
|
|
|
|
|
|
|
|
550
|
5
|
100
|
|
|
|
139
|
if ( $self->help_package ) { |
551
|
3
|
100
|
|
|
|
85
|
die [ Getopt::Alt::Exception->new( |
|
|
100
|
|
|
|
|
|
552
|
|
|
|
|
|
|
message => "Unknown option '" . ($long ? "--$long" : "-$short") . "'\n", |
553
|
|
|
|
|
|
|
option => ($long ? "--$long" : "-$short"), |
554
|
|
|
|
|
|
|
) ]; |
555
|
|
|
|
|
|
|
} |
556
|
|
|
|
|
|
|
else { |
557
|
2
|
100
|
|
|
|
57
|
die [ Getopt::Alt::Exception->new( |
|
|
100
|
|
|
|
|
|
558
|
|
|
|
|
|
|
help => 1, |
559
|
|
|
|
|
|
|
message => "Unknown option '" . ($long ? "--$long" : "-$short") . "'\n", |
560
|
|
|
|
|
|
|
option => ($long ? "--$long" : "-$short"), |
561
|
|
|
|
|
|
|
) ]; |
562
|
|
|
|
|
|
|
} |
563
|
|
|
|
|
|
|
} |
564
|
|
|
|
|
|
|
|
565
|
|
|
|
|
|
|
sub _show_help { |
566
|
6
|
|
|
6
|
|
26
|
my ($self, $verbosity, $msg) = @_; |
567
|
|
|
|
|
|
|
|
568
|
6
|
|
|
|
|
15
|
my %input; |
569
|
6
|
50
|
33
|
|
|
162
|
if ( $self->help_packages && $self->cmd ) { |
|
|
50
|
33
|
|
|
|
|
570
|
0
|
|
|
|
|
0
|
my $package = $self->help_packages->{$self->cmd}; |
571
|
0
|
0
|
|
|
|
0
|
if ($package) { |
572
|
0
|
|
|
|
|
0
|
$package =~ s{::}{/}gxms; |
573
|
0
|
|
|
|
|
0
|
$package .= '.pm'; |
574
|
0
|
|
|
|
|
0
|
require $package; |
575
|
0
|
|
|
|
|
0
|
%input = ( -input => $INC{$package} ); |
576
|
|
|
|
|
|
|
} |
577
|
|
|
|
|
|
|
} |
578
|
|
|
|
|
|
|
elsif ( $self->help_package && $self->help_package ne "1" ) { |
579
|
6
|
|
|
|
|
145
|
my $help = $self->help_package; |
580
|
6
|
50
|
|
|
|
108
|
if ( !-f $help ) { |
581
|
6
|
|
|
|
|
26
|
$help .= '.pm'; |
582
|
6
|
|
|
|
|
23
|
$help =~ s{::}{/}gxms; |
583
|
|
|
|
|
|
|
} |
584
|
6
|
|
|
|
|
31
|
%input = ( -input => $INC{$help} ); |
585
|
|
|
|
|
|
|
} |
586
|
|
|
|
|
|
|
|
587
|
6
|
|
|
|
|
1186
|
require Tie::Handle::Scalar; |
588
|
6
|
50
|
|
|
|
17136
|
my $out = $self->help_pre ? $self->help_pre() : ''; |
589
|
6
|
|
|
|
|
84
|
tie *FH, 'Tie::Handle::Scalar', \$out; |
590
|
6
|
|
|
|
|
2975
|
require Pod::Usage; |
591
|
6
|
50
|
|
|
|
99959
|
Pod::Usage::pod2usage( |
592
|
|
|
|
|
|
|
$msg ? ( -msg => $msg ) : (), |
593
|
|
|
|
|
|
|
-verbose => $verbosity, |
594
|
|
|
|
|
|
|
-exitval => 'NOEXIT', |
595
|
|
|
|
|
|
|
-output => \*FH, |
596
|
|
|
|
|
|
|
%input, |
597
|
|
|
|
|
|
|
); |
598
|
6
|
50
|
|
|
|
39005
|
$out .= $self->help_post ? $self->help_post->() : ''; |
599
|
6
|
|
|
|
|
183
|
die Getopt::Alt::Exception->new( message => $out, help => 1 ); |
600
|
|
|
|
|
|
|
} |
601
|
|
|
|
|
|
|
|
602
|
|
|
|
|
|
|
1; |
603
|
|
|
|
|
|
|
|
604
|
|
|
|
|
|
|
__END__ |
605
|
|
|
|
|
|
|
|
606
|
|
|
|
|
|
|
=head1 NAME |
607
|
|
|
|
|
|
|
|
608
|
|
|
|
|
|
|
Getopt::Alt - Command line option passing with with lots of features |
609
|
|
|
|
|
|
|
|
610
|
|
|
|
|
|
|
=head1 VERSION |
611
|
|
|
|
|
|
|
|
612
|
|
|
|
|
|
|
This documentation refers to Getopt::Alt version 0.5.4. |
613
|
|
|
|
|
|
|
|
614
|
|
|
|
|
|
|
=head1 SYNOPSIS |
615
|
|
|
|
|
|
|
|
616
|
|
|
|
|
|
|
use Getopt::Alt; |
617
|
|
|
|
|
|
|
|
618
|
|
|
|
|
|
|
# OO Style usage |
619
|
|
|
|
|
|
|
# Create a new options object |
620
|
|
|
|
|
|
|
my $opt = Getopt::Alt->new( |
621
|
|
|
|
|
|
|
{ |
622
|
|
|
|
|
|
|
default => { string => 'default' }, |
623
|
|
|
|
|
|
|
}, |
624
|
|
|
|
|
|
|
[ |
625
|
|
|
|
|
|
|
'string|s=s', |
626
|
|
|
|
|
|
|
... |
627
|
|
|
|
|
|
|
], |
628
|
|
|
|
|
|
|
); |
629
|
|
|
|
|
|
|
print "String = " . $opt->opt->{string} . "\n"; |
630
|
|
|
|
|
|
|
|
631
|
|
|
|
|
|
|
# Getopt::Long like usage |
632
|
|
|
|
|
|
|
use Getopt::Alt qw/get_options/; |
633
|
|
|
|
|
|
|
|
634
|
|
|
|
|
|
|
# most basic form |
635
|
|
|
|
|
|
|
my $options = get_options( |
636
|
|
|
|
|
|
|
'string|s=s', |
637
|
|
|
|
|
|
|
'int|i=i', |
638
|
|
|
|
|
|
|
'hash|h=s%', |
639
|
|
|
|
|
|
|
'array|a=s@', |
640
|
|
|
|
|
|
|
'increment|c+', |
641
|
|
|
|
|
|
|
'nullable|n=s?', |
642
|
|
|
|
|
|
|
'negatable|b!', |
643
|
|
|
|
|
|
|
'fixed_values|fixed-values|f=[a|bunch|of|fixed|values]', |
644
|
|
|
|
|
|
|
); |
645
|
|
|
|
|
|
|
print Dumper $options->opt; # passed parameters |
646
|
|
|
|
|
|
|
print join ',', @{ $options->files }; # non option parameters |
647
|
|
|
|
|
|
|
|
648
|
|
|
|
|
|
|
# with defaults |
649
|
|
|
|
|
|
|
my $options = get_options( |
650
|
|
|
|
|
|
|
{ negatable => 1 }, |
651
|
|
|
|
|
|
|
'string|s=s', |
652
|
|
|
|
|
|
|
'int|i=i', |
653
|
|
|
|
|
|
|
'hash|h=s%', |
654
|
|
|
|
|
|
|
'array|a=s@', |
655
|
|
|
|
|
|
|
'increment|c+', |
656
|
|
|
|
|
|
|
'nullable|n=s?', |
657
|
|
|
|
|
|
|
'negatable|b!', |
658
|
|
|
|
|
|
|
'fixed_values|fixed-values|f=[a|bunch|of|fixed|values]', |
659
|
|
|
|
|
|
|
); |
660
|
|
|
|
|
|
|
|
661
|
|
|
|
|
|
|
# with configuration |
662
|
|
|
|
|
|
|
my $options = get_options( |
663
|
|
|
|
|
|
|
{ |
664
|
|
|
|
|
|
|
helper => 1, # default when using get_options |
665
|
|
|
|
|
|
|
sub_command => 1, # stop processing at first non argument parameter |
666
|
|
|
|
|
|
|
}, |
667
|
|
|
|
|
|
|
[ |
668
|
|
|
|
|
|
|
'string|s=s', |
669
|
|
|
|
|
|
|
'int|i=i', |
670
|
|
|
|
|
|
|
'hash|h=s%', |
671
|
|
|
|
|
|
|
'array|a=s@', |
672
|
|
|
|
|
|
|
'increment|c+', |
673
|
|
|
|
|
|
|
'nullable|n=s?', |
674
|
|
|
|
|
|
|
'negatable|b!', |
675
|
|
|
|
|
|
|
'fixed_values|fixed-values|f=[a|bunch|of|fixed|values]', |
676
|
|
|
|
|
|
|
], |
677
|
|
|
|
|
|
|
); |
678
|
|
|
|
|
|
|
print $cmd; # sub command |
679
|
|
|
|
|
|
|
|
680
|
|
|
|
|
|
|
# with sub command details |
681
|
|
|
|
|
|
|
my $options = get_options( |
682
|
|
|
|
|
|
|
{ |
683
|
|
|
|
|
|
|
helper => 1, # default when using get_options |
684
|
|
|
|
|
|
|
sub_command => { |
685
|
|
|
|
|
|
|
sub => [ 'suboption' ], |
686
|
|
|
|
|
|
|
other => [ 'verbose|v' ], |
687
|
|
|
|
|
|
|
}, |
688
|
|
|
|
|
|
|
}, |
689
|
|
|
|
|
|
|
[ |
690
|
|
|
|
|
|
|
'string|s=s', |
691
|
|
|
|
|
|
|
'int|i=i', |
692
|
|
|
|
|
|
|
'hash|h=s%', |
693
|
|
|
|
|
|
|
'array|a=s@', |
694
|
|
|
|
|
|
|
'increment|c+', |
695
|
|
|
|
|
|
|
'nullable|n=s?', |
696
|
|
|
|
|
|
|
'negatable|b!', |
697
|
|
|
|
|
|
|
'fixed_values|fixed-values|f=[a|bunch|of|fixed|values]', |
698
|
|
|
|
|
|
|
], |
699
|
|
|
|
|
|
|
); |
700
|
|
|
|
|
|
|
print Dumper $option->opt; # command with sub command options merged in |
701
|
|
|
|
|
|
|
|
702
|
|
|
|
|
|
|
# auto_complete |
703
|
|
|
|
|
|
|
my $options = get_options( |
704
|
|
|
|
|
|
|
{ |
705
|
|
|
|
|
|
|
helper => 1, # default when using get_options |
706
|
|
|
|
|
|
|
auto_complete => sub { |
707
|
|
|
|
|
|
|
my ($opt, $auto) = @_; |
708
|
|
|
|
|
|
|
# ... code for auto completeion |
709
|
|
|
|
|
|
|
# called if --auto-complete specified on the command line |
710
|
|
|
|
|
|
|
}, |
711
|
|
|
|
|
|
|
}, |
712
|
|
|
|
|
|
|
[ |
713
|
|
|
|
|
|
|
'string|s=s', |
714
|
|
|
|
|
|
|
'int|i=i', |
715
|
|
|
|
|
|
|
], |
716
|
|
|
|
|
|
|
); |
717
|
|
|
|
|
|
|
|
718
|
|
|
|
|
|
|
=head1 DESCRIPTION |
719
|
|
|
|
|
|
|
|
720
|
|
|
|
|
|
|
The aim of C<Getopt::Alt> is to provide an alternative to L<Getopt::Long> that |
721
|
|
|
|
|
|
|
allows a simple command line program to easily grow in complexity. L<Getopt::Lon> |
722
|
|
|
|
|
|
|
can be expanded from a simple command line option passer to allow sub-commands. |
723
|
|
|
|
|
|
|
Option processing may stop at the sub-command or with the help of modules can |
724
|
|
|
|
|
|
|
cascade the processing into the sub command's module or config. |
725
|
|
|
|
|
|
|
|
726
|
|
|
|
|
|
|
The simple usage is quite similar to L<Getopt::Long>: |
727
|
|
|
|
|
|
|
|
728
|
|
|
|
|
|
|
In C<Getopt::Long> you might get your options like: |
729
|
|
|
|
|
|
|
|
730
|
|
|
|
|
|
|
use Getopt::Long; |
731
|
|
|
|
|
|
|
my %options = ( string => 'default' ); |
732
|
|
|
|
|
|
|
GetOptions( |
733
|
|
|
|
|
|
|
\%options, |
734
|
|
|
|
|
|
|
'string|s=s', |
735
|
|
|
|
|
|
|
... |
736
|
|
|
|
|
|
|
); |
737
|
|
|
|
|
|
|
|
738
|
|
|
|
|
|
|
The found options are now stored in the C<%options> hash. |
739
|
|
|
|
|
|
|
|
740
|
|
|
|
|
|
|
In C<Getopt::Alt> you might do the following: |
741
|
|
|
|
|
|
|
|
742
|
|
|
|
|
|
|
use Getopt::Alt qw/get_options/; |
743
|
|
|
|
|
|
|
my %default = ( string => 'default' ); |
744
|
|
|
|
|
|
|
my $opt = get_options( |
745
|
|
|
|
|
|
|
\%default, |
746
|
|
|
|
|
|
|
'string|s=s', |
747
|
|
|
|
|
|
|
... |
748
|
|
|
|
|
|
|
); |
749
|
|
|
|
|
|
|
my %options = %{ $opt->opt }; |
750
|
|
|
|
|
|
|
|
751
|
|
|
|
|
|
|
This will also result in the options stored in the C<%options> hash. |
752
|
|
|
|
|
|
|
|
753
|
|
|
|
|
|
|
Some other differences between Getopt::Alt and Getopt::Long include: |
754
|
|
|
|
|
|
|
|
755
|
|
|
|
|
|
|
=over 4 |
756
|
|
|
|
|
|
|
|
757
|
|
|
|
|
|
|
=item * |
758
|
|
|
|
|
|
|
|
759
|
|
|
|
|
|
|
Bundling - is on by default |
760
|
|
|
|
|
|
|
|
761
|
|
|
|
|
|
|
=item * |
762
|
|
|
|
|
|
|
|
763
|
|
|
|
|
|
|
Case sensitivity is on by default |
764
|
|
|
|
|
|
|
|
765
|
|
|
|
|
|
|
=item * |
766
|
|
|
|
|
|
|
|
767
|
|
|
|
|
|
|
Throws error rather than returning errors. |
768
|
|
|
|
|
|
|
|
769
|
|
|
|
|
|
|
=item * |
770
|
|
|
|
|
|
|
|
771
|
|
|
|
|
|
|
Can work with sub commands |
772
|
|
|
|
|
|
|
|
773
|
|
|
|
|
|
|
=back |
774
|
|
|
|
|
|
|
|
775
|
|
|
|
|
|
|
=head1 SUBROUTINES/METHODS |
776
|
|
|
|
|
|
|
|
777
|
|
|
|
|
|
|
=head2 Exported |
778
|
|
|
|
|
|
|
|
779
|
|
|
|
|
|
|
=head3 C<get_options (@options | $setup, $options)> |
780
|
|
|
|
|
|
|
|
781
|
|
|
|
|
|
|
=head3 C<get_options ($default, 'opt1', 'opt2' ... )> |
782
|
|
|
|
|
|
|
|
783
|
|
|
|
|
|
|
This is the equivalent of calling new(...)->process but it does some extra |
784
|
|
|
|
|
|
|
argument processing. |
785
|
|
|
|
|
|
|
|
786
|
|
|
|
|
|
|
B<Note>: The second form is the same basically the same as Getopt::Long's |
787
|
|
|
|
|
|
|
GetOptions called with a hash ref as the first parameter. |
788
|
|
|
|
|
|
|
|
789
|
|
|
|
|
|
|
=head2 Class Methods |
790
|
|
|
|
|
|
|
|
791
|
|
|
|
|
|
|
=head3 C<new ( \%config, \@optspec )> |
792
|
|
|
|
|
|
|
|
793
|
|
|
|
|
|
|
=head4 config |
794
|
|
|
|
|
|
|
|
795
|
|
|
|
|
|
|
=over 4 |
796
|
|
|
|
|
|
|
|
797
|
|
|
|
|
|
|
=item C<default> - HashRef |
798
|
|
|
|
|
|
|
|
799
|
|
|
|
|
|
|
Sets the default values for all the options. The values in opt will be reset |
800
|
|
|
|
|
|
|
with the values in here each time process is called |
801
|
|
|
|
|
|
|
|
802
|
|
|
|
|
|
|
=item C<files> - ArrayRef[Str] |
803
|
|
|
|
|
|
|
|
804
|
|
|
|
|
|
|
Any arguments that not consumed as part of options (usually files), if no |
805
|
|
|
|
|
|
|
arguments were passed to C<process> then this value would also be put back |
806
|
|
|
|
|
|
|
into C<@ARGV>. |
807
|
|
|
|
|
|
|
|
808
|
|
|
|
|
|
|
=item C<bundle> - bool (Default true) |
809
|
|
|
|
|
|
|
|
810
|
|
|
|
|
|
|
Turns on bundling of arguments eg C<-rv> is equivalent to C<-r -v>. This is |
811
|
|
|
|
|
|
|
on by default. |
812
|
|
|
|
|
|
|
|
813
|
|
|
|
|
|
|
=item C<ignore_case> - bool (Default true) |
814
|
|
|
|
|
|
|
|
815
|
|
|
|
|
|
|
Turns ignoring of the case of arguments, off by default. |
816
|
|
|
|
|
|
|
|
817
|
|
|
|
|
|
|
=item C<helper> - bool |
818
|
|
|
|
|
|
|
|
819
|
|
|
|
|
|
|
If set to a true value this will cause the help, man, and version options to |
820
|
|
|
|
|
|
|
be added the end of your options list. (i.e. you get --help --man and |
821
|
|
|
|
|
|
|
--version arguments for you program.) |
822
|
|
|
|
|
|
|
|
823
|
|
|
|
|
|
|
=item C<help_package> - Str |
824
|
|
|
|
|
|
|
|
825
|
|
|
|
|
|
|
The Perl package with the POD documentation for --help and --man, by default |
826
|
|
|
|
|
|
|
it's the callers package. |
827
|
|
|
|
|
|
|
|
828
|
|
|
|
|
|
|
=item C<name> - Str (Default $0's basename) |
829
|
|
|
|
|
|
|
|
830
|
|
|
|
|
|
|
Used when displaying --version info |
831
|
|
|
|
|
|
|
|
832
|
|
|
|
|
|
|
=item C<options> - Str (Default Getopt::Alt::Dynamic) |
833
|
|
|
|
|
|
|
|
834
|
|
|
|
|
|
|
The parent class for generating options. |
835
|
|
|
|
|
|
|
|
836
|
|
|
|
|
|
|
=item C<opt> - HashRef |
837
|
|
|
|
|
|
|
|
838
|
|
|
|
|
|
|
The values processed from the C<$ARGV> or arguments passed to the C<process> |
839
|
|
|
|
|
|
|
method.. |
840
|
|
|
|
|
|
|
|
841
|
|
|
|
|
|
|
=item C<default> - HashRef |
842
|
|
|
|
|
|
|
|
843
|
|
|
|
|
|
|
The default values for each option. The default value is not modified by |
844
|
|
|
|
|
|
|
processing, so if set the same default will be used from call to call. |
845
|
|
|
|
|
|
|
|
846
|
|
|
|
|
|
|
=item C<aliases> - HashRef[ArrayRef[Str]] |
847
|
|
|
|
|
|
|
|
848
|
|
|
|
|
|
|
When using sub-commands this allows you to configure aliases for those |
849
|
|
|
|
|
|
|
commands, aliases are recursed, they can have extra arguments though. |
850
|
|
|
|
|
|
|
If a configuration file is used aliases can be specified in that file. |
851
|
|
|
|
|
|
|
|
852
|
|
|
|
|
|
|
=item C<config> |
853
|
|
|
|
|
|
|
|
854
|
|
|
|
|
|
|
Stores the data in the configuration files |
855
|
|
|
|
|
|
|
|
856
|
|
|
|
|
|
|
=item C<conf_prefix> - Str (Default ".") |
857
|
|
|
|
|
|
|
|
858
|
|
|
|
|
|
|
The prefix for finding the configuration files. By default the following |
859
|
|
|
|
|
|
|
is used: |
860
|
|
|
|
|
|
|
|
861
|
|
|
|
|
|
|
=over 4 |
862
|
|
|
|
|
|
|
|
863
|
|
|
|
|
|
|
=item * |
864
|
|
|
|
|
|
|
|
865
|
|
|
|
|
|
|
./$conf_prefix$name |
866
|
|
|
|
|
|
|
|
867
|
|
|
|
|
|
|
=item * |
868
|
|
|
|
|
|
|
|
869
|
|
|
|
|
|
|
~/$conf_prefix$name |
870
|
|
|
|
|
|
|
|
871
|
|
|
|
|
|
|
=item * |
872
|
|
|
|
|
|
|
|
873
|
|
|
|
|
|
|
/etc/$conf_prefix$name |
874
|
|
|
|
|
|
|
|
875
|
|
|
|
|
|
|
=back |
876
|
|
|
|
|
|
|
|
877
|
|
|
|
|
|
|
=item C<conf_section> |
878
|
|
|
|
|
|
|
|
879
|
|
|
|
|
|
|
Used if the using program wants the ability to set up configuration groups |
880
|
|
|
|
|
|
|
so that the user can have a bunch of default values. This attribute sets the |
881
|
|
|
|
|
|
|
name in the configuration where configuration groups can be found. There |
882
|
|
|
|
|
|
|
should also be a matching argument so that the user can choose the appropriate |
883
|
|
|
|
|
|
|
configuration. |
884
|
|
|
|
|
|
|
|
885
|
|
|
|
|
|
|
=back |
886
|
|
|
|
|
|
|
|
887
|
|
|
|
|
|
|
=head2 Object Methods |
888
|
|
|
|
|
|
|
|
889
|
|
|
|
|
|
|
=head3 C<BUILD ()> |
890
|
|
|
|
|
|
|
|
891
|
|
|
|
|
|
|
internal method |
892
|
|
|
|
|
|
|
|
893
|
|
|
|
|
|
|
=head3 C<process ()> |
894
|
|
|
|
|
|
|
|
895
|
|
|
|
|
|
|
=head3 C<list_options ()> |
896
|
|
|
|
|
|
|
|
897
|
|
|
|
|
|
|
Returns a list of all command line options in the current object. |
898
|
|
|
|
|
|
|
|
899
|
|
|
|
|
|
|
=head3 C<best_option ()> |
900
|
|
|
|
|
|
|
|
901
|
|
|
|
|
|
|
Decides on the best matching option. |
902
|
|
|
|
|
|
|
|
903
|
|
|
|
|
|
|
=head3 C<complete ()> |
904
|
|
|
|
|
|
|
|
905
|
|
|
|
|
|
|
Command line auto complete helper |
906
|
|
|
|
|
|
|
|
907
|
|
|
|
|
|
|
=head2 Auto Complete |
908
|
|
|
|
|
|
|
|
909
|
|
|
|
|
|
|
For your program (say eg) you can add the following to your C<~/.bashrc> |
910
|
|
|
|
|
|
|
file to get auto-completion. |
911
|
|
|
|
|
|
|
|
912
|
|
|
|
|
|
|
_eg() { |
913
|
|
|
|
|
|
|
COMPREPLY=($(vtide --auto-complete ${COMP_CWORD} -- ${COMP_WORDS[@]})) |
914
|
|
|
|
|
|
|
} |
915
|
|
|
|
|
|
|
complete -F _eg eg |
916
|
|
|
|
|
|
|
|
917
|
|
|
|
|
|
|
B<Note>: This is different from version 0.5.4 and earlier |
918
|
|
|
|
|
|
|
|
919
|
|
|
|
|
|
|
=head1 DIAGNOSTICS |
920
|
|
|
|
|
|
|
|
921
|
|
|
|
|
|
|
=head1 CONFIGURATION AND ENVIRONMENT |
922
|
|
|
|
|
|
|
|
923
|
|
|
|
|
|
|
Configuration files can be used to specify default values and aliases. They |
924
|
|
|
|
|
|
|
can be located in the current directory, $HOME or /etc.The file name is |
925
|
|
|
|
|
|
|
specified by the C<name> attribute (which defaults to the program's name) |
926
|
|
|
|
|
|
|
and is prepended with a dot. eg: |
927
|
|
|
|
|
|
|
|
928
|
|
|
|
|
|
|
For a program called as C<$ ./foo> or C<$ foo> C<name> would be set to foo |
929
|
|
|
|
|
|
|
and possible configuration names would be |
930
|
|
|
|
|
|
|
|
931
|
|
|
|
|
|
|
=over 4 |
932
|
|
|
|
|
|
|
|
933
|
|
|
|
|
|
|
=item * |
934
|
|
|
|
|
|
|
|
935
|
|
|
|
|
|
|
.foo.yml |
936
|
|
|
|
|
|
|
|
937
|
|
|
|
|
|
|
=item * |
938
|
|
|
|
|
|
|
|
939
|
|
|
|
|
|
|
~/.foo.rc |
940
|
|
|
|
|
|
|
|
941
|
|
|
|
|
|
|
=item * |
942
|
|
|
|
|
|
|
|
943
|
|
|
|
|
|
|
/etc/.foo.yml |
944
|
|
|
|
|
|
|
|
945
|
|
|
|
|
|
|
=back |
946
|
|
|
|
|
|
|
|
947
|
|
|
|
|
|
|
See L<Config::Any> for information about config formats and file extensions. |
948
|
|
|
|
|
|
|
|
949
|
|
|
|
|
|
|
=head1 DEPENDENCIES |
950
|
|
|
|
|
|
|
|
951
|
|
|
|
|
|
|
=head1 INCOMPATIBILITIES |
952
|
|
|
|
|
|
|
|
953
|
|
|
|
|
|
|
=head1 BUGS AND LIMITATIONS |
954
|
|
|
|
|
|
|
|
955
|
|
|
|
|
|
|
There are no known bugs in this module. |
956
|
|
|
|
|
|
|
|
957
|
|
|
|
|
|
|
Please report problems to Ivan Wills (ivan.wills@gmail.com). |
958
|
|
|
|
|
|
|
|
959
|
|
|
|
|
|
|
Patches are welcome. |
960
|
|
|
|
|
|
|
|
961
|
|
|
|
|
|
|
=head1 AUTHOR |
962
|
|
|
|
|
|
|
|
963
|
|
|
|
|
|
|
Ivan Wills - (ivan.wills@gmail.com) |
964
|
|
|
|
|
|
|
|
965
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
966
|
|
|
|
|
|
|
|
967
|
|
|
|
|
|
|
Copyright (c) 2009 Ivan Wills (14 Mullion Close, Hornsby Heights, NSW Australia 2077). |
968
|
|
|
|
|
|
|
All rights reserved. |
969
|
|
|
|
|
|
|
|
970
|
|
|
|
|
|
|
This module is free software; you can redistribute it and/or modify it under |
971
|
|
|
|
|
|
|
the same terms as Perl itself. See L<perlartistic>. This program is |
972
|
|
|
|
|
|
|
distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
973
|
|
|
|
|
|
|
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
974
|
|
|
|
|
|
|
PARTICULAR PURPOSE. |
975
|
|
|
|
|
|
|
|
976
|
|
|
|
|
|
|
=cut |