1 package MooseX::Getopt;
2 # ABSTRACT: A Moose role for processing command line options
6 with 'MooseX::Getopt::GLD';
18 with 'MooseX::Getopt';
20 has 'out' => (is => 'rw', isa => 'Str', required => 1);
21 has 'in' => (is => 'rw', isa => 'Str', required => 1);
23 # ... rest of the class here
30 my $app = My::App->new_with_options();
31 # ... rest of the script here
33 ## on the command line
34 % perl my_app_script.pl -in file.input -out file.dump
38 This is a role which provides an alternate constructor for creating
39 objects using parameters passed in from the command line.
41 This module attempts to DWIM as much as possible with the command line
42 params by introspecting your class's attributes. It will use the name
43 of your attribute as the command line option, and if there is a type
44 constraint defined, it will configure Getopt::Long to handle the option
47 You can use the trait L<MooseX::Getopt::Meta::Attribute::Trait> or the
48 attribute metaclass L<MooseX::Getopt::Meta::Attribute> to get non-default
49 commandline option names and aliases.
51 You can use the trait L<MooseX::Getopt::Meta::Attribute::Trait::NoGetopt>
52 or the attribute metaclass L<MooseX::Getopt::Meta::Attribute::NoGetopt>
53 to have C<MooseX::Getopt> ignore your attribute in the commandline options.
55 By default, attributes which start with an underscore are not given
56 commandline argument support, unless the attribute's metaclass is set
57 to L<MooseX::Getopt::Meta::Attribute>. If you don't want your accessors
58 to have the leading underscore in their name, you can do this:
60 # for read/write attributes
61 has '_foo' => (accessor => 'foo', ...);
63 # or for read-only attributes
64 has '_bar' => (reader => 'bar', ...);
66 This will mean that Getopt will not handle a --foo param, but your
67 code can still call the C<foo> method.
69 If your class also uses a configfile-loading role based on
70 L<MooseX::ConfigFromFile>, such as L<MooseX::SimpleConfig>,
71 L<MooseX::Getopt>'s C<new_with_options> will load the configfile
72 specified by the C<--configfile> option (or the default you've
73 given for the configfile attribute) for you.
75 Options specified in multiple places follow the following
76 precedence order: commandline overrides configfile, which
77 overrides explicit new_with_options parameters.
79 =head2 Supported Type Constraints
85 A I<Bool> type constraint is set up as a boolean option with
86 Getopt::Long. So that this attribute description:
88 has 'verbose' => (is => 'rw', isa => 'Bool');
90 would translate into C<verbose!> as a Getopt::Long option descriptor,
91 which would enable the following command line options:
93 % my_script.pl --verbose
94 % my_script.pl --noverbose
96 =item I<Int>, I<Float>, I<Str>
98 These type constraints are set up as properly typed options with
99 Getopt::Long, using the C<=i>, C<=f> and C<=s> modifiers as appropriate.
103 An I<ArrayRef> type constraint is set up as a multiple value option
104 in Getopt::Long. So that this attribute description:
109 default => sub { [] }
112 would translate into C<includes=s@> as a Getopt::Long option descriptor,
113 which would enable the following command line options:
115 % my_script.pl --include /usr/lib --include /usr/local/lib
119 A I<HashRef> type constraint is set up as a hash value option
120 in Getopt::Long. So that this attribute description:
125 default => sub { {} }
128 would translate into C<define=s%> as a Getopt::Long option descriptor,
129 which would enable the following command line options:
131 % my_script.pl --define os=linux --define vendor=debian
135 =head2 Custom Type Constraints
137 It is possible to create custom type constraint to option spec
138 mappings if you need them. The process is fairly simple (but a
139 little verbose maybe). First you create a custom subtype, like
142 subtype 'ArrayOfInts'
144 => where { scalar (grep { looks_like_number($_) } @$_) };
146 Then you register the mapping, like so:
148 MooseX::Getopt::OptionTypeMap->add_option_type_to_map(
149 'ArrayOfInts' => '=i@'
152 Now any attribute declarations using this type constraint will
153 get the custom option spec. So that, this:
157 isa => 'ArrayOfInts',
158 default => sub { [0] }
161 Will translate to the following on the command line:
163 % my_script.pl --nums 5 --nums 88 --nums 199
165 This example is fairly trivial, but more complex validations are
166 easily possible with a little creativity. The trick is balancing
167 the type constraint validations with the Getopt::Long validations.
169 Better examples are certainly welcome :)
171 =head2 Inferred Type Constraints
173 If you define a custom subtype which is a subtype of one of the
174 standard L</Supported Type Constraints> above, and do not explicitly
175 provide custom support as in L</Custom Type Constraints> above,
176 MooseX::Getopt will treat it like the parent type for Getopt
179 For example, if you had the same custom C<ArrayOfInts> subtype
180 from the examples above, but did not add a new custom option
181 type for it to the C<OptionTypeMap>, it would be treated just
182 like a normal C<ArrayRef> type for Getopt purposes (that is,
185 =method B<new_with_options (%params)>
187 This method will take a set of default C<%params> and then collect
188 params from the command line (possibly overriding those in C<%params>)
189 and then return a newly constructed object.
191 The special parameter C<argv>, if specified should point to an array
192 reference with an array to use instead of C<@ARGV>.
194 If L<Getopt::Long/GetOptions> fails (due to invalid arguments),
195 C<new_with_options> will throw an exception.
197 If L<Getopt::Long::Descriptive> is installed and any of the following
198 command line params are passed, the program will exit with usage
199 information (and the option's state will be stored in the help_flag
200 attribute). You can add descriptions for each option by including a
201 B<documentation> option for each attribute to document.
207 If you have L<Getopt::Long::Descriptive> the C<usage> param is also passed to
208 C<new> as the usage option.
212 This accessor contains a reference to a copy of the C<@ARGV> array
213 as it originally existed at the time of C<new_with_options>.
215 =method B<extra_argv>
217 This accessor contains an arrayref of leftover C<@ARGV> elements that
218 L<Getopt::Long> did not parse. Note that the real C<@ARGV> is left
221 B<Important>: By default, L<Getopt::Long> will reject unrecognized I<options>
222 (that is, options that do not correspond with attributes using the Getopt
223 trait). To disable this, and allow options to also be saved in C<extra_argv> (for example to pass along to another class's C<new_with_options>), enable the
224 C<pass_through> option of L<Getopt::Long> for your class: C<use Getopt::Long
225 qw(:config pass_through);>
229 This accessor contains the L<Getopt::Long::Descriptive::Usage> object (if
230 L<Getopt::Long::Descriptive> is used).
234 This accessor contains the boolean state of the --help, --usage and --?
235 options (true if any of these options were passed on the command line).
239 This returns the role meta object.
241 =method B<process_argv (%params)>
243 This does most of the work of C<new_with_options>, analyzing the parameters
244 and argv, except for actually calling the constructor. It returns a
245 L<MooseX::Getopt::ProcessedArgv> object. C<new_with_options> uses this
246 method internally, so modifying this method via subclasses/roles will affect
249 =head2 More Customization Options
251 See L<Getopt::Long#Configuring_Getopt::Long> for many other customizations you
252 can make to how options are parsed. Simply C<use Getopt::Long qw(:config
253 other_options...)> in your class to set these.