App-Cmdline
view release on metacpan or search on metacpan
App::Cmd::Simple. Specifying the options this way, you could (and
probably should) inherit directly from the App::Cmd::Simple without
using "App::Cmdline". Therefore, let's have another example:
sub opt_spec {
my $self = shift;
return
[ 'latitude|y=s' => "geographical latitude" ],
[ 'longitude|x=s' => "geographical longitude" ],
$self->composed_of (
'App::Cmdline::Options::Basic',
'App::Cmdline::Options::DB',
);
}
In this example, your command-line application will recognize the same
options (latitude and longitude) as before and, additionally, all
options that were predefined in the *role* classes
App::Cmdline::Options::Basic and App::Cmdline::Options::DB. See more
about these classes in "PREDEFINED SETS OF OPTIONS";
If not overridden, it returns an empty list.
composed_of
The core method of this module. You call it with a list of names of the
classes that are able to give back a list of predefined options that you
may instantly use. The classes are not only specifying their options
but, for some options, they also do something. For example, the "-h"
option (defined in App::Cmdline::Options::Basic) prints the usage and
exits.
This distribution contains few such classes (see the "PREDEFINED SETS OF
OPTIONS"). Later, they may be published other similar classes providing
different sets of options.
The method returns a list of options definitions that is suitable for
including in the returned values of the opt_spec method (as it was shown
in the example above). The returned value should always be used only at
the end, after your application specifies its own options (those that
are not coming from any predefined set). This is because the last
element of the returned list is a hashref containing configuration for
the Getopt::Long - as described in the Getopt::Long::Descriptive.
Therefore, if you need to call this method more than once or not at the
end, perhaps because you wish to see the options in the help usage in a
different order, you need to remove its last element before you add
anything after that:
sub opt_spec {
my $self = shift;
my @db_options = $self->composed_of ('App::Cmdline::Options::DB');
pop @db_options;
return
@db_options,
[ 'latitude|y=s' => "geographical latitude" ],
[ 'longitude|x=s' => "geographical longitude" ],
$self->composed_of (
'App::Cmdline::Options::Basic',
);
}
The last example looks a bit inconvenient. And you do not need to do it
that way - because the "composed_of" method accepts also any arrayrefs,
ignoring them and just passing them to its return value. That's why you
really can call this method only once and not to be bothered with the
hashref at the end. Here is an example how you can combine class names
(predefined sets) with your own option specification and/or usage
separators (the empty arrayrefs):
return
[ 'check|c' => "only check the configuration" ],
[],
$self->composed_of (
'App::Cmdline::Options::DB',
[ 'show|s' => "show database access properties" ],
[],
'App::Cmdline::Options::Basic',
);
which - when called with the -h option - shows this nicely formatted
usage:
Usage: myapp [short or long options, not bundled]
-c --check only check the configuration
--dbname database name
--dbhost hostname hosting database
--dbport database port number
--dbuser user name to access database
--dbpasswd password to access database
--dbsocket UNIX socket accessing the database
-s --show show database access properties
-h display a short usage message
-v --version display a version
check_for_duplicates
When you are composing options from more sets, it is worth to check
whether, unintentionally, some options are not duplicated. It can be
done by this method that gets the list of options definitions, checks it
(warning if any duplicate was found, and returning the same list
unchanged. It can, therefore, be used like this:
sub opt_spec {
my $self = shift;
return $self->check_for_duplicates (
[ 'latitude|y=s' => "geographical latitude" ],
[ 'longitude|x=s' => "geographical longitude" ],
$self->composed_of (
'App::Cmdline::Options::Basic',
'App::Cmdline::Options::DB',
)
);
}
getopt_conf
The machinery behind the scene is done by the Getopt::Long module. This
module can be configured by a list of strings in order to achieve a
different interpretation of the command-line options. Such as to treat
them case-insensitively, or to allow them to be bundled together. For
the recognized strings you need to read the "Configuring Getopt::Long"
in Getopt::Long. Here is shown how and when to use them.
( run in 0.977 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )