1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
=head1 NAME
App::Cmd::Tutorial - getting started with App::Cmd
=head1 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.
An App::Cmd-based application is made up of three main parts: the script, the
application class, and the command classes.
The script is the actual executable file run at the command line. It can
generally consist of just a few lines:
#!/usr/bin/perl
use YourApp;
YourApp->run;
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:
package YourApp;
use App::Cmd::Setup -app;
1;
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 C<YourApp::Command::>.
We can set up a simple command class like this:
package YourApp::Command::initialize;
use YourApp -command;
1;
Now, a user can run this command, but he'll get an error:
$ yourcmd initialize
YourApp::Command::initialize does not implement mandatory method 'execute'
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:
sub execute {
my ($self, $opt, $args) = @_;
print "Everything has been initialized. (Not really.)\n";
}
Now it works:
$ yourcmd initialize
Everything has been initialized. (Not really.)
The arguments to the run method are the parsed options from the command line
(that is, the switches) and the remaining arguments. With a properly
configured command class, the following invocation:
$ yourcmd reset -zB --new-seed xyzxy foo.db bar.db
might result in the following data:
$opt = {
zero => 1,
no_backup => 1,
new_seed => 'xyzzy',
};
$args = [ qw(foo.db bar.db) ];
Arguments are processed by L<Getopt::Long::Descriptive> (GLD). To customize
its argument processing, a command class can implement a few methods:
C<usage_desc> provides the usage format string; C<opt_spec> provides the option
specification list; C<validate_args> is run after Getopt::Long::Descriptive,
and is meant to validate the C<$args>, which GLD ignores.
The first two methods provide configuration passed to GLD's C<describe_options> routine. To improve our command class, we might add the following code:
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;
}
=head1 TIPS
=over 4
=item *
Delay using large modules using L<autouse>, L<Class::Autouse> or C<require> 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.
=item *
To add a C<--help> option to all your commands create a base class like:
package MyApp::Command;
use App::Cmd::Setup -command;
sub opt_spec {
my ( $class, $app ) = @_;
return (
[ 'help' => "This usage screen" ],
$class->options($app),
)
}
sub validate_args {
my ( $self, $opt, $args ) = @_;
die $self->_usage_text if $opt->{help};
$self->validate( $opt, $args );
}
Where C<options> and C<validate> are "inner" methods which your command
subclasses implement.
=item *
To let your users configure default values for options, put a sub like
sub config {
my $app = shift;
$app->{config} ||= TheLovelyConfigModule->load_config_file();
}
in your main app file, and then do something like:
sub opt_spec {
my ( $class, $app ) = @_;
my ( $name ) = $class->command_names;
return (
[ 'blort=s' => "That special option",
{ default => $app->config->{$name}{blort} || $fallback_default },
],
);
}
Or better yet, put this logic in a superclass and process the return value from
an "inner" method (see previous tip for an example).
=back
=head1 AUTHOR AND COPYRIGHT
Copyright 2005-2006, (code (simply)). All rights reserved; App::Cmd and
bundled code are free software, released under the same terms as perl itself.
App:Cmd was originally written as Rubric::CLI by Ricardo SIGNES in 2005. It
was refactored extensively by Ricardo SIGNES and John Cappiello and released as
App::Cmd in 2006.
|