line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MouseX::Getopt::Basic; |
2
|
|
|
|
|
|
|
# ABSTRACT: MouseX::Getopt::Basic - role to implement the Getopt::Long functionality |
3
|
|
|
|
|
|
|
|
4
|
22
|
|
|
22
|
|
24214
|
use Mouse::Role; |
|
22
|
|
|
|
|
19854
|
|
|
22
|
|
|
|
|
110
|
|
5
|
|
|
|
|
|
|
|
6
|
22
|
|
|
22
|
|
11502
|
use MouseX::Getopt::OptionTypeMap; |
|
22
|
|
|
|
|
33
|
|
|
22
|
|
|
|
|
450
|
|
7
|
22
|
|
|
22
|
|
6788
|
use MouseX::Getopt::Meta::Attribute; |
|
22
|
|
|
|
|
42
|
|
|
22
|
|
|
|
|
554
|
|
8
|
22
|
|
|
22
|
|
7638
|
use MouseX::Getopt::Meta::Attribute::NoGetopt; |
|
22
|
|
|
|
|
35
|
|
|
22
|
|
|
|
|
518
|
|
9
|
22
|
|
|
22
|
|
99
|
use Carp (); |
|
22
|
|
|
|
|
20
|
|
|
22
|
|
|
|
|
452
|
|
10
|
|
|
|
|
|
|
|
11
|
22
|
|
|
22
|
|
682
|
use Getopt::Long 2.37 (); |
|
22
|
|
|
|
|
10210
|
|
|
22
|
|
|
|
|
19877
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
has ARGV => (is => 'rw', isa => 'ArrayRef', metaclass => "NoGetopt"); |
14
|
|
|
|
|
|
|
has extra_argv => (is => 'rw', isa => 'ArrayRef', metaclass => "NoGetopt"); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub new_with_options { |
17
|
109
|
|
|
109
|
1
|
161836
|
my ($class, @params) = @_; |
18
|
|
|
|
|
|
|
|
19
|
109
|
|
|
|
|
138
|
my $config_from_file; |
20
|
109
|
100
|
|
|
|
293
|
if($class->meta->does_role('MouseX::ConfigFromFile')) { |
21
|
11
|
|
|
|
|
450
|
local @ARGV = @ARGV; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# just get the configfile arg now; the rest of the args will be |
24
|
|
|
|
|
|
|
# fetched later |
25
|
11
|
|
|
|
|
13
|
my $configfile; |
26
|
11
|
|
|
|
|
41
|
my $opt_parser = Getopt::Long::Parser->new( config => [ qw( no_auto_help pass_through ) ] ); |
27
|
11
|
|
|
|
|
760
|
$opt_parser->getoptions( "configfile=s" => \$configfile ); |
28
|
|
|
|
|
|
|
|
29
|
11
|
100
|
|
|
|
2128
|
if(!defined $configfile) { |
30
|
6
|
|
|
|
|
15
|
my $cfmeta = $class->meta->find_attribute_by_name('configfile'); |
31
|
6
|
100
|
|
|
|
371
|
$configfile = $cfmeta->default if $cfmeta->has_default; |
32
|
6
|
100
|
|
|
|
13
|
if (ref $configfile eq 'CODE') { |
33
|
|
|
|
|
|
|
# not sure theres a lot you can do with the class and may break some assumptions |
34
|
|
|
|
|
|
|
# warn? |
35
|
2
|
|
|
|
|
6
|
$configfile = &$configfile($class); |
36
|
|
|
|
|
|
|
} |
37
|
6
|
100
|
|
|
|
28
|
if (defined $configfile) { |
38
|
3
|
|
|
|
|
4
|
$config_from_file = eval { |
39
|
3
|
|
|
|
|
14
|
$class->get_config_from_file($configfile); |
40
|
|
|
|
|
|
|
}; |
41
|
3
|
50
|
|
|
|
7200
|
if ($@) { |
42
|
0
|
0
|
|
|
|
0
|
die $@ unless $@ =~ /Specified configfile '\Q$configfile\E' does not exist/; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
else { |
47
|
5
|
|
|
|
|
12
|
$config_from_file = $class->get_config_from_file($configfile); |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
109
|
100
|
|
|
|
4279
|
my $constructor_params = ( @params == 1 ? $params[0] : {@params} ); |
52
|
|
|
|
|
|
|
|
53
|
109
|
50
|
|
|
|
410
|
Carp::croak("Single parameters to new_with_options() must be a HASH ref") |
54
|
|
|
|
|
|
|
unless ref($constructor_params) eq 'HASH'; |
55
|
|
|
|
|
|
|
|
56
|
109
|
|
|
|
|
229
|
my %processed = $class->_parse_argv( |
57
|
|
|
|
|
|
|
options => [ |
58
|
|
|
|
|
|
|
$class->_attrs_to_options( $config_from_file ) |
59
|
|
|
|
|
|
|
], |
60
|
|
|
|
|
|
|
params => $constructor_params, |
61
|
|
|
|
|
|
|
); |
62
|
|
|
|
|
|
|
|
63
|
99
|
100
|
|
|
|
435
|
my $params = $config_from_file ? { %$config_from_file, %{$processed{params}} } : $processed{params}; |
|
7
|
|
|
|
|
19
|
|
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# did the user request usage information? |
66
|
99
|
100
|
100
|
|
|
1006
|
if ( $processed{usage} and $params->{help_flag} ) |
67
|
|
|
|
|
|
|
{ |
68
|
6
|
|
|
|
|
1200
|
$class->_getopt_full_usage($processed{usage}); |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
$class->new( |
72
|
93
|
100
|
|
|
|
41195
|
ARGV => $processed{argv_copy}, |
73
|
|
|
|
|
|
|
extra_argv => $processed{argv}, |
74
|
|
|
|
|
|
|
( $processed{usage} ? ( usage => $processed{usage} ) : () ), |
75
|
|
|
|
|
|
|
%$constructor_params, # explicit params to ->new |
76
|
|
|
|
|
|
|
%$params, # params from CLI |
77
|
|
|
|
|
|
|
); |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
23
|
|
|
23
|
|
57
|
sub _getopt_spec { shift->_traditional_spec(@_); } |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub _parse_argv { |
83
|
109
|
|
|
109
|
|
230
|
my ( $class, %params ) = @_; |
84
|
|
|
|
|
|
|
|
85
|
109
|
100
|
|
|
|
89
|
local @ARGV = @{ $params{params}{argv} || \@ARGV }; |
|
109
|
|
|
|
|
538
|
|
86
|
|
|
|
|
|
|
|
87
|
109
|
|
|
|
|
343
|
my ( $opt_spec, $name_to_init_arg ) = $class->_getopt_spec(%params); |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
# Get a clean copy of the original @ARGV |
90
|
109
|
|
|
|
|
178
|
my $argv_copy = [ @ARGV ]; |
91
|
|
|
|
|
|
|
|
92
|
109
|
|
|
|
|
117
|
my @warnings; |
93
|
109
|
|
|
|
|
127
|
my ( $parsed_options, $usage ) = eval { |
94
|
109
|
|
|
8
|
|
558
|
local $SIG{__WARN__} = sub { push @warnings, @_ }; |
|
8
|
|
|
|
|
4442
|
|
95
|
|
|
|
|
|
|
|
96
|
109
|
|
|
|
|
341
|
return $class->_getopt_get_options(\%params, $opt_spec); |
97
|
|
|
|
|
|
|
}; |
98
|
109
|
|
|
|
|
76728
|
my $e = $@; |
99
|
|
|
|
|
|
|
|
100
|
109
|
100
|
|
|
|
309
|
$class->_getopt_spec_warnings(@warnings) if @warnings; |
101
|
109
|
100
|
|
|
|
191
|
$class->_getopt_spec_exception(\@warnings, $e) if $e; |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
# Get a copy of the Getopt::Long-mangled @ARGV |
104
|
99
|
|
|
|
|
141
|
my $argv_mangled = [ @ARGV ]; |
105
|
|
|
|
|
|
|
|
106
|
107
|
|
|
|
|
309
|
my %constructor_args = ( |
107
|
|
|
|
|
|
|
map { |
108
|
99
|
|
|
|
|
489
|
$name_to_init_arg->{$_} => $parsed_options->{$_} |
109
|
|
|
|
|
|
|
} keys %$parsed_options, |
110
|
|
|
|
|
|
|
); |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
return ( |
113
|
99
|
100
|
|
|
|
734
|
params => \%constructor_args, |
114
|
|
|
|
|
|
|
argv_copy => $argv_copy, |
115
|
|
|
|
|
|
|
argv => $argv_mangled, |
116
|
|
|
|
|
|
|
( defined($usage) ? ( usage => $usage ) : () ), |
117
|
|
|
|
|
|
|
); |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub _getopt_get_options { |
121
|
23
|
|
|
23
|
|
28
|
my ($class, $params, $opt_spec) = @_; |
122
|
23
|
|
|
|
|
20
|
my %options; |
123
|
23
|
|
|
|
|
76
|
Getopt::Long::GetOptions(\%options, @$opt_spec); |
124
|
23
|
|
|
|
|
8944
|
return ( \%options, undef ); |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
8
|
|
|
8
|
|
17
|
sub _getopt_spec_warnings { } |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
sub _getopt_spec_exception { |
130
|
10
|
|
|
10
|
|
58
|
my ($self, $warnings, $exception) = @_; |
131
|
10
|
|
|
|
|
86
|
die @$warnings, $exception; |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub _getopt_full_usage { |
135
|
6
|
|
|
6
|
|
15
|
my ($self, $usage) = @_; |
136
|
6
|
|
|
|
|
17
|
$usage->die; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
sub _usage_format { |
140
|
86
|
|
|
86
|
|
271
|
return "usage: %c %o"; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
sub _traditional_spec { |
144
|
23
|
|
|
23
|
|
39
|
my ( $class, %params ) = @_; |
145
|
|
|
|
|
|
|
|
146
|
23
|
|
|
|
|
22
|
my ( @options, %name_to_init_arg, %options ); |
147
|
|
|
|
|
|
|
|
148
|
23
|
|
|
|
|
22
|
foreach my $opt ( @{ $params{options} } ) { |
|
23
|
|
|
|
|
43
|
|
149
|
119
|
|
|
|
|
201
|
push @options, $opt->{opt_string}; |
150
|
|
|
|
|
|
|
|
151
|
119
|
|
|
|
|
237
|
my $identifier = $opt->{name}; |
152
|
119
|
|
|
|
|
350
|
$identifier =~ s/\W/_/g; # Getopt::Long does this to all option names |
153
|
|
|
|
|
|
|
|
154
|
119
|
|
|
|
|
167
|
$name_to_init_arg{$identifier} = $opt->{init_arg}; |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
|
157
|
23
|
|
|
|
|
75
|
return ( \@options, \%name_to_init_arg ); |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
sub _compute_getopt_attrs { |
161
|
109
|
|
|
109
|
|
117
|
my $class = shift; |
162
|
915
|
100
|
|
|
|
3388
|
sort { $a->insertion_order <=> $b->insertion_order } |
|
610
|
|
|
|
|
9424
|
|
163
|
|
|
|
|
|
|
grep { |
164
|
919
|
|
|
|
|
17549
|
$_->does("MouseX::Getopt::Meta::Attribute::Trait") |
165
|
|
|
|
|
|
|
or |
166
|
|
|
|
|
|
|
$_->name !~ /^_/ |
167
|
|
|
|
|
|
|
} grep { |
168
|
109
|
|
|
|
|
225
|
!$_->does('MouseX::Getopt::Meta::Attribute::Trait::NoGetopt') |
169
|
|
|
|
|
|
|
} $class->meta->get_all_attributes |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
sub _get_cmd_flags_for_attr { |
173
|
556
|
|
|
556
|
|
455
|
my ( $class, $attr ) = @_; |
174
|
|
|
|
|
|
|
|
175
|
556
|
|
|
|
|
777
|
my $flag = $attr->name; |
176
|
|
|
|
|
|
|
|
177
|
556
|
|
|
|
|
359
|
my @aliases; |
178
|
|
|
|
|
|
|
|
179
|
556
|
100
|
|
|
|
1024
|
if ($attr->does('MouseX::Getopt::Meta::Attribute::Trait')) { |
180
|
319
|
100
|
|
|
|
8167
|
$flag = $attr->cmd_flag if $attr->has_cmd_flag; |
181
|
319
|
100
|
|
|
|
703
|
@aliases = @{ $attr->cmd_aliases } if $attr->has_cmd_aliases; |
|
223
|
|
|
|
|
528
|
|
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
|
184
|
556
|
|
|
|
|
2866
|
return ( $flag, @aliases ); |
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
sub _attrs_to_options { |
188
|
109
|
|
|
109
|
|
127
|
my $class = shift; |
189
|
109
|
|
100
|
|
|
465
|
my $config_from_file = shift || {}; |
190
|
|
|
|
|
|
|
|
191
|
109
|
|
|
|
|
113
|
my @options; |
192
|
|
|
|
|
|
|
|
193
|
109
|
|
|
|
|
218
|
foreach my $attr ($class->_compute_getopt_attrs) { |
194
|
556
|
|
|
|
|
1017
|
my ( $flag, @aliases ) = $class->_get_cmd_flags_for_attr($attr); |
195
|
|
|
|
|
|
|
|
196
|
556
|
|
|
|
|
735
|
my $opt_string = join(q{|}, $flag, @aliases); |
197
|
|
|
|
|
|
|
|
198
|
556
|
100
|
|
|
|
1530
|
if ($attr->name eq 'configfile') { |
|
|
50
|
|
|
|
|
|
199
|
11
|
|
|
|
|
11
|
$opt_string .= '=s'; |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
elsif ($attr->has_type_constraint) { |
202
|
545
|
|
|
|
|
647
|
my $type = $attr->type_constraint; |
203
|
545
|
50
|
|
|
|
1068
|
if (MouseX::Getopt::OptionTypeMap->has_option_type($type)) { |
204
|
545
|
|
|
|
|
901
|
$opt_string .= MouseX::Getopt::OptionTypeMap->get_option_type($type) |
205
|
|
|
|
|
|
|
} |
206
|
|
|
|
|
|
|
} |
207
|
|
|
|
|
|
|
|
208
|
556
|
100
|
66
|
|
|
3883
|
push @options, { |
209
|
|
|
|
|
|
|
name => $flag, |
210
|
|
|
|
|
|
|
init_arg => $attr->init_arg, |
211
|
|
|
|
|
|
|
opt_string => $opt_string, |
212
|
|
|
|
|
|
|
required => $attr->is_required && !$attr->has_default && !$attr->has_builder && !exists $config_from_file->{$attr->name}, |
213
|
|
|
|
|
|
|
# NOTE: |
214
|
|
|
|
|
|
|
# this "feature" was breaking because |
215
|
|
|
|
|
|
|
# Getopt::Long::Descriptive would return |
216
|
|
|
|
|
|
|
# the default value as if it was a command |
217
|
|
|
|
|
|
|
# line flag, which would then override the |
218
|
|
|
|
|
|
|
# one passed into a constructor. |
219
|
|
|
|
|
|
|
# See 100_gld_default_bug.t for an example |
220
|
|
|
|
|
|
|
# - SL |
221
|
|
|
|
|
|
|
#( ( $attr->has_default && ( $attr->is_default_a_coderef xor $attr->is_lazy ) ) ? ( default => $attr->default({}) ) : () ), |
222
|
|
|
|
|
|
|
( $attr->has_documentation ? ( doc => $attr->documentation ) : () ), |
223
|
|
|
|
|
|
|
} |
224
|
|
|
|
|
|
|
} |
225
|
|
|
|
|
|
|
|
226
|
109
|
|
|
|
|
617
|
return @options; |
227
|
|
|
|
|
|
|
} |
228
|
|
|
|
|
|
|
|
229
|
22
|
|
|
22
|
|
106
|
no Mouse::Role; |
|
22
|
|
|
|
|
29
|
|
|
22
|
|
|
|
|
78
|
|
230
|
|
|
|
|
|
|
1; |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=head1 SYNOPSIS |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
## In your class |
235
|
|
|
|
|
|
|
package My::App; |
236
|
|
|
|
|
|
|
use Mouse; |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
with 'MouseX::Getopt::Basic'; |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
has 'out' => (is => 'rw', isa => 'Str', required => 1); |
241
|
|
|
|
|
|
|
has 'in' => (is => 'rw', isa => 'Str', required => 1); |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
# ... rest of the class here |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
## in your script |
246
|
|
|
|
|
|
|
#!/usr/bin/perl |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
use My::App; |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
my $app = My::App->new_with_options(); |
251
|
|
|
|
|
|
|
# ... rest of the script here |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
## on the command line |
254
|
|
|
|
|
|
|
% perl my_app_script.pl --in file.input --out file.dump |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
=head1 DESCRIPTION |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
This is like L and can be used instead except that it |
259
|
|
|
|
|
|
|
doesn't make use of L (or "GLD" for short). |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
=over 4 |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
=item new_with_options |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
See L. |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
=back |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
=cut |