1Getopt::Long::DescriptiUvsee(r3)Contributed Perl DocumenGteattoipotn::Long::Descriptive(3)
2
3
4
6 Getopt::Long::Descriptive - Getopt::Long, but simpler and more powerful
7
9 version 0.103
10
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
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
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.
129
130 If the option specification contains a third element, it adds extra
131 constraints or modifiers to the interpretation and validation of the
132 value. These are the keys that may be present in that hashref, and how
133 they behave:
134
135 implies
136 implies => 'bar'
137 implies => [qw(foo bar)]
138 implies => { foo => 1, bar => 2 }
139
140 If option A has an "implies" entry, then if A is given, other
141 options will be enabled. The value may be a single option to set,
142 an arrayref of options to set, or a hashref of options to set to
143 specific values.
144
145 required
146 required => 1
147
148 If an option is required, failure to provide the option will result
149 in "describe_options" printing the usage message and exiting.
150
151 hidden
152 hidden => 1
153
154 This option will not show up in the usage text.
155
156 You can achieve the same behavior by using the string "hidden" for
157 the option's description.
158
159 one_of
160 one_of => \@subopt_specs
161
162 This is useful for a group of options that are related. Each
163 option spec is added to the list for normal parsing and validation.
164
165 Your option name will end up with a value of the name of the option
166 that was chosen. For example, given the following spec:
167
168 [ "mode" => hidden => { one_of => [
169 [ "get|g" => "get the value" ],
170 [ "set|s" => "set the value" ],
171 [ "delete" => "delete it" ],
172 ] } ],
173
174 No usage text for 'mode' will be displayed, but text for get, set,
175 and delete will be displayed.
176
177 If more than one of get, set, or delete is given, an error will be
178 thrown.
179
180 So, given the @opt_spec above, and an @ARGV of "('--get')", the
181 following would be true:
182
183 $opt->get == 1;
184
185 $opt->mode eq 'get';
186
187 Note: "get" would not be set if "mode" defaulted to 'get' and no
188 arguments were passed in.
189
190 Even though the option sub-specs for "one_of" are meant to be
191 'first class' specs, some options don't make sense with them, e.g.
192 "required".
193
194 As a further shorthand, you may specify "one_of" options using this
195 form:
196
197 [ mode => \@option_specs, \%constraints ]
198
199 shortcircuit
200 shortcircuit => 1
201
202 If this option is present no other options will be returned. Other
203 options present will be checked for proper types, but not for
204 constraints. This provides a way of specifying "--help" style
205 options.
206
207 Params::Validate
208 In addition, any constraint understood by Params::Validate may be
209 used.
210
211 For example, to accept positive integers:
212
213 [ 'max-iterations=i', "maximum number of iterations",
214 { callbacks => { positive => sub { shift() > 0 } } } ],
215
216 (Internally, all constraints are translated into Params::Validate
217 options or callbacks.)
218
219 %arg
220
221 The %arg to "describe_options" is optional. If the last parameter is a
222 hashref, it contains extra arguments to modify the way
223 "describe_options" works. Valid arguments are:
224
225 getopt_conf - an arrayref of strings, passed to Getopt::Long::Configure
226 show_defaults - a boolean which controls whether an option's default
227 value (if applicable) is shown as part of the usage message
228 (for backward compatibility this defaults to false)
229
230 prog_name
231 This routine, exported on demand, returns the basename of $0, grabbed
232 at compile-time. You can override this guess by calling
233 "prog_name($string)" yourself.
234
236 "-types"
237 Any of the Params::Validate type constants ("SCALAR", etc.) can be
238 imported as well. You can get all of them at once by importing
239 "-types".
240
241 "-all"
242 This import group will import "-type", "describe_options", and
243 "prog_name".
244
246 Getopt::Long::Descriptive uses Sub::Exporter to build and export the
247 "describe_options" routine. By writing a new class that extends
248 Getopt::Long::Descriptive, the behavior of the constructed
249 "describe_options" routine can be changed.
250
251 The following methods can be overridden:
252
253 usage_class
254 my $class = Getopt::Long::Descriptive->usage_class;
255
256 This returns the class to be used for constructing a Usage object, and
257 defaults to Getopt::Long::Descriptive::Usage.
258
260 · Getopt::Long
261
262 · Params::Validate
263
265 · Hans Dieter Pearcey <hdp@cpan.org>
266
267 · Ricardo Signes <rjbs@cpan.org>
268
270 · Arthur Axel 'fREW' Schmidt <frioux@gmail.com>
271
272 · Dave Rolsky <autarch@urth.org>
273
274 · Diab Jerius <djerius@cfa.harvard.edu>
275
276 · Hans Dieter Pearcey <hdp@pobox.com>
277
278 · Hans Dieter Pearcey <hdp@weftsoar.net>
279
280 · Harley Pig <harleypig@gmail.com>
281
282 · hdp@cpan.org <hdp@cpan.org@fc0e91e4-031c-0410-8307-be39b06d7656>
283
284 · Karen Etheridge <ether@cpan.org>
285
286 · Niels Thykier <niels@thykier.net>
287
288 · Olaf Alders <olaf@wundersolutions.com>
289
290 · Roman Hubacek <roman.hubacek@centrum.cz>
291
292 · Smylers <SMYLERS@cpan.fsck.com>
293
294 · Thomas Neumann <blacky+perl@fluffbunny.de>
295
296 · zhouzhen1 <zhouzhen1@gmail.com>
297
299 This software is copyright (c) 2005 by Hans Dieter Pearcey.
300
301 This is free software; you can redistribute it and/or modify it under
302 the same terms as the Perl 5 programming language system itself.
303
304
305
306perl v5.28.0 2018-08-02 Getopt::Long::Descriptive(3)