| 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
|
|
23165
|
use Mouse::Role; |
|
|
22
|
|
|
|
|
19988
|
|
|
|
22
|
|
|
|
|
118
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
22
|
|
|
22
|
|
12461
|
use MouseX::Getopt::OptionTypeMap; |
|
|
22
|
|
|
|
|
34
|
|
|
|
22
|
|
|
|
|
420
|
|
|
7
|
22
|
|
|
22
|
|
7390
|
use MouseX::Getopt::Meta::Attribute; |
|
|
22
|
|
|
|
|
40
|
|
|
|
22
|
|
|
|
|
523
|
|
|
8
|
22
|
|
|
22
|
|
7889
|
use MouseX::Getopt::Meta::Attribute::NoGetopt; |
|
|
22
|
|
|
|
|
30
|
|
|
|
22
|
|
|
|
|
479
|
|
|
9
|
22
|
|
|
22
|
|
89
|
use Carp (); |
|
|
22
|
|
|
|
|
22
|
|
|
|
22
|
|
|
|
|
501
|
|
|
10
|
|
|
|
|
|
|
|
|
11
|
22
|
|
|
22
|
|
689
|
use Getopt::Long 2.37 (); |
|
|
22
|
|
|
|
|
9436
|
|
|
|
22
|
|
|
|
|
21314
|
|
|
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
|
156902
|
my ($class, @params) = @_; |
|
18
|
|
|
|
|
|
|
|
|
19
|
109
|
|
|
|
|
126
|
my $config_from_file; |
|
20
|
109
|
100
|
|
|
|
279
|
if($class->meta->does_role('MouseX::ConfigFromFile')) { |
|
21
|
11
|
|
|
|
|
585
|
local @ARGV = @ARGV; |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# just get the configfile arg now; the rest of the args will be |
|
24
|
|
|
|
|
|
|
# fetched later |
|
25
|
11
|
|
|
|
|
11
|
my $configfile; |
|
26
|
11
|
|
|
|
|
73
|
my $opt_parser = Getopt::Long::Parser->new( config => [ qw( no_auto_help pass_through ) ] ); |
|
27
|
11
|
|
|
|
|
918
|
$opt_parser->getoptions( "configfile=s" => \$configfile ); |
|
28
|
|
|
|
|
|
|
|
|
29
|
11
|
100
|
|
|
|
2412
|
if(!defined $configfile) { |
|
30
|
6
|
|
|
|
|
23
|
my $cfmeta = $class->meta->find_attribute_by_name('configfile'); |
|
31
|
6
|
100
|
|
|
|
384
|
$configfile = $cfmeta->default if $cfmeta->has_default; |
|
32
|
6
|
100
|
|
|
|
16
|
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
|
|
|
|
|
8
|
$configfile = &$configfile($class); |
|
36
|
|
|
|
|
|
|
} |
|
37
|
6
|
100
|
|
|
|
32
|
if (defined $configfile) { |
|
38
|
3
|
|
|
|
|
6
|
$config_from_file = eval { |
|
39
|
3
|
|
|
|
|
17
|
$class->get_config_from_file($configfile); |
|
40
|
|
|
|
|
|
|
}; |
|
41
|
3
|
50
|
|
|
|
7556
|
if ($@) { |
|
42
|
0
|
0
|
|
|
|
0
|
die $@ unless $@ =~ /Specified configfile '\Q$configfile\E' does not exist/; |
|
43
|
|
|
|
|
|
|
} |
|
44
|
|
|
|
|
|
|
} |
|
45
|
|
|
|
|
|
|
} |
|
46
|
|
|
|
|
|
|
else { |
|
47
|
5
|
|
|
|
|
18
|
$config_from_file = $class->get_config_from_file($configfile); |
|
48
|
|
|
|
|
|
|
} |
|
49
|
|
|
|
|
|
|
} |
|
50
|
|
|
|
|
|
|
|
|
51
|
109
|
100
|
|
|
|
3901
|
my $constructor_params = ( @params == 1 ? $params[0] : {@params} ); |
|
52
|
|
|
|
|
|
|
|
|
53
|
109
|
50
|
|
|
|
256
|
Carp::croak("Single parameters to new_with_options() must be a HASH ref") |
|
54
|
|
|
|
|
|
|
unless ref($constructor_params) eq 'HASH'; |
|
55
|
|
|
|
|
|
|
|
|
56
|
109
|
|
|
|
|
224
|
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
|
|
|
|
414
|
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
|
66
|
|
|
721
|
if ( $processed{usage} and $params->{help_flag} ) |
|
67
|
|
|
|
|
|
|
{ |
|
68
|
6
|
|
|
|
|
1092
|
$class->_getopt_full_usage($processed{usage}); |
|
69
|
|
|
|
|
|
|
} |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
$class->new( |
|
72
|
|
|
|
|
|
|
ARGV => $processed{argv_copy}, |
|
73
|
|
|
|
|
|
|
extra_argv => $processed{argv}, |
|
74
|
93
|
100
|
|
|
|
41207
|
( $processed{usage} ? ( usage => $processed{usage} ) : () ), |
|
75
|
|
|
|
|
|
|
%$constructor_params, # explicit params to ->new |
|
76
|
|
|
|
|
|
|
%$params, # params from CLI |
|
77
|
|
|
|
|
|
|
); |
|
78
|
|
|
|
|
|
|
} |
|
79
|
|
|
|
|
|
|
|
|
80
|
23
|
|
|
23
|
|
42
|
sub _getopt_spec { shift->_traditional_spec(@_); } |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub _parse_argv { |
|
83
|
109
|
|
|
109
|
|
232
|
my ( $class, %params ) = @_; |
|
84
|
|
|
|
|
|
|
|
|
85
|
109
|
100
|
|
|
|
93
|
local @ARGV = @{ $params{params}{argv} || \@ARGV }; |
|
|
109
|
|
|
|
|
505
|
|
|
86
|
|
|
|
|
|
|
|
|
87
|
109
|
|
|
|
|
346
|
my ( $opt_spec, $name_to_init_arg ) = $class->_getopt_spec(%params); |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
# Get a clean copy of the original @ARGV |
|
90
|
109
|
|
|
|
|
171
|
my $argv_copy = [ @ARGV ]; |
|
91
|
|
|
|
|
|
|
|
|
92
|
109
|
|
|
|
|
95
|
my @warnings; |
|
93
|
109
|
|
|
|
|
125
|
my ( $parsed_options, $usage ) = eval { |
|
94
|
109
|
|
|
8
|
|
528
|
local $SIG{__WARN__} = sub { push @warnings, @_ }; |
|
|
8
|
|
|
|
|
4428
|
|
|
95
|
|
|
|
|
|
|
|
|
96
|
109
|
|
|
|
|
280
|
return $class->_getopt_get_options(\%params, $opt_spec); |
|
97
|
|
|
|
|
|
|
}; |
|
98
|
109
|
|
|
|
|
74421
|
my $e = $@; |
|
99
|
|
|
|
|
|
|
|
|
100
|
109
|
100
|
|
|
|
289
|
$class->_getopt_spec_warnings(@warnings) if @warnings; |
|
101
|
109
|
100
|
|
|
|
194
|
$class->_getopt_spec_exception(\@warnings, $e) if $e; |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
# Get a copy of the Getopt::Long-mangled @ARGV |
|
104
|
99
|
|
|
|
|
151
|
my $argv_mangled = [ @ARGV ]; |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
my %constructor_args = ( |
|
107
|
|
|
|
|
|
|
map { |
|
108
|
99
|
|
|
|
|
362
|
$name_to_init_arg->{$_} => $parsed_options->{$_} |
|
|
107
|
|
|
|
|
282
|
|
|
109
|
|
|
|
|
|
|
} keys %$parsed_options, |
|
110
|
|
|
|
|
|
|
); |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
return ( |
|
113
|
99
|
100
|
|
|
|
681
|
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
|
|
22
|
my ($class, $params, $opt_spec) = @_; |
|
122
|
23
|
|
|
|
|
19
|
my %options; |
|
123
|
23
|
|
|
|
|
57
|
Getopt::Long::GetOptions(\%options, @$opt_spec); |
|
124
|
23
|
|
|
|
|
7331
|
return ( \%options, undef ); |
|
125
|
|
|
|
|
|
|
} |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
8
|
|
|
sub _getopt_spec_warnings { } |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
sub _getopt_spec_exception { |
|
130
|
10
|
|
|
10
|
|
43
|
my ($self, $warnings, $exception) = @_; |
|
131
|
10
|
|
|
|
|
94
|
die @$warnings, $exception; |
|
132
|
|
|
|
|
|
|
} |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub _getopt_full_usage { |
|
135
|
6
|
|
|
6
|
|
13
|
my ($self, $usage) = @_; |
|
136
|
6
|
|
|
|
|
15
|
$usage->die; |
|
137
|
|
|
|
|
|
|
} |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
sub _usage_format { |
|
140
|
86
|
|
|
86
|
|
273
|
return "usage: %c %o"; |
|
141
|
|
|
|
|
|
|
} |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
sub _traditional_spec { |
|
144
|
23
|
|
|
23
|
|
38
|
my ( $class, %params ) = @_; |
|
145
|
|
|
|
|
|
|
|
|
146
|
23
|
|
|
|
|
24
|
my ( @options, %name_to_init_arg, %options ); |
|
147
|
|
|
|
|
|
|
|
|
148
|
23
|
|
|
|
|
22
|
foreach my $opt ( @{ $params{options} } ) { |
|
|
23
|
|
|
|
|
33
|
|
|
149
|
119
|
|
|
|
|
183
|
push @options, $opt->{opt_string}; |
|
150
|
|
|
|
|
|
|
|
|
151
|
119
|
|
|
|
|
88
|
my $identifier = $opt->{name}; |
|
152
|
119
|
|
|
|
|
110
|
$identifier =~ s/\W/_/g; # Getopt::Long does this to all option names |
|
153
|
|
|
|
|
|
|
|
|
154
|
119
|
|
|
|
|
153
|
$name_to_init_arg{$identifier} = $opt->{init_arg}; |
|
155
|
|
|
|
|
|
|
} |
|
156
|
|
|
|
|
|
|
|
|
157
|
23
|
|
|
|
|
51
|
return ( \@options, \%name_to_init_arg ); |
|
158
|
|
|
|
|
|
|
} |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
sub _compute_getopt_attrs { |
|
161
|
109
|
|
|
109
|
|
96
|
my $class = shift; |
|
162
|
915
|
|
|
|
|
3167
|
sort { $a->insertion_order <=> $b->insertion_order } |
|
163
|
|
|
|
|
|
|
grep { |
|
164
|
610
|
100
|
|
|
|
8914
|
$_->does("MouseX::Getopt::Meta::Attribute::Trait") |
|
165
|
|
|
|
|
|
|
or |
|
166
|
|
|
|
|
|
|
$_->name !~ /^_/ |
|
167
|
|
|
|
|
|
|
} grep { |
|
168
|
109
|
|
|
|
|
224
|
!$_->does('MouseX::Getopt::Meta::Attribute::Trait::NoGetopt') |
|
|
919
|
|
|
|
|
15309
|
|
|
169
|
|
|
|
|
|
|
} $class->meta->get_all_attributes |
|
170
|
|
|
|
|
|
|
} |
|
171
|
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
sub _get_cmd_flags_for_attr { |
|
173
|
556
|
|
|
556
|
|
469
|
my ( $class, $attr ) = @_; |
|
174
|
|
|
|
|
|
|
|
|
175
|
556
|
|
|
|
|
689
|
my $flag = $attr->name; |
|
176
|
|
|
|
|
|
|
|
|
177
|
556
|
|
|
|
|
341
|
my @aliases; |
|
178
|
|
|
|
|
|
|
|
|
179
|
556
|
100
|
|
|
|
904
|
if ($attr->does('MouseX::Getopt::Meta::Attribute::Trait')) { |
|
180
|
319
|
100
|
|
|
|
6615
|
$flag = $attr->cmd_flag if $attr->has_cmd_flag; |
|
181
|
319
|
100
|
|
|
|
620
|
@aliases = @{ $attr->cmd_aliases } if $attr->has_cmd_aliases; |
|
|
223
|
|
|
|
|
481
|
|
|
182
|
|
|
|
|
|
|
} |
|
183
|
|
|
|
|
|
|
|
|
184
|
556
|
|
|
|
|
2585
|
return ( $flag, @aliases ); |
|
185
|
|
|
|
|
|
|
} |
|
186
|
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
sub _attrs_to_options { |
|
188
|
109
|
|
|
109
|
|
109
|
my $class = shift; |
|
189
|
109
|
|
100
|
|
|
414
|
my $config_from_file = shift || {}; |
|
190
|
|
|
|
|
|
|
|
|
191
|
109
|
|
|
|
|
91
|
my @options; |
|
192
|
|
|
|
|
|
|
|
|
193
|
109
|
|
|
|
|
204
|
foreach my $attr ($class->_compute_getopt_attrs) { |
|
194
|
556
|
|
|
|
|
980
|
my ( $flag, @aliases ) = $class->_get_cmd_flags_for_attr($attr); |
|
195
|
|
|
|
|
|
|
|
|
196
|
556
|
|
|
|
|
777
|
my $opt_string = join(q{|}, $flag, @aliases); |
|
197
|
|
|
|
|
|
|
|
|
198
|
556
|
100
|
|
|
|
1484
|
if ($attr->name eq 'configfile') { |
|
|
|
50
|
|
|
|
|
|
|
199
|
11
|
|
|
|
|
15
|
$opt_string .= '=s'; |
|
200
|
|
|
|
|
|
|
} |
|
201
|
|
|
|
|
|
|
elsif ($attr->has_type_constraint) { |
|
202
|
545
|
|
|
|
|
588
|
my $type = $attr->type_constraint; |
|
203
|
545
|
50
|
|
|
|
1000
|
if (MouseX::Getopt::OptionTypeMap->has_option_type($type)) { |
|
204
|
545
|
|
|
|
|
805
|
$opt_string .= MouseX::Getopt::OptionTypeMap->get_option_type($type) |
|
205
|
|
|
|
|
|
|
} |
|
206
|
|
|
|
|
|
|
} |
|
207
|
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
push @options, { |
|
209
|
|
|
|
|
|
|
name => $flag, |
|
210
|
|
|
|
|
|
|
init_arg => $attr->init_arg, |
|
211
|
|
|
|
|
|
|
opt_string => $opt_string, |
|
212
|
556
|
100
|
66
|
|
|
3680
|
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
|
|
|
|
|
392
|
return @options; |
|
227
|
|
|
|
|
|
|
} |
|
228
|
|
|
|
|
|
|
|
|
229
|
22
|
|
|
22
|
|
110
|
no Mouse::Role; |
|
|
22
|
|
|
|
|
23
|
|
|
|
22
|
|
|
|
|
84
|
|
|
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 |