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
|
20
|
|
|
20
|
|
42737
|
use Mouse::Role; |
|
20
|
|
|
|
|
42567
|
|
|
20
|
|
|
|
|
147
|
|
5
|
|
|
|
|
|
|
|
6
|
20
|
|
|
20
|
|
29234
|
use MouseX::Getopt::OptionTypeMap; |
|
20
|
|
|
|
|
47
|
|
|
20
|
|
|
|
|
849
|
|
7
|
20
|
|
|
20
|
|
17862
|
use MouseX::Getopt::Meta::Attribute; |
|
20
|
|
|
|
|
66
|
|
|
20
|
|
|
|
|
813
|
|
8
|
20
|
|
|
20
|
|
13962
|
use MouseX::Getopt::Meta::Attribute::NoGetopt; |
|
20
|
|
|
|
|
52
|
|
|
20
|
|
|
|
|
590
|
|
9
|
20
|
|
|
20
|
|
125
|
use Carp (); |
|
20
|
|
|
|
|
36
|
|
|
20
|
|
|
|
|
739
|
|
10
|
|
|
|
|
|
|
|
11
|
20
|
|
|
20
|
|
1712
|
use Getopt::Long 2.37 (); |
|
20
|
|
|
|
|
18531
|
|
|
20
|
|
|
|
|
32707
|
|
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
|
98
|
|
|
98
|
1
|
265383
|
my ($class, @params) = @_; |
18
|
|
|
|
|
|
|
|
19
|
98
|
|
|
|
|
367
|
my $config_from_file; |
20
|
98
|
50
|
|
|
|
1001
|
if($class->meta->does_role('MouseX::ConfigFromFile')) { |
21
|
0
|
|
|
|
|
0
|
local @ARGV = @ARGV; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# just get the configfile arg now; the rest of the args will be |
24
|
|
|
|
|
|
|
# fetched later |
25
|
0
|
|
|
|
|
0
|
my $configfile; |
26
|
0
|
|
|
|
|
0
|
my $opt_parser = Getopt::Long::Parser->new( config => [ qw( no_auto_help pass_through ) ] ); |
27
|
0
|
|
|
|
|
0
|
$opt_parser->getoptions( "configfile=s" => \$configfile ); |
28
|
|
|
|
|
|
|
|
29
|
0
|
0
|
|
|
|
0
|
if(!defined $configfile) { |
30
|
0
|
|
|
|
|
0
|
my $cfmeta = $class->meta->find_attribute_by_name('configfile'); |
31
|
0
|
0
|
|
|
|
0
|
$configfile = $cfmeta->default if $cfmeta->has_default; |
32
|
0
|
0
|
|
|
|
0
|
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
|
0
|
|
|
|
|
0
|
$configfile = &$configfile($class); |
36
|
|
|
|
|
|
|
} |
37
|
0
|
0
|
|
|
|
0
|
if (defined $configfile) { |
38
|
0
|
|
|
|
|
0
|
$config_from_file = eval { |
39
|
0
|
|
|
|
|
0
|
$class->get_config_from_file($configfile); |
40
|
|
|
|
|
|
|
}; |
41
|
0
|
0
|
|
|
|
0
|
if ($@) { |
42
|
0
|
0
|
|
|
|
0
|
die $@ unless $@ =~ /Specified configfile '\Q$configfile\E' does not exist/; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
else { |
47
|
0
|
|
|
|
|
0
|
$config_from_file = $class->get_config_from_file($configfile); |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
98
|
100
|
|
|
|
9831
|
my $constructor_params = ( @params == 1 ? $params[0] : {@params} ); |
52
|
|
|
|
|
|
|
|
53
|
98
|
50
|
|
|
|
595
|
Carp::croak("Single parameters to new_with_options() must be a HASH ref") |
54
|
|
|
|
|
|
|
unless ref($constructor_params) eq 'HASH'; |
55
|
|
|
|
|
|
|
|
56
|
98
|
|
|
|
|
1107
|
my %processed = $class->_parse_argv( |
57
|
|
|
|
|
|
|
options => [ |
58
|
|
|
|
|
|
|
$class->_attrs_to_options( $config_from_file ) |
59
|
|
|
|
|
|
|
], |
60
|
|
|
|
|
|
|
params => $constructor_params, |
61
|
|
|
|
|
|
|
); |
62
|
|
|
|
|
|
|
|
63
|
90
|
50
|
|
|
|
993
|
my $params = $config_from_file ? { %$config_from_file, %{$processed{params}} } : $processed{params}; |
|
0
|
|
|
|
|
0
|
|
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# did the user request usage information? |
66
|
90
|
100
|
100
|
|
|
1673
|
if ( $processed{usage} and $params->{help_flag} ) |
67
|
|
|
|
|
|
|
{ |
68
|
5
|
|
|
|
|
808
|
$class->_getopt_full_usage($processed{usage}); |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
$class->new( |
72
|
85
|
100
|
|
|
|
27832
|
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
|
|
95
|
sub _getopt_spec { shift->_traditional_spec(@_); } |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub _parse_argv { |
83
|
98
|
|
|
98
|
|
367
|
my ( $class, %params ) = @_; |
84
|
|
|
|
|
|
|
|
85
|
98
|
100
|
|
|
|
149
|
local @ARGV = @{ $params{params}{argv} || \@ARGV }; |
|
98
|
|
|
|
|
741
|
|
86
|
|
|
|
|
|
|
|
87
|
98
|
|
|
|
|
1671
|
my ( $opt_spec, $name_to_init_arg ) = $class->_getopt_spec(%params); |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
# Get a clean copy of the original @ARGV |
90
|
98
|
|
|
|
|
298
|
my $argv_copy = [ @ARGV ]; |
91
|
|
|
|
|
|
|
|
92
|
98
|
|
|
|
|
157
|
my @warnings; |
93
|
98
|
|
|
|
|
186
|
my ( $parsed_options, $usage ) = eval { |
94
|
98
|
|
|
8
|
|
828
|
local $SIG{__WARN__} = sub { push @warnings, @_ }; |
|
8
|
|
|
|
|
6578
|
|
95
|
|
|
|
|
|
|
|
96
|
98
|
|
|
|
|
458
|
return $class->_getopt_get_options(\%params, $opt_spec); |
97
|
|
|
|
|
|
|
}; |
98
|
98
|
|
|
|
|
116907
|
my $e = $@; |
99
|
|
|
|
|
|
|
|
100
|
98
|
100
|
|
|
|
397
|
$class->_getopt_spec_warnings(@warnings) if @warnings; |
101
|
98
|
100
|
|
|
|
406
|
$class->_getopt_spec_exception(\@warnings, $e) if $e; |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
# Get a copy of the Getopt::Long-mangled @ARGV |
104
|
90
|
|
|
|
|
212
|
my $argv_mangled = [ @ARGV ]; |
105
|
|
|
|
|
|
|
|
106
|
93
|
|
|
|
|
437
|
my %constructor_args = ( |
107
|
|
|
|
|
|
|
map { |
108
|
90
|
|
|
|
|
1542
|
$name_to_init_arg->{$_} => $parsed_options->{$_} |
109
|
|
|
|
|
|
|
} keys %$parsed_options, |
110
|
|
|
|
|
|
|
); |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
return ( |
113
|
90
|
100
|
|
|
|
1114
|
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
|
|
41
|
my ($class, $params, $opt_spec) = @_; |
122
|
23
|
|
|
|
|
31
|
my %options; |
123
|
23
|
|
|
|
|
120
|
Getopt::Long::GetOptions(\%options, @$opt_spec); |
124
|
23
|
|
|
|
|
15334
|
return ( \%options, undef ); |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
8
|
|
|
8
|
|
62
|
sub _getopt_spec_warnings { } |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
sub _getopt_spec_exception { |
130
|
8
|
|
|
8
|
|
31
|
my ($self, $warnings, $exception) = @_; |
131
|
8
|
|
|
|
|
138
|
die @$warnings, $exception; |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub _getopt_full_usage { |
135
|
5
|
|
|
5
|
|
54
|
my ($self, $usage) = @_; |
136
|
5
|
|
|
|
|
19
|
$usage->die; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
sub _usage_format { |
140
|
75
|
|
|
75
|
|
396
|
return "usage: %c %o"; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
sub _traditional_spec { |
144
|
23
|
|
|
23
|
|
66
|
my ( $class, %params ) = @_; |
145
|
|
|
|
|
|
|
|
146
|
23
|
|
|
|
|
37
|
my ( @options, %name_to_init_arg, %options ); |
147
|
|
|
|
|
|
|
|
148
|
23
|
|
|
|
|
32
|
foreach my $opt ( @{ $params{options} } ) { |
|
23
|
|
|
|
|
65
|
|
149
|
119
|
|
|
|
|
210
|
push @options, $opt->{opt_string}; |
150
|
|
|
|
|
|
|
|
151
|
119
|
|
|
|
|
287
|
my $identifier = $opt->{name}; |
152
|
119
|
|
|
|
|
188
|
$identifier =~ s/\W/_/g; # Getopt::Long does this to all option names |
153
|
|
|
|
|
|
|
|
154
|
119
|
|
|
|
|
548
|
$name_to_init_arg{$identifier} = $opt->{init_arg}; |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
|
157
|
23
|
|
|
|
|
100
|
return ( \@options, \%name_to_init_arg ); |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
sub _compute_getopt_attrs { |
161
|
98
|
|
|
98
|
|
325
|
my $class = shift; |
162
|
828
|
100
|
|
|
|
6638
|
sort { $a->insertion_order <=> $b->insertion_order } |
|
552
|
|
|
|
|
17923
|
|
163
|
|
|
|
|
|
|
grep { |
164
|
828
|
|
|
|
|
46905
|
$_->does("MouseX::Getopt::Meta::Attribute::Trait") |
165
|
|
|
|
|
|
|
or |
166
|
|
|
|
|
|
|
$_->name !~ /^_/ |
167
|
|
|
|
|
|
|
} grep { |
168
|
98
|
|
|
|
|
528
|
!$_->does('MouseX::Getopt::Meta::Attribute::Trait::NoGetopt') |
169
|
|
|
|
|
|
|
} $class->meta->get_all_attributes |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
sub _get_cmd_flags_for_attr { |
173
|
498
|
|
|
498
|
|
738
|
my ( $class, $attr ) = @_; |
174
|
|
|
|
|
|
|
|
175
|
498
|
|
|
|
|
1375
|
my $flag = $attr->name; |
176
|
|
|
|
|
|
|
|
177
|
498
|
|
|
|
|
546
|
my @aliases; |
178
|
|
|
|
|
|
|
|
179
|
498
|
100
|
|
|
|
1429
|
if ($attr->does('MouseX::Getopt::Meta::Attribute::Trait')) { |
180
|
308
|
100
|
|
|
|
13045
|
$flag = $attr->cmd_flag if $attr->has_cmd_flag; |
181
|
308
|
100
|
|
|
|
1144
|
@aliases = @{ $attr->cmd_aliases } if $attr->has_cmd_aliases; |
|
212
|
|
|
|
|
946
|
|
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
|
184
|
498
|
|
|
|
|
4363
|
return ( $flag, @aliases ); |
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
sub _attrs_to_options { |
188
|
98
|
|
|
98
|
|
192
|
my $class = shift; |
189
|
98
|
|
50
|
|
|
970
|
my $config_from_file = shift || {}; |
190
|
|
|
|
|
|
|
|
191
|
98
|
|
|
|
|
170
|
my @options; |
192
|
|
|
|
|
|
|
|
193
|
98
|
|
|
|
|
383
|
foreach my $attr ($class->_compute_getopt_attrs) { |
194
|
498
|
|
|
|
|
1669
|
my ( $flag, @aliases ) = $class->_get_cmd_flags_for_attr($attr); |
195
|
|
|
|
|
|
|
|
196
|
498
|
|
|
|
|
1064
|
my $opt_string = join(q{|}, $flag, @aliases); |
197
|
|
|
|
|
|
|
|
198
|
498
|
50
|
|
|
|
2507
|
if ($attr->name eq 'configfile') { |
|
|
50
|
|
|
|
|
|
199
|
0
|
|
|
|
|
0
|
$opt_string .= '=s'; |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
elsif ($attr->has_type_constraint) { |
202
|
498
|
|
|
|
|
1084
|
my $type = $attr->type_constraint; |
203
|
498
|
50
|
|
|
|
1706
|
if (MouseX::Getopt::OptionTypeMap->has_option_type($type)) { |
204
|
498
|
|
|
|
|
1510
|
$opt_string .= MouseX::Getopt::OptionTypeMap->get_option_type($type) |
205
|
|
|
|
|
|
|
} |
206
|
|
|
|
|
|
|
} |
207
|
|
|
|
|
|
|
|
208
|
498
|
100
|
33
|
|
|
6084
|
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
|
98
|
|
|
|
|
703
|
return @options; |
227
|
|
|
|
|
|
|
} |
228
|
|
|
|
|
|
|
|
229
|
20
|
|
|
20
|
|
138
|
no Mouse::Role; |
|
20
|
|
|
|
|
45
|
|
|
20
|
|
|
|
|
116
|
|
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 |