1Getopt::Long::DescriptiUvsee(r3)Contributed Perl DocumenGteattoipotn::Long::Descriptive(3)
2
3
4

NAME

6       Getopt::Long::Descriptive - Getopt::Long, but simpler and more powerful
7

VERSION

9       version 0.109
10

SYNOPSIS

12         use Getopt::Long::Descriptive;
13
14         my ($opt, $usage) = describe_options(
15           'my-program %o <some-arg>',
16           [ 'server|s=s', "the server to connect to", { required => 1  } ],
17           [ 'port|p=i',   "the port to connect to",   { default  => 79 } ],
18           [],
19           [ 'verbose|v',  "print extra stuff"            ],
20           [ 'help',       "print usage message and exit", { shortcircuit => 1 } ],
21         );
22
23         print($usage->text), exit if $opt->help;
24
25         Client->connect( $opt->server, $opt->port );
26
27         print "Connected!\n" if $opt->verbose;
28
29       ...and running "my-program --help" will produce:
30
31         my-program [-psv] [long options...] <some-arg>
32           -s --server     the server to connect to
33           -p --port       the port to connect to
34
35           -v --verbose    print extra stuff
36           --help          print usage message and exit
37

DESCRIPTION

39       Getopt::Long::Descriptive is yet another Getopt library.  It's built
40       atop Getopt::Long, and gets a lot of its features, but tries to avoid
41       making you think about its huge array of options.
42
43       It also provides usage (help) messages, data validation, and a few
44       other useful features.
45

FUNCTIONS

47       Getopt::Long::Descriptive only exports one routine by default:
48       "describe_options".  All GLD's exports are exported by Sub::Exporter.
49
50   describe_options
51         my ($opt, $usage) = describe_options($usage_desc, @opt_spec, \%arg);
52
53       This routine inspects @ARGV for options that match the supplied spec.
54       If all the options are valid then it returns the options given and an
55       object for generating usage messages; if not then it dies with an
56       explanation of what was wrong and a usage message.
57
58       The $opt object will be a dynamically-generated subclass of
59       Getopt::Long::Descriptive::Opts.  In brief, each of the options in
60       @opt_spec becomes an accessor method on the object, using the first-
61       given name, with dashes converted to underscores.  For more
62       information, see the documentation for the Opts class.
63
64       The $usage object will be a Getopt::Long::Descriptive::Usage object,
65       which provides a "text" method to get the text of the usage message and
66       "die" to die with it.  For more methods and options, consults the
67       documentation for the Usage class.
68
69       $usage_desc
70
71       The $usage_desc parameter to "describe_options" is a "sprintf"-like
72       string that is used in generating the first line of the usage message.
73       It's a one-line summary of how the command is to be invoked.  A typical
74       usage description might be:
75
76         $usage_desc = "%c %o <source> <desc>";
77
78       %c will be replaced with what Getopt::Long::Descriptive thinks is the
79       program name (it's computed from $0, see "prog_name").
80
81       %o will be replaced with a list of the short options, as well as the
82       text "[long options...]" if any have been defined.
83
84       The rest of the usage description can be used to summarize what
85       arguments are expected to follow the program's options, and is entirely
86       free-form.
87
88       Literal "%" characters will need to be written as "%%", just like with
89       "sprintf".
90
91       @opt_spec
92
93       The @opt_spec part of the args to "describe_options" is used to
94       configure option parsing and to produce the usage message.  Each entry
95       in the list is an arrayref describing one option, like this:
96
97         @opt_spec = (
98           [ "verbose|V" => "be noisy"       ],
99           [ "logfile=s" => "file to log to" ],
100         );
101
102       The first value in the arrayref is a Getopt::Long-style option
103       specification.  In brief, they work like this:  each one is a pipe-
104       delimited list of names, optionally followed by a type declaration.
105       Type declarations are '=x' or ':x', where "=" means a value is required
106       and ":" means it is optional.  x may be 's' to indicate a string is
107       required, 'i' for an integer, or 'f' for a number with a fractional
108       part.  The type spec may end in "@" to indicate that the option may
109       appear multiple times.
110
111       For more information on how these work, see the Getopt::Long
112       documentation.
113
114       The first name given should be the canonical name, as it will be used
115       as the accessor method on the $opt object.  Dashes in the name will be
116       converted to underscores, and all letters will be lowercased.  For this
117       reason, all options should generally have a long-form name.
118
119       The second value in the arrayref is a description of the option, for
120       use in the usage message.
121
122       Special Option Specifications
123
124       If the option specification (arrayref) is empty, it will have no effect
125       other than causing a blank line to appear in the usage message.
126
127       If the option specification contains only one element, it will be
128       printed in the usage message with no other effect.  If the element is a
129       reference, its referent will be printed as-is.  Otherwise, it will be
130       reformatted like other text in the usage message.
131
132       If the option specification contains a third element, it adds extra
133       constraints or modifiers to the interpretation and validation of the
134       value.  These are the keys that may be present in that hashref, and how
135       they behave:
136
137       implies
138             implies => 'bar'
139             implies => [qw(foo bar)]
140             implies => { foo => 1, bar => 2 }
141
142           If option A has an "implies" entry, then if A is given, other
143           options will be enabled.  The value may be a single option to set,
144           an arrayref of options to set, or a hashref of options to set to
145           specific values.
146
147       required
148             required => 1
149
150           If an option is required, failure to provide the option will result
151           in "describe_options" printing the usage message and exiting.
152
153       hidden
154             hidden => 1
155
156           This option will not show up in the usage text.
157
158           You can achieve the same behavior by using the string "hidden" for
159           the option's description.
160
161       one_of
162             one_of => \@subopt_specs
163
164           This is useful for a group of options that are related.  Each
165           option spec is added to the list for normal parsing and validation.
166
167           Your option name will end up with a value of the name of the option
168           that was chosen.  For example, given the following spec:
169
170             [ "mode" => hidden => { one_of => [
171               [ "get|g"  => "get the value" ],
172               [ "set|s"  => "set the value" ],
173               [ "delete" => "delete it" ],
174             ] } ],
175
176           No usage text for 'mode' will be displayed, but text for get, set,
177           and delete will be displayed.
178
179           If more than one of get, set, or delete is given, an error will be
180           thrown.
181
182           So, given the @opt_spec above, and an @ARGV of "('--get')", the
183           following would be true:
184
185             $opt->get == 1;
186
187             $opt->mode eq 'get';
188
189           Note: "get" would not be set if "mode" defaulted to 'get' and no
190           arguments were passed in.
191
192           Even though the option sub-specs for "one_of" are meant to be
193           'first class' specs, some options don't make sense with them, e.g.
194           "required".
195
196           As a further shorthand, you may specify "one_of" options using this
197           form:
198
199             [ mode => \@option_specs, \%constraints ]
200
201       shortcircuit
202             shortcircuit => 1
203
204           If this option is present no other options will be returned.  Other
205           options present will be checked for proper types, but not for
206           constraints.  This provides a way of specifying "--help" style
207           options.
208
209       Params::Validate
210           In addition, any constraint understood by Params::Validate may be
211           used.
212
213           For example, to accept positive integers:
214
215             [ 'max-iterations=i', "maximum number of iterations",
216               { callbacks => { positive => sub { shift() > 0 } } } ],
217
218           (Internally, all constraints are translated into Params::Validate
219           options or callbacks.)
220
221       %arg
222
223       The %arg to "describe_options" is optional.  If the last parameter is a
224       hashref, it contains extra arguments to modify the way
225       "describe_options" works.  Valid arguments are:
226
227         getopt_conf   - an arrayref of strings, passed to Getopt::Long::Configure
228         show_defaults - a boolean which controls whether an option's default
229                         value (if applicable) is shown as part of the usage message
230                         (for backward compatibility this defaults to false)
231
232   prog_name
233       This routine, exported on demand, returns the basename of $0, grabbed
234       at compile-time.  You can override this guess by calling
235       "prog_name($string)" yourself.
236

OTHER EXPORTS

238   "-types"
239       Any of the Params::Validate type constants ("SCALAR", etc.) can be
240       imported as well.  You can get all of them at once by importing
241       "-types".
242
243   "-all"
244       This import group will import "-type", "describe_options", and
245       "prog_name".
246

CUSTOMIZING

248       Getopt::Long::Descriptive uses Sub::Exporter to build and export the
249       "describe_options" routine.  By writing a new class that extends
250       Getopt::Long::Descriptive, the behavior of the constructed
251       "describe_options" routine can be changed.
252
253       The following methods can be overridden:
254
255   usage_class
256         my $class = Getopt::Long::Descriptive->usage_class;
257
258       This returns the class to be used for constructing a Usage object, and
259       defaults to Getopt::Long::Descriptive::Usage.
260

SEE ALSO

262       •   Getopt::Long
263
264       •   Params::Validate
265

AUTHORS

267       •   Hans Dieter Pearcey <hdp@cpan.org>
268
269       •   Ricardo Signes <rjbs@cpan.org>
270

CONTRIBUTORS

272       •   Arthur Axel 'fREW' Schmidt <frioux@gmail.com>
273
274       •   Dave Rolsky <autarch@urth.org>
275
276       •   Diab Jerius <djerius@cfa.harvard.edu>
277
278       •   Hans Dieter Pearcey <hdp@pobox.com>
279
280       •   Hans Dieter Pearcey <hdp@weftsoar.net>
281
282       •   Harley Pig <harleypig@gmail.com>
283
284       •   hdp@cpan.org <hdp@cpan.org@fc0e91e4-031c-0410-8307-be39b06d7656>
285
286       •   Karen Etheridge <ether@cpan.org>
287
288       •   Michael McClimon <michael@mcclimon.org>
289
290       •   Niels Thykier <niels@thykier.net>
291
292       •   Olaf Alders <olaf@wundersolutions.com>
293
294       •   Ricardo Signes <rjbs@semiotic.systems>
295
296       •   Roman Hubacek <roman.hubacek@centrum.cz>
297
298       •   Smylers <SMYLERS@cpan.fsck.com>
299
300       •   Thomas Neumann <blacky+perl@fluffbunny.de>
301
302       •   zhouzhen1 <zhouzhen1@gmail.com>
303
305       This software is copyright (c) 2005 by Hans Dieter Pearcey.
306
307       This is free software; you can redistribute it and/or modify it under
308       the same terms as the Perl 5 programming language system itself.
309
310
311
312perl v5.34.0                      2021-07-22      Getopt::Long::Descriptive(3)
Impressum