1 package MooseX::Getopt;
2 # ABSTRACT: A Moose role for processing command line options
4 use MooseX::Role::Parameterized;
6 parameter getopt_conf => (
7 isa => 'ArrayRef[Str]',
8 default => sub { [ 'pass_through' ] },
14 my $getopt_conf = $p->getopt_conf;
16 with 'MooseX::Getopt::GLD' => { getopt_conf => $getopt_conf };
30 with 'MooseX::Getopt';
34 with 'MooseX::Getopt' => { getopt_conf => [ 'getopt_compat', 'bundling', ... ] };
36 has 'out' => (is => 'rw', isa => 'Str', required => 1);
37 has 'in' => (is => 'rw', isa => 'Str', required => 1);
39 # ... rest of the class here
46 my $app = My::App->new_with_options();
47 # ... rest of the script here
49 ## on the command line
50 % perl my_app_script.pl -in file.input -out file.dump
54 This is a role which provides an alternate constructor for creating
55 objects using parameters passed in from the command line.
57 This module attempts to DWIM as much as possible with the command line
58 params by introspecting your class's attributes. It will use the name
59 of your attribute as the command line option, and if there is a type
60 constraint defined, it will configure Getopt::Long to handle the option
63 You can use the trait L<MooseX::Getopt::Meta::Attribute::Trait> or the
64 attribute metaclass L<MooseX::Getopt::Meta::Attribute> to get non-default
65 commandline option names and aliases.
67 You can use the trait L<MooseX::Getopt::Meta::Attribute::Trait::NoGetopt>
68 or the attribute metaclass L<MooseX::Getopt::Meta::Attribute::NoGetopt>
69 to have C<MooseX::Getopt> ignore your attribute in the commandline options.
71 By default, attributes which start with an underscore are not given
72 commandline argument support, unless the attribute's metaclass is set
73 to L<MooseX::Getopt::Meta::Attribute>. If you don't want your accessors
74 to have the leading underscore in their name, you can do this:
76 # for read/write attributes
77 has '_foo' => (accessor => 'foo', ...);
79 # or for read-only attributes
80 has '_bar' => (reader => 'bar', ...);
82 This will mean that Getopt will not handle a --foo param, but your
83 code can still call the C<foo> method.
85 If your class also uses a configfile-loading role based on
86 L<MooseX::ConfigFromFile>, such as L<MooseX::SimpleConfig>,
87 L<MooseX::Getopt>'s C<new_with_options> will load the configfile
88 specified by the C<--configfile> option (or the default you've
89 given for the configfile attribute) for you.
91 Options specified in multiple places follow the following
92 precendence order: commandline overrides configfile, which
93 overrides explicit new_with_options parameters.
97 This role is a parameterized role. It accepts a HashRef of parameters. For now
98 there is only one configuration parameter, C<getopt_conf>. This parameter is an
99 ArrayRef of strings, which are L<Getopt::Long> configuraion options (see
100 "Configuring Getopt::Long" in L<Getopt::Long>). See L<SYNOPSIS> for an example.
102 =head2 Supported Type Constraints
108 A I<Bool> type constraint is set up as a boolean option with
109 Getopt::Long. So that this attribute description:
111 has 'verbose' => (is => 'rw', isa => 'Bool');
113 would translate into C<verbose!> as a Getopt::Long option descriptor,
114 which would enable the following command line options:
116 % my_script.pl --verbose
117 % my_script.pl --noverbose
119 =item I<Int>, I<Float>, I<Str>
121 These type constraints are set up as properly typed options with
122 Getopt::Long, using the C<=i>, C<=f> and C<=s> modifiers as appropriate.
126 An I<ArrayRef> type constraint is set up as a multiple value option
127 in Getopt::Long. So that this attribute description:
132 default => sub { [] }
135 would translate into C<includes=s@> as a Getopt::Long option descriptor,
136 which would enable the following command line options:
138 % my_script.pl --include /usr/lib --include /usr/local/lib
142 A I<HashRef> type constraint is set up as a hash value option
143 in Getopt::Long. So that this attribute description:
148 default => sub { {} }
151 would translate into C<define=s%> as a Getopt::Long option descriptor,
152 which would enable the following command line options:
154 % my_script.pl --define os=linux --define vendor=debian
158 =head2 Custom Type Constraints
160 It is possible to create custom type constraint to option spec
161 mappings if you need them. The process is fairly simple (but a
162 little verbose maybe). First you create a custom subtype, like
165 subtype 'ArrayOfInts'
167 => where { scalar (grep { looks_like_number($_) } @$_) };
169 Then you register the mapping, like so:
171 MooseX::Getopt::OptionTypeMap->add_option_type_to_map(
172 'ArrayOfInts' => '=i@'
175 Now any attribute declarations using this type constraint will
176 get the custom option spec. So that, this:
180 isa => 'ArrayOfInts',
181 default => sub { [0] }
184 Will translate to the following on the command line:
186 % my_script.pl --nums 5 --nums 88 --nums 199
188 This example is fairly trivial, but more complex validations are
189 easily possible with a little creativity. The trick is balancing
190 the type constraint validations with the Getopt::Long validations.
192 Better examples are certainly welcome :)
194 =head2 Inferred Type Constraints
196 If you define a custom subtype which is a subtype of one of the
197 standard L</Supported Type Constraints> above, and do not explicitly
198 provide custom support as in L</Custom Type Constraints> above,
199 MooseX::Getopt will treat it like the parent type for Getopt
202 For example, if you had the same custom C<ArrayOfInts> subtype
203 from the examples above, but did not add a new custom option
204 type for it to the C<OptionTypeMap>, it would be treated just
205 like a normal C<ArrayRef> type for Getopt purposes (that is,
208 =method B<new_with_options (%params)>
210 This method will take a set of default C<%params> and then collect
211 params from the command line (possibly overriding those in C<%params>)
212 and then return a newly constructed object.
214 The special parameter C<argv>, if specified should point to an array
215 reference with an array to use instead of C<@ARGV>.
217 If L<Getopt::Long/GetOptions> fails (due to invalid arguments),
218 C<new_with_options> will throw an exception.
220 If L<Getopt::Long::Descriptive> is installed and any of the following
221 command line params are passed, the program will exit with usage
222 information (and the option's state will be stored in the help_flag
223 attribute). You can add descriptions for each option by including a
224 B<documentation> option for each attribute to document.
230 If you have L<Getopt::Long::Descriptive> the C<usage> param is also passed to
231 C<new> as the usage option.
235 This accessor contains a reference to a copy of the C<@ARGV> array
236 as it originally existed at the time of C<new_with_options>.
238 =method B<extra_argv>
240 This accessor contains an arrayref of leftover C<@ARGV> elements that
241 L<Getopt::Long> did not parse. Note that the real C<@ARGV> is left
246 This accessor contains the L<Getopt::Long::Descriptive::Usage> object (if
247 L<Getopt::Long::Descriptive> is used).
251 This accessor contains the boolean state of the --help, --usage and --?
252 options (true if any of these options were passed on the command line).
256 This returns the role meta object.
258 =method B<process_argv (%params)>
260 This does most of the work of C<new_with_options>, analyzing the parameters
261 and argv, except for actually calling the constructor. It returns a
262 L<MooseX::Getopt::ProcessedArgv> object. C<new_with_options> uses this
263 method internally, so modifying this method via subclasses/roles will affect