App-Cmdline
view release on metacpan or search on metacpan
sprintf.
By default, the "App::Cmdline" returns slightly different usage
description depending on the bundling configuration option (see
getopt_conf): if the bundling is disabled, the bundle of all short
options is not shown. Often, you want to use whatever "App::Cmdline"
returns plus what you wish to add on the first line of the usage. For
example:
sub usage_desc {
return shift->SUPER::usage_desc() . ' ...and anything else';
}
validate_args
Originally, this method was meant to check (validate) the command-line
arguments (remember that arguments are whatever remains on the
command-line after options defined in the opt_spec method have been
processed). The options themselves could be already validated by various
subroutines and attributes given in the option specifications (as
described, sometimes only vaguely, in the Getopt::Long::Descriptive).
But sometimes, it is useful to have all validation, of options and of
remaining arguments on the command-line.
*Important:* Some predefined sets of options (see the "PREDEFINED SETS
OF OPTIONS") do also some checking (or other actions, like printing the
version and exiting) and this checking is invoked from the
"App::Cmdline"'s validate_args method. Therefore, it is strongly
recommended that if you overwrite this method, you also call the SUPER:
sub validate_args {
my ($self, $opt, $args) = @_;
$self->SUPER::validate_args ($opt, $args);
if ($opt->number and scalar @$args != $opt->number) {
$self->usage_error ("Option --number does not correspond with the number of arguments");
}
}
senger@ShereKhan2:myapp -n 2 a b c
Error: Option --number does not correspond with the number of arguments
Usage: myapp [short or long options, not bundled] <some arguments...>
-n --number expected number of args
-h display a short usage message
docs/App-Cmdline.html view on Meta::CPAN
<li>%o will be replaced with a list of the short options, as well as the text "[long options...]" if any have been defined.</li>
<li>Literal % characters will need to be written as %%, just like with sprintf.</li>
</ul>
<p>By default, the <code>App::Cmdline</code> returns slightly different usage description depending on the bundling configuration option (see <a href="#getopt_conf" class="podlinkpod"
>getopt_conf</a>): if the bundling is disabled, the bundle of all short options is not shown. Often, you want to use whatever <code>App::Cmdline</code> returns plus what you wish to add on the first line of the usage. For example:</p>
<pre> sub usage_desc {
return shift->SUPER::usage_desc() . ' ...and anything else';
}</pre>
<h2><a class='u' href='#___top' title='click to go to top of document'
name="validate_args"
><b>validate_args</b></a></h2>
<p>Originally, this method was meant to check (validate) the command-line arguments (remember that arguments are whatever remains on the command-line after options defined in the <a href="#opt_spec" class="podlinkpod"
>opt_spec</a> method have been processed). The options themselves could be already validated by various subroutines and attributes given in the option specifications (as described, sometimes only vaguely, in the <a href="http://search.cpan.org/perldo...
>Getopt::Long::Descriptive</a>). But sometimes, it is useful to have all validation, of options and of arguments, in one place - so we have this method.</p>
<p>The method gets two parameters, <code>$opt</code> and <code>$args</code>. The first one is an instance of <a href="http://search.cpan.org/perldoc?Getopt%3A%3ALong%3A%3ADescriptive%3A%3AOpts" class="podlinkpod"
>Getopt::Long::Descriptive::Opts</a> giving you access to all existing options, using their names (as were defined in <a href="#opt_spec" class="podlinkpod"
>opt_spec</a>) as the access methods. The second parameter is an arrayref containing all remaining arguments on the command-line.</p>
<p><i>Important:</i> Some predefined sets of options (see the <a href="#PREDEFINED_SETS_OF_OPTIONS" class="podlinkpod"
>"PREDEFINED SETS OF OPTIONS"</a>) do also some checking (or other actions, like printing the version and exiting) and this checking is invoked from the <code>App::Cmdline</code>'s validate_args method. Therefore, it is strongly recommend...
<pre> sub validate_args {
my ($self, $opt, $args) = @_;
$self->SUPER::validate_args ($opt, $args);
if ($opt->number and scalar @$args != $opt->number) {
$self->usage_error ("Option --number does not correspond with the number of arguments");
}
}
senger@ShereKhan2:myapp -n 2 a b c
Error: Option --number does not correspond with the number of arguments
Usage: myapp [short or long options, not bundled] <some arguments...>
-n --number expected number of args
-h display a short usage message
lib/App/Cmdline.pm view on Meta::CPAN
=back
By default, the C<App::Cmdline> returns slightly different usage
description depending on the bundling configuration option (see
L<getopt_conf|"getopt_conf">): if the bundling is disabled, the bundle
of all short options is not shown. Often, you want to use whatever
C<App::Cmdline> returns plus what you wish to add on the first line of
the usage. For example:
sub usage_desc {
return shift->SUPER::usage_desc() . ' ...and anything else';
}
=head2 B<validate_args>
Originally, this method was meant to check (validate) the command-line
arguments (remember that arguments are whatever remains on the
command-line after options defined in the L<opt_spec|"opt_spec">
method have been processed). The options themselves could be already
validated by various subroutines and attributes given in the option
specifications (as described, sometimes only vaguely, in the
lib/App/Cmdline.pm view on Meta::CPAN
an arrayref containing all remaining arguments on the command-line.
I<Important:> Some predefined sets of options (see the L<"PREDEFINED
SETS OF OPTIONS">) do also some checking (or other actions, like
printing the version and exiting) and this checking is invoked from
the C<App::Cmdline>'s validate_args method. Therefore, it is strongly
recommended that if you overwrite this method, you also call the SUPER:
sub validate_args {
my ($self, $opt, $args) = @_;
$self->SUPER::validate_args ($opt, $args);
if ($opt->number and scalar @$args != $opt->number) {
$self->usage_error ("Option --number does not correspond with the number of arguments");
}
}
senger@ShereKhan2:myapp -n 2 a b c
Error: Option --number does not correspond with the number of arguments
Usage: myapp [short or long options, not bundled] <some arguments...>
-n --number expected number of args
-h display a short usage message
lib/App/Cmdline/Options/ExtBasic.pm view on Meta::CPAN
my @OPT_SPEC = (
[ 'help' => "display a full usage message" ],
[ 'man|m' => "display a full manual page" ],
[ 'quiet|q' => "skip various progress messages" ],
);
# ----------------------------------------------------------------
# Return definition of my options
# ----------------------------------------------------------------
sub get_opt_spec {
return shift->SUPER::get_opt_spec(), @OPT_SPEC;
}
# ----------------------------------------------------------------
# Do typical actions with my options
# ----------------------------------------------------------------
sub validate_opts {
my ($class, $app, $caller, $opt, $args) = @_;
$class->SUPER::validate_opts ($app, $caller, $opt, $args);
# show various levels of help and exit
my $pod_where = pod_where ({-inc => 1}, $app);
pod2usage (-input => $pod_where, -verbose => 1, -exitval => 'NOEXIT')
if $opt->can ('help') and $opt->help;
pod2usage (-input => $pod_where, -verbose => 2, -exitval => 'NOEXIT')
if $opt->can ('man') and $opt->man;
if ( ($opt->can ('help') and $opt->help) or ($opt->can ('man') and $opt->man) ) {
if ($^S) { die "Okay\n" } else { exit (0) };
}
lib/App/Cmdline/Options/ExtDB.pm view on Meta::CPAN
our $VERSION = '0.1.2'; # VERSION
my @OPT_SPEC = (
[ 'dbshow' => "show database access properties" ],
);
# ----------------------------------------------------------------
# Return definition of my options
# ----------------------------------------------------------------
sub get_opt_spec {
return shift->SUPER::get_opt_spec(), @OPT_SPEC;
}
# ----------------------------------------------------------------
# Do typical actions with my options
# ----------------------------------------------------------------
sub validate_opts {
my ($class, $app, $caller, $opt, $args) = @_;
if ($opt->dbshow) {
print "DBNAME: ", ($opt->dbname || 'n/a'), "\n";
t/01-testapp.t view on Meta::CPAN
# $self->composed_of (
# 'App::Cmdline::Options::ExtBasic',
# );
# }
# Use this for validating your options (and remaining arguments).
# sub validate_args {
# my ($self, $opt, $args) = @_;
# $self->SUPER::validate_args ($opt, $args);
# if ($opt->number and scalar @$args != $opt->number) {
# $self->usage_error ("Option --number does not correspond with the number of arguments");
# }
# }
# Use this for changing the first line of the Usage message.
# sub usage_desc {
# my $self = shift;
# return $self->SUPER::usage_desc() . " <some arguments...>";
# }
# Use this to change (App::Cmdline's) default configuration of Getopt::Long
#sub getopt_conf {
# return [ 'bundling' ];
#}
#-----------------------------------------------------------------
# The main job is done here. Mandatory method.
#-----------------------------------------------------------------
t/App/testapp.pm view on Meta::CPAN
# $self->composed_of (
# 'App::Cmdline::Options::ExtBasic',
# );
# }
# Use this for validating your options (and remaining arguments).
# sub validate_args {
# my ($self, $opt, $args) = @_;
# $self->SUPER::validate_args ($opt, $args);
# if ($opt->number and scalar @$args != $opt->number) {
# $self->usage_error ("Option --number does not correspond with the number of arguments");
# }
# }
# Use this for changing the first line of the Usage message.
# sub usage_desc {
# my $self = shift;
# return $self->SUPER::usage_desc() . " <some arguments...>";
# }
# Use this to change (App::Cmdline's) default configuration of Getopt::Long
#sub getopt_conf {
# return [ 'bundling' ];
#}
#-----------------------------------------------------------------
# The main job is done here. Mandatory method.
#-----------------------------------------------------------------
( run in 1.478 second using v1.01-cache-2.11-cpan-49f99fa48dc )