|
NAMEGetopt::Long::Descriptive - Getopt::Long, but simpler and more powerfulVERSIONversion 0.110SYNOPSISuse Getopt::Long::Descriptive; my ($opt, $usage) = describe_options( 'my-program %o <some-arg>', [ 'server|s=s', "the server to connect to", { required => 1 } ], [ 'port|p=i', "the port to connect to", { default => 79 } ], [], [ 'verbose|v', "print extra stuff" ], [ 'help', "print usage message and exit", { shortcircuit => 1 } ], ); print($usage->text), exit if $opt->help; Client->connect( $opt->server, $opt->port ); print "Connected!\n" if $opt->verbose; ...and running "my-program --help" will produce: my-program [-psv] [long options...] <some-arg> -s --server the server to connect to -p --port the port to connect to -v --verbose print extra stuff --help print usage message and exit DESCRIPTIONGetopt::Long::Descriptive is yet another Getopt library. It's built atop Getopt::Long, and gets a lot of its features, but tries to avoid making you think about its huge array of options.It also provides usage (help) messages, data validation, and a few other useful features. PERL VERSIONThis library should run on perls released even a long time ago. It should work on any version of perl released in the last five years.Although it may work on older versions of perl, no guarantee is made that the minimum required version will not be increased. The version may be increased for any reason, and there is no promise that patches will be accepted to lower the minimum required perl. FUNCTIONSGetopt::Long::Descriptive only exports one routine by default: "describe_options". All GLD's exports are exported by Sub::Exporter.describe_optionsmy ($opt, $usage) = describe_options($usage_desc, @opt_spec, \%arg); This routine inspects @ARGV for options that match the supplied spec. If all the options are valid then it returns the options given and an object for generating usage messages; if not then it dies with an explanation of what was wrong and a usage message. The $opt object will be a dynamically-generated subclass of Getopt::Long::Descriptive::Opts. In brief, each of the options in @opt_spec becomes an accessor method on the object, using the first-given name, with dashes converted to underscores. For more information, see the documentation for the Opts class. The $usage object will be a Getopt::Long::Descriptive::Usage object, which provides a "text" method to get the text of the usage message and "die" to die with it. For more methods and options, consults the documentation for the Usage class. $usage_desc The $usage_desc parameter to "describe_options" is a "sprintf"-like string that is used in generating the first line of the usage message. It's a one-line summary of how the command is to be invoked. A typical usage description might be: $usage_desc = "%c %o <source> <desc>"; %c will be replaced with what Getopt::Long::Descriptive thinks is the program name (it's computed from $0, see "prog_name"). %o will be replaced with a list of the short options, as well as the text "[long options...]" if any have been defined. The rest of the usage description can be used to summarize what arguments are expected to follow the program's options, and is entirely free-form. Literal "%" characters will need to be written as "%%", just like with "sprintf". @opt_spec The @opt_spec part of the args to "describe_options" is used to configure option parsing and to produce the usage message. Each entry in the list is an arrayref describing one option, like this: @opt_spec = ( [ "verbose|V" => "be noisy" ], [ "logfile=s" => "file to log to" ], ); The first value in the arrayref is a Getopt::Long-style option specification. In brief, they work like this: each one is a pipe-delimited list of names, optionally followed by a type declaration. Type declarations are '=x' or ':x', where "=" means a value is required and ":" means it is optional. x may be 's' to indicate a string is required, 'i' for an integer, or 'f' for a number with a fractional part. The type spec may end in "@" to indicate that the option may appear multiple times. For more information on how these work, see the Getopt::Long documentation. The first name given should be the canonical name, as it will be used as the accessor method on the $opt object. Dashes in the name will be converted to underscores, and all letters will be lowercased. For this reason, all options should generally have a long-form name. The second value in the arrayref is a description of the option, for use in the usage message. Special Option Specifications If the option specification (arrayref) is empty, it will have no effect other than causing a blank line to appear in the usage message. If the option specification contains only one element, it will be printed in the usage message with no other effect. If the element is a reference, its referent will be printed as-is. Otherwise, it will be reformatted like other text in the usage message. If the option specification contains a third element, it adds extra constraints or modifiers to the interpretation and validation of the value. These are the keys that may be present in that hashref, and how they behave:
%arg The %arg to "describe_options" is optional. If the last parameter is a hashref, it contains extra arguments to modify the way "describe_options" works. Valid arguments are: getopt_conf - an arrayref of strings, passed to Getopt::Long::Configure show_defaults - a boolean which controls whether an option's default value (if applicable) is shown as part of the usage message (for backward compatibility this defaults to false) prog_nameThis routine, exported on demand, returns the basename of $0, grabbed at compile-time. You can override this guess by calling "prog_name($string)" yourself.OTHER EXPORTS"-types"Any of the Params::Validate type constants ("SCALAR", etc.) can be imported as well. You can get all of them at once by importing "-types"."-all"This import group will import "-type", "describe_options", and "prog_name".CUSTOMIZINGGetopt::Long::Descriptive uses Sub::Exporter to build and export the "describe_options" routine. By writing a new class that extends Getopt::Long::Descriptive, the behavior of the constructed "describe_options" routine can be changed.The following methods can be overridden: usage_classmy $class = Getopt::Long::Descriptive->usage_class; This returns the class to be used for constructing a Usage object, and defaults to Getopt::Long::Descriptive::Usage. SEE ALSO
AUTHORS
CONTRIBUTORS
COPYRIGHT AND LICENSEThis software is copyright (c) 2005 by Hans Dieter Pearcey.This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
Visit the GSP FreeBSD Man Page Interface. |