1App::Cmd::Tutorial(3) User Contributed Perl DocumentationApp::Cmd::Tutorial(3)
2
3
4

NAME

6       App::Cmd::Tutorial - getting started with App::Cmd
7

VERSION

9       version 0.311
10

DESCRIPTION

12       App::Cmd is a set of tools designed to make it simple to write
13       sophisticated command line programs.  It handles commands with multiple
14       subcommands, generates usage text, validates options, and lets you
15       write your program as easy-to-test classes.
16
17       An App::Cmd-based application is made up of three main parts:  the
18       script, the application class, and the command classes.
19
20       The script is the actual executable file run at the command line.  It
21       can generally consist of just a few lines:
22
23         #!/usr/bin/perl
24         use YourApp;
25         YourApp->run;
26
27       All the work of argument parsing, validation, and dispatch is taken
28       care of by your application class.  The application class can also be
29       pretty simple, and might look like this:
30
31         package YourApp;
32         use App::Cmd::Setup -app;
33         1;
34
35       When a new application instance is created, it loads all of the command
36       classes it can find, looking for modules under the Command namespace
37       under its own name.  In the above snippet, for example, YourApp will
38       look for any module with a name starting with "YourApp::Command::".
39
40       We can set up a simple command class like this:
41
42         package YourApp::Command::initialize;
43         use YourApp -command;
44         1;
45
46       Now, a user can run this command, but he'll get an error:
47
48         $ yourcmd initialize
49         YourApp::Command::initialize does not implement mandatory method 'execute'
50
51       Oops!  This dies because we haven't told the command class what it
52       should do when executed.  This is easy, we just add some code:
53
54         sub execute {
55           my ($self, $opt, $args) = @_;
56
57           print "Everything has been initialized.  (Not really.)\n";
58         }
59
60       Now it works:
61
62         $ yourcmd initialize
63         Everything has been initialized.  (Not really.)
64
65       The arguments to the execute method are the parsed options from the
66       command line (that is, the switches) and the remaining arguments.  With
67       a properly configured command class, the following invocation:
68
69         $ yourcmd reset -zB --new-seed xyzxy foo.db bar.db
70
71       might result in the following data:
72
73         $opt = {
74           zero      => 1,
75           no_backup => 1,
76           new_seed  => 'xyzzy',
77         };
78
79         $args = [ qw(foo.db bar.db) ];
80
81       Arguments are processed by Getopt::Long::Descriptive (GLD).  To
82       customize its argument processing, a command class can implement a few
83       methods: "usage_desc" provides the usage format string; "opt_spec"
84       provides the option specification list; "validate_args" is run after
85       Getopt::Long::Descriptive, and is meant to validate the $args, which
86       GLD ignores.
87
88       The first two methods provide configuration passed to GLD's
89       "describe_options" routine.  To improve our command class, we might add
90       the following code:
91
92         sub usage_desc { "yourcmd %o [dbfile ...]" }
93
94         sub opt_spec {
95           return (
96             [ "skip-refs|R",  "skip reference checks during init", ],
97             [ "values|v=s@",  "starting values", { default => [ 0, 1, 3 ] } ],
98           );
99         }
100
101         sub validate_args {
102           my ($self, $opt, $args) = @_;
103
104           # we need at least one argument beyond the options; die with that message
105           # and the complete "usage" text describing switches, etc
106           $self->usage_error("too few arguments") unless @$args;
107         }
108

TIPS

110       ·   Delay using large modules using autouse, Class::Autouse or
111           "require" in your commands to save memory and make startup faster.
112           Since only one of these commands will be run anyway, there's no
113           need to preload the requirements for all of them.
114
115       ·   To add a "--help" option to all your commands create a base class
116           like:
117
118             package MyApp::Command;
119             use App::Cmd::Setup -command;
120
121             sub opt_spec {
122               my ( $class, $app ) = @_;
123               return (
124                 [ 'help' => "This usage screen" ],
125                 $class->options($app),
126               )
127             }
128
129             sub validate_args {
130               my ( $self, $opt, $args ) = @_;
131               die $self->_usage_text if $opt->{help};
132               $self->validate( $opt, $args );
133             }
134
135           Where "options" and "validate" are "inner" methods which your
136           command subclasses implement.
137
138       ·   To let your users configure default values for options, put a sub
139           like
140
141             sub config {
142               my $app = shift;
143               $app->{config} ||= TheLovelyConfigModule->load_config_file();
144             }
145
146           in your main app file, and then do something like:
147
148             sub opt_spec {
149               my ( $class, $app ) = @_;
150               my ( $name ) = $class->command_names;
151               return (
152                 [ 'blort=s' => "That special option",
153                   { default => $app->config->{$name}{blort} || $fallback_default },
154                 ],
155               );
156             }
157
158           Or better yet, put this logic in a superclass and process the
159           return value from an "inner" method (see previous tip for an
160           example).
161

AUTHOR

163       Ricardo Signes <rjbs@cpan.org>
164
166       This software is copyright (c) 2011 by Ricardo Signes.
167
168       This is free software; you can redistribute it and/or modify it under
169       the same terms as the Perl 5 programming language system itself.
170
171
172
173perl v5.12.3                      2011-03-18             App::Cmd::Tutorial(3)
Impressum