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