403Webshell
Server IP : 216.92.14.13  /  Your IP : 216.73.216.171
Web Server : Apache
System : Linux vps4089.pairvps.com 5.15.0-190-generic #200-Ubuntu SMP Fri Aug 7 15:06:04 UTC 2026 x86_64
User : rmlac2fmr ( 1040637)
PHP Version : 8.2.32
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON  |  Sudo : ON  |  Pkexec : ON
Directory :  /usr/local/man/man3/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /usr/local/man/man3/App::Cmd::Tutorial.3
.\" Automatically generated by Pod::Man 2.28 (Pod::Simple 3.29)
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" Set up some character translations and predefined strings.  \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote.  \*(C+ will
.\" give a nicer C++.  Capital omega is used to do unbreakable dashes and
.\" therefore won't be available.  \*(C` and \*(C' expand to `' in nroff,
.\" nothing in troff, for use with C<>.
.tr \(*W-
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
.    ds -- \(*W-
.    ds PI pi
.    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
.    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
.    ds L" ""
.    ds R" ""
.    ds C` ""
.    ds C' ""
'br\}
.el\{\
.    ds -- \|\(em\|
.    ds PI \(*p
.    ds L" ``
.    ds R" ''
.    ds C`
.    ds C'
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el       .ds Aq '
.\"
.\" If the F register is turned on, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD.  Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.\"
.\" Avoid warning from groff about undefined register 'F'.
.de IX
..
.nr rF 0
.if \n(.g .if rF .nr rF 1
.if (\n(rF:(\n(.g==0)) \{
.    if \nF \{
.        de IX
.        tm Index:\\$1\t\\n%\t"\\$2"
..
.        if !\nF==2 \{
.            nr % 0
.            nr F 2
.        \}
.    \}
.\}
.rr rF
.\" ========================================================================
.\"
.IX Title "App::Cmd::Tutorial 3"
.TH App::Cmd::Tutorial 3 "2015-02-23" "perl v5.22.0" "User Contributed Perl Documentation"
.\" For nroff, turn off justification.  Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH "NAME"
App::Cmd::Tutorial \- getting started with App::Cmd
.SH "VERSION"
.IX Header "VERSION"
version 0.327
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
App::Cmd is a set of tools designed to make it simple to write sophisticated
command line programs.  It handles commands with multiple subcommands,
generates usage text, validates options, and lets you write your program as
easy-to-test classes.
.PP
An App::Cmd\-based application is made up of three main parts:  the script,
the application class, and the command classes.
.SS "The Script"
.IX Subsection "The Script"
The script is the actual executable file run at the command line.  It can
generally consist of just a few lines:
.PP
.Vb 3
\&  #!/usr/bin/perl
\&  use YourApp;
\&  YourApp\->run;
.Ve
.SS "The Application Class"
.IX Subsection "The Application Class"
All the work of argument parsing, validation, and dispatch is taken care of by
your application class.  The application class can also be pretty simple, and
might look like this:
.PP
.Vb 3
\&  package YourApp;
\&  use App::Cmd::Setup \-app;
\&  1;
.Ve
.PP
When a new application instance is created, it loads all of the command classes
it can find, looking for modules under the Command namespace under its own
name.  In the above snippet, for example, YourApp will look for any module with
a name starting with \f(CW\*(C`YourApp::Command::\*(C'\fR.
.SS "The Command Classes"
.IX Subsection "The Command Classes"
We can set up a simple command class like this:
.PP
.Vb 4
\&  # ABSTRACT: set up YourApp
\&  package YourApp::Command::initialize;
\&  use YourApp \-command;
\&  1;
.Ve
.PP
Now, a user can run this command, but he'll get an error:
.PP
.Vb 2
\&  $ yourcmd initialize
\&  YourApp::Command::initialize does not implement mandatory method \*(Aqexecute\*(Aq
.Ve
.PP
Oops!  This dies because we haven't told the command class what it should do
when executed.  This is easy, we just add some code:
.PP
.Vb 2
\&  sub execute {
\&    my ($self, $opt, $args) = @_;
\&
\&    print "Everything has been initialized.  (Not really.)\en";
\&  }
.Ve
.PP
Now it works:
.PP
.Vb 2
\&  $ yourcmd initialize
\&  Everything has been initialized.  (Not really.)
.Ve
.SS "Default Commands"
.IX Subsection "Default Commands"
By default applications made with App::Cmd know two commands: \f(CW\*(C`commands\*(C'\fR and
\&\f(CW\*(C`help\*(C'\fR.
.IP "commands" 4
.IX Item "commands"
lists available commands.
.Sp
.Vb 2
\&  $yourcmd commands
\&  Available commands:
\&
\&    commands: list the application\*(Aqs commands
\&        help: display a command\*(Aqs help screen
\&
\&        init: set up YourApp
.Ve
.Sp
Note that by default the commands receive a description from the \f(CW\*(C`# ABSTRACT\*(C'\fR
comment in the respective command's module, or from the \f(CW\*(C`=head1 NAME\*(C'\fR Pod
section.
.IP "help" 4
.IX Item "help"
allows one to query for details on command's specifics.
.Sp
.Vb 2
\&  $yourcmd help initialize
\&   yourcmd initialize [\-z] [long options...]
\&
\&          \-z \-\-zero        ignore zeros
.Ve
.Sp
Of course, it's possible to disable or change the default commands, see
App::Cmd.
.SS "Arguments and Options"
.IX Subsection "Arguments and Options"
In this example
.PP
.Vb 1
\&  $ yourcmd reset \-zB \-\-new\-seed xyzxy foo.db bar.db
.Ve
.PP
\&\f(CW\*(C`\-zB\*(C'\fR and \f(CW\*(C`\-\-new\-seed xyzxy\*(C'\fR are \*(L"options\*(R" and \f(CW\*(C`foo.db\*(C'\fR and \f(CW\*(C`bar.db\*(C'\fR
are \*(L"arguments.\*(R"
.PP
With a properly configured command class, the above invocation results in
nicely formatted data:
.PP
.Vb 5
\&  $opt = {
\&    zero      => 1,
\&    no_backup => 1, #default value
\&    new_seed  => \*(Aqxyzzy\*(Aq,
\&  };
\&
\&  $args = [ qw(foo.db bar.db) ];
.Ve
.PP
Arguments are processed by Getopt::Long::Descriptive (\s-1GLD\s0).  To customize
its argument processing, a command class can implement a few methods:
\&\f(CW\*(C`usage_desc\*(C'\fR provides the usage format string; \f(CW\*(C`opt_spec\*(C'\fR provides the option
specification list; \f(CW\*(C`validate_args\*(C'\fR is run after Getopt::Long::Descriptive,
and is meant to validate the \f(CW$args\fR, which \s-1GLD\s0 ignores. See Getopt::Long
for format specifications.
.PP
The first two methods provide configuration passed to \s-1GLD\s0's \f(CW\*(C`describe_options\*(C'\fR
routine.  To improve our command class, we might add the following code:
.PP
.Vb 1
\&  sub usage_desc { "yourcmd %o [dbfile ...]" }
\&
\&  sub opt_spec {
\&    return (
\&      [ "skip\-refs|R",  "skip reference checks during init", ],
\&      [ "values|v=s@",  "starting values", { default => [ 0, 1, 3 ] } ],
\&    );
\&  }
\&
\&  sub validate_args {
\&    my ($self, $opt, $args) = @_;
\&
\&    # we need at least one argument beyond the options; die with that message
\&    # and the complete "usage" text describing switches, etc
\&    $self\->usage_error("too few arguments") unless @$args;
\&  }
.Ve
.SS "Global Options"
.IX Subsection "Global Options"
There are several ways of making options available everywhere (globally). This
recipe makes local options accessible in all commands.
.PP
To add a \f(CW\*(C`\-\-help\*(C'\fR option to all your commands create a base class like:
.PP
.Vb 2
\&  package MyApp::Command;
\&  use App::Cmd::Setup \-command;
\&
\&  sub opt_spec {
\&    my ( $class, $app ) = @_;
\&    return (
\&      [ \*(Aqhelp\*(Aq => "this usage screen" ],
\&      $class\->options($app),
\&    )
\&  }
\&
\&  sub validate_args {
\&    my ( $self, $opt, $args ) = @_;
\&    if ( $opt\->{help} ) {
\&      my ($command) = $self\->command_names;
\&      $self\->app\->execute_command(
\&        $self\->app\->prepare_command("help", $command)
\&      );
\&      exit;
\&    }
\&    $self\->validate( $opt, $args );
\&  }
.Ve
.PP
Where \f(CW\*(C`options\*(C'\fR and \f(CW\*(C`validate\*(C'\fR are \*(L"inner\*(R" methods which your command
subclasses implement to provide command-specific options and validation.
.PP
Note: this is a new file, previously not mentioned in this tutorial and this
tip does not recommend the use of global_opt_spec which offers an alternative
way of specifying global options.
.SH "TIPS"
.IX Header "TIPS"
.IP "\(bu" 4
Delay using large modules using Class::Load, Module::Runtime or \f(CW\*(C`require\*(C'\fR in
your commands to save memory and make startup faster. Since only one of these
commands will be run anyway, there's no need to preload the requirements for
all of them.
.IP "\(bu" 4
Add a \f(CW\*(C`description\*(C'\fR method to your commands for more verbose output
from the built-in help command.
.Sp
.Vb 3
\&  sub description {
\&    return "The initialize command prepares ...";
\&  }
.Ve
.IP "\(bu" 4
To let your users configure default values for options, put a sub like
.Sp
.Vb 4
\&  sub config {
\&    my $app = shift;
\&    $app\->{config} ||= TheLovelyConfigModule\->load_config_file();
\&  }
.Ve
.Sp
in your main app file, and then do something like:
.Sp
.Vb 10
\&  package YourApp;
\&  sub opt_spec {
\&    my ( $class, $app ) = @_;
\&    my ( $name ) = $class\->command_names;
\&    return (
\&      [ \*(Aqblort=s\*(Aq => "That special option",
\&        { default => $app\->config\->{$name}{blort} || $fallback_default },
\&      ],
\&    );
\&  }
.Ve
.Sp
Or better yet, put this logic in a superclass and process the return value from
an \*(L"inner\*(R" method:
.Sp
.Vb 8
\&  package YourApp::Command;
\&  sub opt_spec {
\&    my ( $class, $app ) = @_;
\&    return (
\&      [ \*(Aqhelp\*(Aq => "this usage screen" ],
\&      $class\->options($app),
\&    )
\&  }
.Ve
.IP "\(bu" 4
You need to activate \f(CW\*(C`strict\*(C'\fR and \f(CW\*(C`warnings\*(C'\fR as usual if you want them.
App::Cmd doesn't do that for you.
.SH "IGNORING THINGS"
.IX Header "IGNORING THINGS"
Some people find that for whatever reason, they wish to put Modules in their
\&\f(CW\*(C`MyApp::Command::\*(C'\fR namespace which are not commands, or not commands intended
for use by \f(CW\*(C`MyApp\*(C'\fR.
.PP
Good examples include, but are not limited to, things like
\&\f(CW\*(C`MyApp::Command::frobrinate::Plugin::Quietly\*(C'\fR, where \f(CW\*(C`::Quietly\*(C'\fR is only
useful for the \f(CW\*(C`frobrinate\*(C'\fR command.
.PP
The default behaviour is to treat such packages as errors, as for the majority
of use cases, things in \f(CW\*(C`::Command\*(C'\fR are expected to \fIonly\fR be commands, and
thus, anything that, by our heuristics, is not a command, is highly likely to be
a mistake.
.PP
And as all commands are loaded simultaneously, an error in any one of these
commands will yield a fatal error.
.PP
There are a few ways to specify that you are sure you want to do this, with
varying ranges of scope and complexity.
.SS "Ignoring a Single Module."
.IX Subsection "Ignoring a Single Module."
This is the simplest approach, and most useful for one-offs.
.PP
.Vb 1
\&  package YourApp::Command::foo::NotACommand;
\&
\&  use YourApp \-ignore;
\&
\&  <whatever you want here>
.Ve
.PP
This will register this package's namespace with YourApp to be excluded from
its plugin validation magic. It otherwise makes no changes to
\&\f(CW\*(C`::NotACommand\*(C'\fR's namespace, does nothing magical with \f(CW@ISA\fR, and doesn't
bolt any hidden functions on.
.PP
Its also probably good to notice that it is ignored \fIonly\fR by
\&\f(CW\*(C`YourApp\*(C'\fR. If for whatever reason you have two different \f(CW\*(C`App::Cmd\*(C'\fR systems
under which \f(CW\*(C`::NotACommand\*(C'\fR is visible, you'll need to set it ignored to both.
.PP
This is probably a big big warning \fB\s-1NOT\s0\fR to do that.
.SS "Ignoring Multiple modules from the App level."
.IX Subsection "Ignoring Multiple modules from the App level."
If you really fancy it, you can override the \f(CW\*(C`should_ignore\*(C'\fR method provided by
\&\f(CW\*(C`App::Cmd\*(C'\fR to tweak its ignore logic. The most useful example of this is as
follows:
.PP
.Vb 5
\&  sub should_ignore {
\&    my ( $self, $command_class ) = @_;
\&    return 1 if not $command_class\->isa( \*(AqApp::Cmd::Command\*(Aq );
\&    return;
\&  }
.Ve
.PP
This will prematurely mark for ignoring all packages that don't subclass
\&\f(CW\*(C`App::Cmd::Command\*(C'\fR, which causes non-commands ( or perhaps commands that are
coded wrongly / broken ) to be silently skipped.
.PP
Note that by overriding this method, you will lose the effect of any of the
other ignore mechanisms completely. If you want to combine the original
\&\f(CW\*(C`should_ignore\*(C'\fR method with your own logic, you'll want to steal \f(CW\*(C`Moose\*(C'\fR's
\&\f(CW\*(C`around\*(C'\fR method modifier.
.PP
.Vb 1
\&  use Moose::Util;
\&
\&  Moose::Util::add_method_modifier( _\|_PACKAGE_\|_, \*(Aqaround\*(Aq, [
\&    should_ignore => sub {
\&      my $orig = shift;
\&      my $self = shift;
\&      return 1 if not $command_class\->isa( \*(AqApp::Cmd::Command\*(Aq );
\&      return $self\->$orig( @_ );
\&  }]);
.Ve
.SH "SEE ALSO"
.IX Header "SEE ALSO"
\&\s-1CPAN\s0 modules using App::Cmd <http://deps.cpantesters.org/depended-on-by.pl?module=App%3A%3ACmd>
.SH "AUTHOR"
.IX Header "AUTHOR"
Ricardo Signes <rjbs@cpan.org>
.SH "COPYRIGHT AND LICENSE"
.IX Header "COPYRIGHT AND LICENSE"
This software is copyright (c) 2015 by Ricardo Signes.
.PP
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

Youez - 2016 - github.com/yon3zu
LinuXploit