App::Cmd::Tutorial - getting started with App::Cmd
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 -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;
In fact, you can roll these two together, keeping it all in the script, if you want:
#!/usr/bin/perl package YourApp; use App::Cmd::Setup -app; YourApp->run;
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::Cmd will look for any module with a name starting with YourApp::Cmd::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::Cmd::Command::initialize does not implement mandatory method 'run'
Oops! This dies because we haven't told the command class what it should do when run. This is easy, we just add some code:
sub run { 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 options passed from the command line and the leftover 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 Getopt::Long::Descriptive. To customize its argument procession, a command class can implement a few methods: usage_desc provides the usage format string; opt_spec provides the option specification list; validate_args is run after Getopt::Long::Descriptive.
usage_desc
opt_spec
validate_args
The first two methods provide configuration passed to GLD's describe_options routine. To improve our command class, we might add the following code:
describe_options
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 $self->usage->text unless @$args; }
Delay using large modules using autouse, Class::Autouse or 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.
require
To add a --help option to all your commands create a base class like:
--help
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 options and validate are "inner" methods which your command subclasses implement.
options
validate
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 exmaple).
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.
To install App::Cmd, copy and paste the appropriate command in to your terminal.
cpanm
cpanm App::Cmd
CPAN shell
perl -MCPAN -e shell install App::Cmd
For more information on module installation, please visit the detailed CPAN module installation guide.