PERL Modules | Getopt-LongUsage |
CodePin.org |
| ABOUT |
|
Describe the usage of Getopt::Long options in human readable format |
| AVAILABILITY | ||||||||
|
View the Getopt-LongUsage
README,
CHANGES,
online. Download the source code from Subversion.
Download from CPAN: http://cpan.perl.org/modules/by-module/Getopt/ |
| POD DOCUMENTATION |
NAMEGetopt::LongUsage - Describe the usage of Getopt::Long options in human readable format
SYNOPSISProvide the description for Getopt::Long options in order to generate a descriptive usage for the user. Example code:
use Getopt::Long;
use Getopt::LongUsage;
my ($width,$length,$verbose,$help);
my @getoptconf = ( 'width=i' => \$width,
'length=i' => \$length,
'verbose|v' => \$verbose,
'help|h' => \$help
);
my $usage = sub {
my @getopt_long_configuration = @_;
GetLongUsage (
'cli_use' => ($0 ." [options]"),
'descriptions' =>
[ 'width' => "The width",
'length' => "The length",
'verbose' => "verbose",
'help' => "this help message"
],
'Getopt_Long' => \@getopt_long_configuration,
);
};
GetOptions( @getoptconf ) || die ($usage->( @getoptconf ),"\n");
...etc...
Example output:
linux$ ./test_it.pl --not-an-option
Unknown option: not-an-option
./test_it.pl [options]
--width The width
--length The length
-v, --verbose verbose
-h, --help this help message
DESCRIPTIONThis is a pure perl module which generates a user help message for a perl script
that implements the
REQUIREMENTSThe following perl modules are depended on by this module:
IMPORTED METHODSWhen the calling application invokes this module in a use clause, the following method will be imported into its space.
METHODS
newCreate a new object instances of this module. It is not necessary to create an object for this module, as the methods can be called outside of OO style programming.
my $glu = new Getopt::LongUsage();
ParseGetoptLongConfigParse option configuration for This method reproduces enough code as found in
my $configmap = ParseGetoptLongConfig (
\%options,
'isAvailable',
'color=s',
'type=s',
'cityGrown=s@',
'secretAttr=i',
'verbose|v',
'help|h'
);
my @getoptconf = (
'isAvailable' => \$isAvailable,
'color=s' => \$color,
'type=s' => \$type,
'cityGrown=s@' => \@cityGrown,
'secretAttr:i' => \$secretAttr,
'verbose|v' => \$verbose,
'help|h' => \$help
);
my $configmap = ParseGetoptLongConfig ( @getoptconf );
GetLongUsageGenerate a usage message from Getopt::Long options.
GetLongUsage ( header => "This is my header text",
footer => "This is my footer text"
cli_use => "myprog [options] arg1 arg2",
descriptions => \@descriptions,
format => \@format_options,
hidden_opts => \@hidden_options,
Getopt_Long => \@getopt_long_options
);
EXAMPLES
Actually, the descriptions are not needed eitherExample code:
use Getopt::Long;
use Getopt::LongUsage;
my %options;
my @getoptconf = ( \%options,
'isAvailable',
'color=s',
'type=s',
'cityGrown=s@',
'verbose|v',
'help|h'
);
my $usage = sub {
my @getopt_long_configuration = @_;
GetLongUsage (
'Getopt_Long' => \@getopt_long_configuration,
);
};
GetOptions( @getoptconf ) || die ($usage->( @getoptconf ),"\n");
...etc...
Example output:
linux$ perl test.pl --notanoption
Unknown option: notanoption
-v, --verbose
--isAvailable
--color
-h, --help
--type
--cityGrown
Using more formatting optionsExample code:
use Getopt::Long;
use Getopt::LongUsage;
my $VERSION = "2.1.5";
my %options;
my @getoptconf = ( \%options,
'isAvailable',
'color=s',
'type=s',
'cityGrown=s@',
'secretAttr:i',
'verbose|v',
'help|h'
);
my $usage = sub {
my @getopt_long_configuration = @_;
GetLongUsage (
'header' => ("MyApple Program version ".$VERSION."\n".'Author Smith <author@example.com>'."\n"),
'cli_use' => ($0 ."[options] <args>"),
'descriptions' =>
[ 'isAvailable' => "The apple type is available",
'color' => "The color of this apple type",
'type' => "The type of apple, i.e. \"Gala\"",
'cityGrown' => "The city(s) in which this apple is grown",
'secretAttr' => "You should not see this option",
'verbose' => "verbose",
'help' => "help"
],
'hidden_opts => [qw(secretAttr)]
'footer' => undef,
'format' =>
[ 'tab' => 4,
'indent' => 0
],
'Getopt_Long' => \@getopt_long_configuration,
);
};
GetOptions( @getoptconf ) || die ($usage->( @getoptconf ),"\n");
...etc...
Example output:
MyApple Program version 2.1.5
Author Smith <author@example.com>
script.pl [options] <args>
--isAvailable The apple type is available
--color The color of this apple type
--type The type of apple, i.e. "Gala"
--cityGrown The city(s) in which this apple is grown
-v, --verbose verbose
-h, --help help
Combining Getopt::XML with Getopt::LongUsageConsidering the following XML File '/path/to/xml_file.xml' and content:
<apple>
<color>red</color>
<type>red delicious</type>
<isAvailable/>
<cityGrown>Macomb</cityGrown>
<cityGrown>Peoria</cityGrown>
<cityGrown>Galesburg</cityGrown>
</apple>
Using that XML file as input for default values:
use Getopt::Long;
use Getopt::LongUsage;
use Getopt::XML qw(GetXMLOptionsFromFile);
use Data::Dump qw(pp);
#
# Set the Getopt::Long Configuration
my @GetoptLongConfig = (
\%options,
'isAvailable',
'color=s',
'type=s',
'cityGrown=s@'
);
#
# Read the XML data in as arguments to Getopt::Long
my %options;
GetXMLOptionsFromFile (
xmlfile => '/path/to/xml_file.xml',
xmlpath => '/apple',
Getopt_Long => \@GetoptLongConfig
);
print "==My Default Values==\n";
pp(\%options);
#
# Setup the user's help message for Getopt::Long
my $usage = sub {
my @getopt_long_configuration = @_;
GetLongUsage (
'header' => ("MyApple Program version ".$VERSION."\n".'Author Smith <author@example.com>'."\n"),
'cli_use' => ($0 ." [options] <args>"),
'descriptions' =>
[ 'isAvailable' => "The apple type is available",
'color' => "The color of this apple type",
'type' => "The type of apple, i.e. \"Gala\"",
'cityGrown' => "The city(s) in which this apple is grown"
],
'footer' => "\n",
'format' =>
[ 'tab' => 2,
'indent' => 2
],
'Getopt_Long' => \@getopt_long_configuration,
);
};
#
# Finally retrieve and absorb the user provided input via Getopt::Long
GetOptions( @GetoptLongConfig ) || die ($usage->( @GetoptLongConfig ));
print "==My Runtime Values==\n";
pp(\%options);
Example output when providing an invalid option
linux$ perl test.pl --notanoption
==My Default Values==
{
cityGrown => ["Macomb", "Peoria", "Galesburg"],
color => "red",
isAvailable => 1,
type => "red delicious",
}
Unknown option: notanoption
MyApple Program version
Author Smith <author@example.com>
test.pl [options] <args>
--isAvailable The apple type is available
--color The color of this apple type
--type The type of apple, i.e. "Gala"
--cityGrown The city(s) in which this apple is grown
Example output when providing a valid option
linux$ perl test.pl --color="blue" --type="blue delicious"
==My Default Values==
{
cityGrown => ["Macomb", "Peoria", "Galesburg"],
color => "red",
isAvailable => 1,
type => "red delicious",
}
==My Runtime Values==
{
cityGrown => ["Macomb", "Peoria", "Galesburg"],
color => "blue",
isAvailable => 1,
type => "blue delicious",
}
TODO
AUTHORRussell E Glaue, http://russ.glaue.org
SEE ALSO
Getopt::LongUsage on Codepin: http://www.codepin.org/project/perlmod/Getopt-LongUsage
COPYRIGHT AND LICENSECopyright (c) 2010 Center for the Application of Information Technologies. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. |