File Coverage

blib/lib/MooseX/Getopt.pm
Criterion Covered Total %
statement 6 6 100.0
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 8 8 100.0


line stmt bran cond sub pod time code
1             package MooseX::Getopt; # git description: v0.74-5-g0d02d3b
2             # ABSTRACT: A Moose role for processing command line options
3             # KEYWORDS: moose extension command line options attributes executable flags switches arguments
4              
5             our $VERSION = '0.75';
6              
7 25     25   3756631 use Moose::Role 0.56;
  25         3807522  
  25         170  
8 25     25   156129 use namespace::autoclean;
  25         170325  
  25         136  
9              
10             with 'MooseX::Getopt::GLD';
11              
12             1;
13              
14             __END__
15              
16             =pod
17              
18             =encoding UTF-8
19              
20             =head1 NAME
21              
22             MooseX::Getopt - A Moose role for processing command line options
23              
24             =head1 VERSION
25              
26             version 0.75
27              
28             =head1 SYNOPSIS
29              
30             ## In your class
31             package My::App;
32             use Moose;
33              
34             with 'MooseX::Getopt';
35              
36             has 'out' => (is => 'rw', isa => 'Str', required => 1);
37             has 'in' => (is => 'rw', isa => 'Str', required => 1);
38              
39             # ... rest of the class here
40              
41             ## in your script
42             #!/usr/bin/perl
43              
44             use My::App;
45              
46             my $app = My::App->new_with_options();
47             # ... rest of the script here
48              
49             ## on the command line
50             % perl my_app_script.pl -in file.input -out file.dump
51              
52             =head1 DESCRIPTION
53              
54             This is a role which provides an alternate constructor for creating
55             objects using parameters passed in from the command line.
56              
57             =head1 METHODS
58              
59             =head2 C<< new_with_options (%params) >>
60              
61             This method will take a set of default C<%params> and then collect
62             parameters from the command line (possibly overriding those in C<%params>)
63             and then return a newly constructed object.
64              
65             The special parameter C<argv>, if specified should point to an array
66             reference with an array to use instead of C<@ARGV>.
67              
68             If L<Getopt::Long/GetOptions> fails (due to invalid arguments),
69             C<new_with_options> will throw an exception.
70              
71             If L<Getopt::Long::Descriptive> is installed and any of the following
72             command line parameters are passed, the program will exit with usage
73             information (and the option's state will be stored in the help_flag
74             attribute). You can add descriptions for each option by including a
75             B<documentation> option for each attribute to document.
76              
77             -?
78             --?
79             -h
80             --help
81             --usage
82              
83             If you have L<Getopt::Long::Descriptive> the C<usage> parameter is also passed to
84             C<new> as the usage option.
85              
86             =head2 C<ARGV>
87              
88             This accessor contains a reference to a copy of the C<@ARGV> array
89             as it originally existed at the time of C<new_with_options>.
90              
91             =head2 C<extra_argv>
92              
93             This accessor contains an arrayref of leftover C<@ARGV> elements that
94             L<Getopt::Long> did not parse. Note that the real C<@ARGV> is left
95             untouched.
96              
97             B<Important>: By default, L<Getopt::Long> will reject unrecognized I<options>
98             (that is, options that do not correspond with attributes using the C<Getopt>
99             trait). To disable this, and allow options to also be saved in C<extra_argv>
100             (for example to pass along to another class's C<new_with_options>), you can either enable the
101             C<pass_through> option of L<Getopt::Long> for your class: C<< use Getopt::Long
102             qw(:config pass_through); >> or specify a value for L<MooseX::Getopt::GLD>'s C<getopt_conf> parameter.
103              
104             =head2 C<usage>
105              
106             This accessor contains the L<Getopt::Long::Descriptive::Usage> object (if
107             L<Getopt::Long::Descriptive> is used).
108              
109             =head2 C<help_flag>
110              
111             This accessor contains the boolean state of the --help, --usage and --?
112             options (true if any of these options were passed on the command line).
113              
114             =head2 C<print_usage_text>
115              
116             This method is called internally when the C<help_flag> state is true.
117             It prints the text from the C<usage> object (see above) to C<STDOUT>
118             (and then after this method is called, the
119             program terminates normally). You can apply a method modification (see
120             L<Moose::Manual::MethodModifiers>) if different behaviour is desired, for
121             example to include additional text.
122              
123             =head2 C<meta>
124              
125             This returns the role meta object.
126              
127             =head2 C<process_argv (%params)>
128              
129             This does most of the work of C<new_with_options>, analyzing the parameters
130             and C<argv>, except for actually calling the constructor. It returns a
131             L<MooseX::Getopt::ProcessedArgv> object. C<new_with_options> uses this
132             method internally, so modifying this method via subclasses/roles will affect
133             C<new_with_options>.
134              
135             =for stopwords DWIM metaclass
136              
137             This module attempts to DWIM as much as possible with the command line
138             parameters by introspecting your class's attributes. It will use the name
139             of your attribute as the command line option, and if there is a type
140             constraint defined, it will configure L<Getopt::Long> to handle the option
141             accordingly.
142              
143             You can use the trait L<MooseX::Getopt::Meta::Attribute::Trait> or the
144             attribute metaclass L<MooseX::Getopt::Meta::Attribute> to get non-default
145             command-line option names and aliases.
146              
147             You can use the trait L<MooseX::Getopt::Meta::Attribute::Trait::NoGetopt>
148             or the attribute metaclass L<MooseX::Getopt::Meta::Attribute::NoGetopt>
149             to have C<MooseX::Getopt> ignore your attribute in the command-line options.
150              
151             By default, attributes which start with an underscore are not given
152             command-line argument support, unless the attribute's metaclass is set
153             to L<MooseX::Getopt::Meta::Attribute>. If you don't want your accessors
154             to have the leading underscore in their name, you can do this:
155              
156             # for read/write attributes
157             has '_foo' => (accessor => 'foo', ...);
158              
159             # or for read-only attributes
160             has '_bar' => (reader => 'bar', ...);
161              
162             This will mean that MooseX::Getopt will not handle a --foo parameter, but your
163             code can still call the C<foo> method.
164              
165             =for stopwords configfile
166              
167             If your class also uses a configfile-loading role based on
168             L<MooseX::ConfigFromFile>, such as L<MooseX::SimpleConfig>,
169             L<MooseX::Getopt>'s C<new_with_options> will load the configfile
170             specified by the C<--configfile> option (or the default you've
171             given for the configfile attribute) for you.
172              
173             Options specified in multiple places follow the following
174             precedence order: command-line overrides configfile, which
175             overrides explicit new_with_options parameters.
176              
177             =head2 Supported Type Constraints
178              
179             =over 4
180              
181             =item I<Bool>
182              
183             A I<Bool> type constraint is set up as a boolean option with
184             L<Getopt::Long>. So that this attribute description:
185              
186             has 'verbose' => (is => 'rw', isa => 'Bool');
187              
188             would translate into C<verbose!> as a L<Getopt::Long> option descriptor,
189             which would enable the following command line options:
190              
191             % my_script.pl --verbose
192             % my_script.pl --noverbose
193              
194             =for stopwords Str
195              
196             =item I<Int>, I<Float>, I<Str>
197              
198             These type constraints are set up as properly typed options with
199             L<Getopt::Long>, using the C<=i>, C<=f> and C<=s> modifiers as appropriate.
200              
201             =item I<ArrayRef>
202              
203             An I<ArrayRef> type constraint is set up as a multiple value option
204             in L<Getopt::Long>. So that this attribute description:
205              
206             has 'include' => (
207             is => 'rw',
208             isa => 'ArrayRef',
209             default => sub { [] }
210             );
211              
212             would translate into C<includes=s@> as a L<Getopt::Long> option descriptor,
213             which would enable the following command line options:
214              
215             % my_script.pl --include /usr/lib --include /usr/local/lib
216              
217             =item I<HashRef>
218              
219             A I<HashRef> type constraint is set up as a hash value option
220             in L<Getopt::Long>. So that this attribute description:
221              
222             has 'define' => (
223             is => 'rw',
224             isa => 'HashRef',
225             default => sub { {} }
226             );
227              
228             would translate into C<define=s%> as a L<Getopt::Long> option descriptor,
229             which would enable the following command line options:
230              
231             % my_script.pl --define os=linux --define vendor=debian
232              
233             =back
234              
235             =head2 Custom Type Constraints
236              
237             =for stopwords subtype
238              
239             It is possible to create custom type constraint to option spec
240             mappings if you need them. The process is fairly simple (but a
241             little verbose maybe). First you create a custom subtype, like
242             so:
243              
244             subtype 'ArrayOfInts'
245             => as 'ArrayRef'
246             => where { scalar (grep { looks_like_number($_) } @$_) };
247              
248             Then you register the mapping, like so:
249              
250             MooseX::Getopt::OptionTypeMap->add_option_type_to_map(
251             'ArrayOfInts' => '=i@'
252             );
253              
254             Now any attribute declarations using this type constraint will
255             get the custom option spec. So that, this:
256              
257             has 'nums' => (
258             is => 'ro',
259             isa => 'ArrayOfInts',
260             default => sub { [0] }
261             );
262              
263             Will translate to the following on the command line:
264              
265             % my_script.pl --nums 5 --nums 88 --nums 199
266              
267             This example is fairly trivial, but more complex validations are
268             easily possible with a little creativity. The trick is balancing
269             the type constraint validations with the L<Getopt::Long> validations.
270              
271             Better examples are certainly welcome :)
272              
273             =head2 Inferred Type Constraints
274              
275             If you define a custom subtype which is a subtype of one of the
276             standard L</Supported Type Constraints> above, and do not explicitly
277             provide custom support as in L</Custom Type Constraints> above,
278             MooseX::Getopt will treat it like the parent type for Getopt
279             purposes.
280              
281             For example, if you had the same custom C<ArrayOfInts> subtype
282             from the examples above, but did not add a new custom option
283             type for it to the C<OptionTypeMap>, it would be treated just
284             like a normal C<ArrayRef> type for Getopt purposes (that is,
285             C<=s@>).
286              
287             =head2 More Customization Options
288              
289             =for stopwords customizations
290              
291             See L<Getopt::Long/Configuring Getopt::Long> for many other customizations you
292             can make to how options are parsed. Simply C<use Getopt::Long qw(:config
293             other_options...)> in your class to set these.
294              
295             Note in particular that the default setting for case sensitivity has changed
296             over time in L<Getopt::Long::Descriptive>, so if you rely on a particular
297             setting, you should set it explicitly, or enforce the version of
298             L<Getopt::Long::Descriptive> that you install.
299              
300             =head1 SEE ALSO
301              
302             =over 4
303              
304             =item *
305              
306             L<MooseX::Getopt::Usage>, an extension to generate man pages, with colour
307              
308             =item *
309              
310             L<MooX::Options>, similar functionality for L<Moo>
311              
312             =back
313              
314             =head1 SUPPORT
315              
316             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-Getopt>
317             (or L<bug-MooseX-Getopt@rt.cpan.org|mailto:bug-MooseX-Getopt@rt.cpan.org>).
318              
319             There is also a mailing list available for users of this distribution, at
320             L<http://lists.perl.org/list/moose.html>.
321              
322             There is also an irc channel available for users of this distribution, at
323             L<C<#moose> on C<irc.perl.org>|irc://irc.perl.org/#moose>.
324              
325             =head1 AUTHOR
326              
327             Stevan Little <stevan@iinteractive.com>
328              
329             =head1 CONTRIBUTORS
330              
331             =for stopwords Karen Etheridge Tomas Doran Stevan Little Yuval Kogman Florian Ragwitz Brandon L Black Shlomi Fish Hans Dieter Pearcey Olaf Alders Dave Rolsky Nelo Onyiah Ryan D Johnson Ricardo SIGNES Ævar Arnfjörð Bjarmason Damien Krotkine Hinrik Örn Sigurðsson Chris Prather Devin Austin Gregory Oschwald Jose Luis Martinez Todd Hepler Andreas Koenig König Dagfinn Ilmari Mannsåker Damyan Ivanov Drew Taylor Gordon Irving Jesse Luehrs John Goulah Jonathan Swartz Justin Hunter Michael Schout Stuart A Johnston
332              
333             =over 4
334              
335             =item *
336              
337             Karen Etheridge <ether@cpan.org>
338              
339             =item *
340              
341             Tomas Doran <bobtfish@bobtfish.net>
342              
343             =item *
344              
345             Stevan Little <stevan.little@iinteractive.com>
346              
347             =item *
348              
349             Yuval Kogman <nothingmuch@woobling.org>
350              
351             =item *
352              
353             Florian Ragwitz <rafl@debian.org>
354              
355             =item *
356              
357             Brandon L Black <blblack@gmail.com>
358              
359             =item *
360              
361             Shlomi Fish <shlomif@cpan.org>
362              
363             =item *
364              
365             Hans Dieter Pearcey <hdp@weftsoar.net>
366              
367             =item *
368              
369             Olaf Alders <olaf@wundersolutions.com>
370              
371             =item *
372              
373             Dave Rolsky <autarch@urth.org>
374              
375             =item *
376              
377             Nelo Onyiah <nelo.onyiah@gmail.com>
378              
379             =item *
380              
381             Ryan D Johnson <ryan@innerfence.com>
382              
383             =item *
384              
385             Ricardo SIGNES <rjbs@cpan.org>
386              
387             =item *
388              
389             Ævar Arnfjörð Bjarmason <avarab@gmail.com>
390              
391             =item *
392              
393             Damien Krotkine <dkrotkine@weborama.com>
394              
395             =item *
396              
397             Hinrik Örn Sigurðsson <hinrik.sig@gmail.com>
398              
399             =item *
400              
401             Chris Prather <chris@prather.org>
402              
403             =item *
404              
405             Devin Austin <dhoss@cpan.org>
406              
407             =item *
408              
409             Gregory Oschwald <goschwald@maxmind.com>
410              
411             =item *
412              
413             Jose Luis Martinez <jlmartinez@capside.com>
414              
415             =item *
416              
417             Todd Hepler <thepler@employees.org>
418              
419             =item *
420              
421             Andreas Koenig <andk@cpan.org>
422              
423             =item *
424              
425             Andreas König <Andreas.Koenig.extern@telecolumbus.de>
426              
427             =item *
428              
429             Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>
430              
431             =item *
432              
433             Damyan Ivanov <dmn@debian.org>
434              
435             =item *
436              
437             Drew Taylor <drew@drewtaylor.com>
438              
439             =item *
440              
441             Gordon Irving <goraxe@goraxe.me.uk>
442              
443             =item *
444              
445             Jesse Luehrs <doy@tozt.net>
446              
447             =item *
448              
449             John Goulah <jgoulah@cpan.org>
450              
451             =item *
452              
453             Jonathan Swartz <swartz@pobox.com>
454              
455             =item *
456              
457             Justin Hunter <justin.d.hunter@gmail.com>
458              
459             =item *
460              
461             Michael Schout <mschout@gkg.net>
462              
463             =item *
464              
465             Shlomi Fish <shlomif@shlomifish.org>
466              
467             =item *
468              
469             Stevan Little <stevan.little@gmail.com>
470              
471             =item *
472              
473             Stuart A Johnston <saj_git@thecommune.net>
474              
475             =back
476              
477             =head1 COPYRIGHT AND LICENSE
478              
479             This software is copyright (c) 2007 by Infinity Interactive, Inc.
480              
481             This is free software; you can redistribute it and/or modify it under
482             the same terms as the Perl 5 programming language system itself.
483              
484             =cut