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