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
|
|
480145
|
use Moose; |
|
7
|
|
|
|
|
3195407
|
|
|
7
|
|
|
|
|
45
|
|
10
|
7
|
|
|
7
|
|
51103
|
use warnings; |
|
7
|
|
|
|
|
21
|
|
|
7
|
|
|
|
|
221
|
|
11
|
7
|
|
|
7
|
|
4495
|
use version; |
|
7
|
|
|
|
|
13593
|
|
|
7
|
|
|
|
|
39
|
|
12
|
7
|
|
|
7
|
|
630
|
use Carp; |
|
7
|
|
|
|
|
16
|
|
|
7
|
|
|
|
|
502
|
|
13
|
7
|
|
|
7
|
|
3459
|
use English qw/ -no_match_vars /; |
|
7
|
|
|
|
|
25483
|
|
|
7
|
|
|
|
|
41
|
|
14
|
7
|
|
|
7
|
|
2493
|
use List::Util qw/uniq/; |
|
7
|
|
|
|
|
15
|
|
|
7
|
|
|
|
|
441
|
|
15
|
7
|
|
|
7
|
|
3331
|
use Getopt::Alt::Option qw/build_option/; |
|
7
|
|
|
|
|
57
|
|
|
7
|
|
|
|
|
41
|
|
16
|
7
|
|
|
7
|
|
4240
|
use Getopt::Alt::Exception; |
|
7
|
|
|
|
|
14
|
|
|
7
|
|
|
|
|
159
|
|
17
|
7
|
|
|
7
|
|
34
|
use Try::Tiny; |
|
7
|
|
|
|
|
14
|
|
|
7
|
|
|
|
|
414
|
|
18
|
7
|
|
|
7
|
|
5984
|
use Path::Tiny; |
|
7
|
|
|
|
|
77601
|
|
|
7
|
|
|
|
|
376
|
|
19
|
7
|
|
|
7
|
|
3673
|
use Config::Any; |
|
7
|
|
|
|
|
59322
|
|
|
7
|
|
|
|
|
243
|
|
20
|
7
|
|
|
7
|
|
3541
|
use File::HomeDir; |
|
7
|
|
|
|
|
37817
|
|
|
7
|
|
|
|
|
650
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
use overload ( |
23
|
1
|
|
|
1
|
|
42
|
'@{}' => sub { $_[0]->files }, |
24
|
6
|
|
|
6
|
|
1712
|
'bool' => sub { 1 }, |
25
|
7
|
|
|
7
|
|
57
|
); |
|
7
|
|
|
|
|
16
|
|
|
7
|
|
|
|
|
71
|
|
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
Moose::Exporter->setup_import_methods( |
28
|
|
|
|
|
|
|
as_is => [qw/get_options/], |
29
|
|
|
|
|
|
|
); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
our $VERSION = version->new('0.5.2'); |
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
|
|
132
|
my ($base_class, @params) = @_; |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
# construct a class of options passing |
183
|
34
|
|
|
|
|
152
|
my $class_name = 'Getopt::Alt::Dynamic::A' . $count++; |
184
|
34
|
|
|
|
|
356
|
my $option_class = Moose::Meta::Class->create( |
185
|
|
|
|
|
|
|
$class_name, |
186
|
|
|
|
|
|
|
superclasses => [ $base_class ], |
187
|
|
|
|
|
|
|
); |
188
|
|
|
|
|
|
|
|
189
|
34
|
|
|
|
|
87469
|
while ( my $option = shift @params ) { |
190
|
203
|
|
|
|
|
2844
|
build_option($option_class, $option); |
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
|
193
|
33
|
|
|
|
|
628
|
return $class_name; |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
sub BUILD { |
197
|
34
|
|
|
34
|
1
|
58471
|
my ($self) = @_; |
198
|
|
|
|
|
|
|
|
199
|
34
|
|
|
|
|
1042
|
my $basename = $self->name; |
200
|
34
|
|
|
|
|
898
|
my $prefix = $self->conf_prefix; |
201
|
34
|
|
|
|
|
440
|
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
|
|
|
|
|
12
|
map { %$_ } |
211
|
2
|
|
|
|
|
7
|
map { values %$_ } |
212
|
34
|
|
|
|
|
612542
|
reverse @{ $conf } |
|
34
|
|
|
|
|
160
|
|
213
|
|
|
|
|
|
|
}; |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
# perlcritic is confused here combining hashes is not the same as comma separated arguments |
216
|
34
|
|
|
|
|
99
|
$self->default({ %{$self->default}, %$conf, }); ## no critic |
|
34
|
|
|
|
|
1495
|
|
217
|
34
|
|
|
|
|
959
|
$self->config($conf); |
218
|
|
|
|
|
|
|
|
219
|
34
|
100
|
|
|
|
151
|
if ($conf->{aliases}) { |
220
|
1
|
|
|
|
|
2
|
for my $alias (keys %{ $conf->{aliases} }) { |
|
1
|
|
|
|
|
4
|
|
221
|
1
|
|
|
|
|
24
|
$self->aliases->{$alias} = [ split /\s+/xms, $conf->{aliases}{$alias} ]; |
222
|
|
|
|
|
|
|
} |
223
|
|
|
|
|
|
|
} |
224
|
|
|
|
|
|
|
|
225
|
34
|
|
|
|
|
187
|
return; |
226
|
|
|
|
|
|
|
} |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
sub get_options { |
229
|
15
|
|
|
15
|
1
|
12769
|
my @args = @_; |
230
|
15
|
|
|
|
|
47
|
my $caller = caller; |
231
|
|
|
|
|
|
|
|
232
|
15
|
100
|
100
|
|
|
123
|
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
|
|
|
|
|
41
|
my $self; |
238
|
|
|
|
|
|
|
try { |
239
|
15
|
|
|
15
|
|
752
|
$self = __PACKAGE__->new(@args); |
240
|
|
|
|
|
|
|
|
241
|
14
|
50
|
33
|
|
|
511
|
$self->help_package($caller) if !$self->help_package || $self->help_package eq __PACKAGE__; |
242
|
|
|
|
|
|
|
|
243
|
14
|
|
|
|
|
67
|
$self->process(); |
244
|
|
|
|
|
|
|
} |
245
|
|
|
|
|
|
|
catch { |
246
|
7
|
100
|
66
|
7
|
|
91865
|
if ( ref $_ && ref $_ eq 'Getopt::Alt::Exception' && $_->help ) { |
|
|
|
100
|
|
|
|
|
247
|
3
|
|
|
|
|
26
|
die $_; |
248
|
|
|
|
|
|
|
} |
249
|
|
|
|
|
|
|
|
250
|
4
|
|
|
|
|
52
|
warn $_; |
251
|
4
|
|
|
|
|
2610
|
$self = __PACKAGE__->new(); |
252
|
|
|
|
|
|
|
|
253
|
4
|
50
|
33
|
|
|
947
|
$self->help_package($caller) if !$self->help_package || $self->help_package eq __PACKAGE__; |
254
|
|
|
|
|
|
|
|
255
|
4
|
|
|
|
|
22
|
$self->_show_help(1); |
256
|
15
|
|
|
|
|
139
|
}; |
257
|
|
|
|
|
|
|
|
258
|
8
|
50
|
|
|
|
222
|
return if !defined $self; |
259
|
|
|
|
|
|
|
|
260
|
8
|
50
|
|
|
|
218
|
return wantarray ? ( $self->opt, $self->cmd, $self ) : $self->opt; |
261
|
|
|
|
|
|
|
} |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
sub process { |
264
|
87
|
|
|
87
|
1
|
3358
|
my ($self, @args) = @_; |
265
|
87
|
|
|
|
|
161
|
my $passed_args = scalar @args; |
266
|
87
|
100
|
|
|
|
285
|
@args = $passed_args ? @args : @ARGV; |
267
|
87
|
|
|
|
|
2973
|
$self->clear_opt; |
268
|
87
|
|
|
|
|
8984
|
$self->clear_cmd; |
269
|
87
|
|
|
|
|
2110
|
$self->files([]); |
270
|
|
|
|
|
|
|
|
271
|
87
|
|
|
|
|
214
|
my @args_orig = @args; |
272
|
87
|
|
|
|
|
2166
|
my $class = $self->options; |
273
|
87
|
|
|
|
|
169
|
$self->opt( $class->new( %{ $self->default } ) ); |
|
87
|
|
|
|
|
1916
|
|
274
|
87
|
|
|
|
|
198
|
my @errors; |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
ARG: |
277
|
87
|
|
|
|
|
271
|
while (my $arg = shift @args) { |
278
|
94
|
|
|
|
|
146
|
my $action = ''; |
279
|
|
|
|
|
|
|
try { |
280
|
94
|
|
|
94
|
|
4307
|
my ($long, $short, $arg_data); |
281
|
94
|
100
|
|
|
|
607
|
if ( $arg =~ /^-- (\w[^=\s]+) (?:= (.*) )?/xms ) { |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
282
|
33
|
|
|
|
|
97
|
$long = $1; |
283
|
33
|
|
|
|
|
65
|
$arg_data = $2; |
284
|
|
|
|
|
|
|
} |
285
|
|
|
|
|
|
|
elsif ( $arg =~ /^- (\w) =? (.*)/xms ) { |
286
|
55
|
|
|
|
|
145
|
$short = $1; |
287
|
55
|
|
|
|
|
111
|
$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
|
|
|
|
|
13
|
push @{ $self->files }, $arg; |
|
6
|
|
|
|
|
166
|
|
307
|
6
|
100
|
|
|
|
181
|
die $self->sub_command ? "last\n" : "next\n"; |
308
|
|
|
|
|
|
|
} |
309
|
|
|
|
|
|
|
|
310
|
88
|
|
|
|
|
260
|
my ($opt, $new_value) = $self->best_option( $long, $short ); |
311
|
83
|
100
|
|
|
|
237
|
if (defined $new_value) { |
312
|
3
|
|
|
|
|
85
|
$long = $opt->name; |
313
|
3
|
|
|
|
|
16
|
$short = undef; |
314
|
3
|
|
|
|
|
14
|
($arg_data) = $arg =~ /^--?(\d+)$/; |
315
|
|
|
|
|
|
|
} |
316
|
83
|
|
|
|
|
1946
|
$opt->value( $self->opt->{ $opt->name } ); |
317
|
|
|
|
|
|
|
|
318
|
83
|
|
|
|
|
377
|
my ($value, $used) = $opt->process( $long, $short, $arg_data, \@args ); |
319
|
78
|
|
|
|
|
2109
|
my $opt_name = $opt->name; |
320
|
78
|
50
|
33
|
|
|
1730
|
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
|
|
|
|
|
2009
|
$self->opt->{$opt->name} = $value; |
325
|
|
|
|
|
|
|
|
326
|
78
|
100
|
100
|
|
|
300
|
if ( !$used && $short && defined $arg_data && length $arg_data ) { |
|
|
|
66
|
|
|
|
|
|
|
|
100
|
|
|
|
|
327
|
2
|
|
|
|
|
7
|
unshift @args, '-' . $arg_data; |
328
|
|
|
|
|
|
|
} |
329
|
78
|
0
|
33
|
|
|
2261
|
if ($self->has_conf_section |
|
|
|
33
|
|
|
|
|
|
|
|
0
|
|
|
|
|
330
|
|
|
|
|
|
|
&& $self->conf_section |
331
|
|
|
|
|
|
|
&& $self->conf_section == $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
|
|
43130
|
if ( $_ eq "next\n" ) { |
|
|
100
|
|
|
|
|
|
347
|
2
|
|
|
|
|
8
|
$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
|
|
|
|
145
|
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
|
|
|
|
|
17
|
$action = 'last'; |
358
|
|
|
|
|
|
|
} |
359
|
|
|
|
|
|
|
else { |
360
|
10
|
100
|
66
|
|
|
66
|
$_ = $_->[0] if ref $_ eq 'ARRAY' && @$_ == 1; |
361
|
|
|
|
|
|
|
|
362
|
10
|
50
|
33
|
|
|
345
|
if ( $self->has_auto_complete && $self->opt->auto_complete ) { |
363
|
0
|
|
|
|
|
0
|
push @errors, $_; |
364
|
|
|
|
|
|
|
} |
365
|
|
|
|
|
|
|
else { |
366
|
10
|
|
|
|
|
73
|
die $_; |
367
|
|
|
|
|
|
|
} |
368
|
|
|
|
|
|
|
} |
369
|
94
|
|
|
|
|
951
|
}; |
370
|
84
|
100
|
|
|
|
2536
|
next if $action eq 'next'; |
371
|
82
|
100
|
|
|
|
319
|
last if $action eq 'last'; |
372
|
|
|
|
|
|
|
} |
373
|
|
|
|
|
|
|
|
374
|
77
|
100
|
|
|
|
2376
|
if ( $self->has_sub_command ) { |
375
|
4
|
50
|
33
|
|
|
12
|
shift @{ $self->files } if @{ $self->files } && $self->files->[0] eq '--'; |
|
0
|
|
|
|
|
0
|
|
|
4
|
|
|
|
|
88
|
|
376
|
|
|
|
|
|
|
|
377
|
4
|
50
|
33
|
|
|
10
|
if ( ! @{ $self->files } && @args ) { |
|
4
|
|
|
|
|
87
|
|
378
|
0
|
|
|
|
|
0
|
$self->files([ @args ]); |
379
|
|
|
|
|
|
|
} |
380
|
|
|
|
|
|
|
|
381
|
4
|
50
|
33
|
|
|
95
|
$self->cmd( shift @{ $self->files } ) if ! $self->cmd && @{ $self->files }; |
|
4
|
|
|
|
|
90
|
|
|
4
|
|
|
|
|
85
|
|
382
|
|
|
|
|
|
|
} |
383
|
77
|
100
|
66
|
|
|
526
|
if ( !$passed_args && $self->files ) { |
384
|
13
|
|
|
|
|
41
|
@ARGV = ( @{ $self->files }, @args ); ## no critic |
|
13
|
|
|
|
|
288
|
|
385
|
|
|
|
|
|
|
} |
386
|
|
|
|
|
|
|
|
387
|
77
|
100
|
|
|
|
2114
|
if ( $self->has_sub_command ) { |
388
|
4
|
100
|
33
|
|
|
104
|
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
|
|
|
|
73
|
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
|
|
|
|
80
|
if ( ref $self->sub_command->{$self->cmd} eq 'ARRAY' ) { |
404
|
|
|
|
|
|
|
# make a copy of the sub command |
405
|
3
|
|
|
|
|
7
|
my $sub = [ @{$self->sub_command->{$self->cmd}} ]; |
|
3
|
|
|
|
|
71
|
|
406
|
|
|
|
|
|
|
# check the style |
407
|
3
|
100
|
66
|
|
|
33
|
my $options = @$sub == 2 |
408
|
|
|
|
|
|
|
&& ref $sub->[0] eq 'HASH' |
409
|
|
|
|
|
|
|
&& ref $sub->[1] eq 'ARRAY' ? shift @$sub : {}; |
410
|
3
|
100
|
|
|
|
15
|
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
|
|
|
|
|
97
|
%{ $options }, ## no critic |
417
|
|
|
|
|
|
|
options => $self->options, # inherit this objects options |
418
|
3
|
100
|
|
|
|
84
|
default => { %{ $self->opt }, %{ $options->{default} || {} } }, |
|
3
|
|
|
|
|
70
|
|
|
3
|
|
|
|
|
44
|
|
419
|
|
|
|
|
|
|
}, |
420
|
|
|
|
|
|
|
$opt_args |
421
|
|
|
|
|
|
|
); |
422
|
3
|
|
|
|
|
62
|
local @ARGV = (); |
423
|
3
|
50
|
|
|
|
86
|
if ( $self->opt->auto_complete ) { |
424
|
0
|
|
|
|
|
0
|
push @args, '--auto-complete', $self->opt->auto_complete, '--'; |
425
|
|
|
|
|
|
|
} |
426
|
3
|
|
|
|
|
55
|
$sub_obj->process(@args); |
427
|
3
|
|
|
|
|
68
|
$self->opt( $sub_obj->opt ); |
428
|
3
|
|
|
|
|
72
|
$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
|
|
|
|
2096
|
if ( $self->help_package ) { |
443
|
11
|
100
|
33
|
|
|
313
|
if ( $self->opt->{version} ) { |
|
|
100
|
66
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
444
|
1
|
|
|
|
|
9
|
my ($name) = $PROGRAM_NAME =~ m{^.*/(.*?)$}mxs; |
445
|
1
|
50
|
|
|
|
6
|
my $version = defined $main::VERSION ? $main::VERSION : 'undef'; |
446
|
1
|
|
|
|
|
39
|
die Getopt::Alt::Exception->new( |
447
|
|
|
|
|
|
|
message => "$name Version = $version\n", |
448
|
|
|
|
|
|
|
help => 1, |
449
|
|
|
|
|
|
|
); |
450
|
|
|
|
|
|
|
} |
451
|
|
|
|
|
|
|
elsif ( $self->opt->{man} ) { |
452
|
1
|
|
|
|
|
5
|
$self->_show_help(2); |
453
|
|
|
|
|
|
|
} |
454
|
|
|
|
|
|
|
elsif ( $self->opt->{help} ) { |
455
|
1
|
|
|
|
|
5
|
$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
|
|
|
|
|
243
|
return $self; |
466
|
|
|
|
|
|
|
} |
467
|
|
|
|
|
|
|
|
468
|
|
|
|
|
|
|
sub complete { |
469
|
4
|
|
|
4
|
1
|
9028
|
my ($self, $errors) = @_; |
470
|
|
|
|
|
|
|
|
471
|
4
|
100
|
66
|
|
|
141
|
if ( $self->sub_command && !$self->cmd ) { |
|
|
50
|
33
|
|
|
|
|
472
|
2
|
|
|
|
|
5
|
my $cmd = shift @ARGV; |
473
|
2
|
100
|
|
|
|
6
|
my @sub_command = grep { $cmd ? /$cmd/ : 1 } sort keys %{ $self->sub_command }; |
|
4
|
|
|
|
|
29
|
|
|
2
|
|
|
|
|
46
|
|
474
|
2
|
|
|
|
|
95
|
print join ' ', @sub_command; |
475
|
|
|
|
|
|
|
} |
476
|
|
|
|
|
|
|
elsif ( $ARGV[-1] && $ARGV[-1] =~ /^-/xms ) { |
477
|
2
|
|
|
|
|
4
|
my $cmd = $ARGV[-1]; |
478
|
2
|
50
|
|
|
|
11
|
print join ' ', grep { $cmd ? /^$cmd/ : 1 } sort $self->list_options; |
|
14
|
|
|
|
|
154
|
|
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
|
|
|
|
38
|
return $EXIT ? exit 0 : undef; |
487
|
|
|
|
|
|
|
} |
488
|
|
|
|
|
|
|
|
489
|
|
|
|
|
|
|
sub list_options { |
490
|
4
|
|
|
4
|
1
|
145
|
my ($self) = @_; |
491
|
4
|
|
|
|
|
8
|
my @names; |
492
|
|
|
|
|
|
|
|
493
|
4
|
|
|
|
|
98
|
my $meta = $self->options->meta; |
494
|
|
|
|
|
|
|
|
495
|
4
|
|
|
|
|
147
|
for my $name ( $meta->get_attribute_list ) { |
496
|
21
|
|
|
|
|
86
|
my $opt = $meta->get_attribute($name); |
497
|
21
|
|
|
|
|
134
|
for my $name (@{ $opt->names }) { |
|
21
|
|
|
|
|
607
|
|
498
|
|
|
|
|
|
|
|
499
|
|
|
|
|
|
|
# skip auto-complete commands (they are hidden options) |
500
|
33
|
100
|
|
|
|
48
|
next if grep {$name eq $_} |
|
132
|
|
|
|
|
220
|
|
501
|
|
|
|
|
|
|
qw/auto_complete auto-complete auto_complete_list auto-complete-list/; |
502
|
21
|
|
|
|
|
44
|
push @names, $name |
503
|
|
|
|
|
|
|
} |
504
|
|
|
|
|
|
|
} |
505
|
|
|
|
|
|
|
|
506
|
|
|
|
|
|
|
return map { |
507
|
21
|
100
|
|
|
|
86
|
length $_ == 1 ? "-$_" : "--$_" |
508
|
|
|
|
|
|
|
} |
509
|
4
|
|
|
|
|
24
|
uniq sort { lc $a cmp lc $b } @names; |
|
35
|
|
|
|
|
75
|
|
510
|
|
|
|
|
|
|
} |
511
|
|
|
|
|
|
|
|
512
|
|
|
|
|
|
|
sub best_option { |
513
|
94
|
|
|
94
|
1
|
229
|
my ($self, $long, $short, $has_no) = @_; |
514
|
|
|
|
|
|
|
|
515
|
94
|
100
|
100
|
|
|
291
|
if ($has_no && $long) { |
516
|
3
|
|
|
|
|
15
|
$long =~ s/^no-//xms; |
517
|
|
|
|
|
|
|
} |
518
|
|
|
|
|
|
|
|
519
|
94
|
|
|
|
|
2495
|
my $meta = $self->options->meta; |
520
|
|
|
|
|
|
|
|
521
|
94
|
|
|
|
|
2091
|
for my $name ( $meta->get_attribute_list ) { |
522
|
770
|
|
|
|
|
2511
|
my $opt = $meta->get_attribute($name); |
523
|
|
|
|
|
|
|
|
524
|
770
|
100
|
100
|
|
|
13763
|
return ($opt, undef) if $long && $opt->name eq $long; |
525
|
|
|
|
|
|
|
|
526
|
740
|
|
|
|
|
1040
|
for my $name (@{ $opt->names }) { |
|
740
|
|
|
|
|
20073
|
|
527
|
1251
|
50
|
66
|
|
|
2796
|
return ($opt, undef) if defined $long && $name eq $long; |
528
|
1251
|
100
|
100
|
|
|
3602
|
return ($opt, undef) if defined $short && $name eq $short; |
529
|
|
|
|
|
|
|
} |
530
|
|
|
|
|
|
|
} |
531
|
|
|
|
|
|
|
|
532
|
14
|
100
|
100
|
|
|
270
|
if (($long && $long =~ /^\d+$/xms) || (defined $short && $short =~ /^\d$/xms)) { |
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
533
|
3
|
|
|
|
|
69
|
$meta = $self->opt->meta; |
534
|
3
|
|
|
|
|
68
|
for my $name ( $meta->get_attribute_list ) { |
535
|
45
|
|
|
|
|
123
|
my $opt = $meta->get_attribute($name); |
536
|
45
|
100
|
100
|
|
|
1410
|
return ($opt, $long || $short) if $opt->number; |
537
|
|
|
|
|
|
|
} |
538
|
|
|
|
|
|
|
} |
539
|
|
|
|
|
|
|
|
540
|
11
|
100
|
|
|
|
69
|
return $self->best_option($long, $short, 1) if !$has_no; |
541
|
|
|
|
|
|
|
|
542
|
5
|
100
|
|
|
|
142
|
if ( $self->help_package ) { |
543
|
3
|
100
|
|
|
|
103
|
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
|
|
|
|
73
|
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
|
|
25
|
my ($self, $verbosity, $msg) = @_; |
559
|
|
|
|
|
|
|
|
560
|
6
|
|
|
|
|
15
|
my %input; |
561
|
6
|
50
|
33
|
|
|
167
|
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
|
|
|
|
|
149
|
my $help = $self->help_package; |
572
|
6
|
50
|
|
|
|
110
|
if ( !-f $help ) { |
573
|
6
|
|
|
|
|
27
|
$help .= '.pm'; |
574
|
6
|
|
|
|
|
23
|
$help =~ s{::}{/}gxms; |
575
|
|
|
|
|
|
|
} |
576
|
6
|
|
|
|
|
33
|
%input = ( -input => $INC{$help} ); |
577
|
|
|
|
|
|
|
} |
578
|
|
|
|
|
|
|
|
579
|
6
|
|
|
|
|
1118
|
require Tie::Handle::Scalar; |
580
|
6
|
|
|
|
|
17088
|
my $out = ''; |
581
|
6
|
|
|
|
|
85
|
tie *FH, 'Tie::Handle::Scalar', \$out; |
582
|
6
|
|
|
|
|
2969
|
require Pod::Usage; |
583
|
6
|
50
|
|
|
|
101203
|
Pod::Usage::pod2usage( |
584
|
|
|
|
|
|
|
$msg ? ( -msg => $msg ) : (), |
585
|
|
|
|
|
|
|
-verbose => $verbosity, |
586
|
|
|
|
|
|
|
-exitval => 'NOEXIT', |
587
|
|
|
|
|
|
|
-output => \*FH, |
588
|
|
|
|
|
|
|
%input, |
589
|
|
|
|
|
|
|
); |
590
|
6
|
|
|
|
|
39362
|
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.2. |
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.2 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 |