App::CLI::Toolkit - a helper module for generating command-line utilities
App::CLI::Toolkit is designed to take the hassle out of writing command-line apps in Perl. It handles the parsing of both parameters and options (see below for the distinction) and generates usage information from the details you give it.
use App::CLI::Toolkit; my $app = App::CLI::Toolkit->new( description = 'A replacement for cp', params = [ qw(source dest) ], options = { 'recursive|r' => 'If source is a directory, copies' . 'the directory contents recursively', 'force|f' => 'If target already exists, overwrite it' 'verbose|v' => 'Produce verbose output' } ); print "Copying " . $app->source . " to " . $app->dest . "\n" if $app->verbose; if ($app->recursive) { # Do recursive gumbo } if ($app->force) { # Don't take no for an answer } ...
App::CLI::Toolkit->new(%config)
Constructs a new App::CLI::Toolkit object
A description of what the app does. Used in the usage string that App::CLI::Toolkit generates.
Example:
$app = new App::CLI::Toolkit(description => 'A cool new replacement for grep!')
App::CLI::Toolkit automatically gives your app help options (-h and --help). Supply a noautohelp value that equates to true (e.g. 1) to suppress this.
A reference to a hash mapping option names to a description of what the option does. The hash keys follow the conventions of Getopt::Long.
A reference to an array of parameter names. When the app is invoked, parameters follow the app name on the command line.
$app = new App::CLI::Toolkit(params => ['name']) print uc $app->name
Yields this result:
$ my-app fred FRED
Parameters can be optional, in which case your application will provide a default if the user doesn't provide a parameter. For example, the target argument to ln is optional and defaults to the filename of the source in the current working directory.
ln
Specify an optional argument in App::CLI::Toolkit by adding ? to the end of the parameter name.
App::CLI::Toolkit
?
$app = new App::CLI::Toolkit(params => ['target?']); print $app->target || $ENV{PWD} . "\n"
$ my-app /var/tmp /var/tmp $ my-app /home/simon
Applications can take one or more instances of a particular parameter. For example, mv takes one or more file arguments followed by a single target parameter.
mv
Specify a multiple-value argument in App::CLI::Toolkit by adding + to the end of the parameter name.
+
The accessor for multiple-value parameters returns a list, even if the user only supplied one value.
$app = new App::CLI::Toolkit(params => ['files+']); print join(', ', $app->files) . "\n"
$ my-app foo bar wibble foo, bar, wibble
Applications can take zero or more instances of a particular parameter. For example, ls takes either no parameters (in which case it lists the contents of the current working directory) or a list of parameters (in which case it lists the contents of each parameter).
ls
Specify an optional, multiple-value argument in App::CLI::Toolkit by adding * to the end of the parameter name.
*
The accessor for optional, multiple-value parameters returns a list, even if the user only supplied one value.
$app = new App::CLI::Toolkit(params => ['dirs*']); if ($app->dirs) { print join(', ', $app->dirs) . "\n"; } else { print "No dirs given, using $ENV{PWD}\n"; }
$ my-app foo bar wibble foo, bar, wibble $ my-app foo foo $ my-app No dirs given, using /home/simon
You can only have one multiple-value parameter type per application.
You can't follow an optional parameter type with a non-optional parameter type.
Your App::CLI::Toolkit object has methods named after each of the params and options specified in the constructor.
$app = App::CLI::Toolkit( params => [ qw(foo bar?) ], options => { 'verbose|v' => 'Give more verbose output', } ) print $app->foo; print $app->bar if $app->bar; print "Some verbose rubbish\n" if $app->verbose;
Where an option has multiple labels (eg. verbose and v in the above example), the accessor has the name of the first label in the list.
verbose
v
Gets the value stored against key, where key is an option name or param label. This is an alternative to the convenience accessors named after the option name or param label.
$app = App::CLI::Toolkit(params => ['foo']) print $app->foo; # prints the value of the 'foo' param print $app->get('foo') # same
Gets the usage string for your application
Simon Whitaker, <swhitaker at cpan.org>
<swhitaker at cpan.org>
Please report any bugs or feature requests to bug-app-cli-toolkit at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=App-CLI-Toolkit. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-app-cli-toolkit at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc App::CLI::Toolkit
You can also look for information at:
RT: CPAN's request tracker
http://rt.cpan.org/NoAuth/Bugs.html?Dist=App-CLI-Toolkit
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/App-CLI-Toolkit
CPAN Ratings
http://cpanratings.perl.org/d/App-CLI-Toolkit
Search CPAN
http://search.cpan.org/dist/App-CLI-Toolkit
Thanks to Chris Lokotsch for the code reviews.
Copyright 2008 Simon Whitaker, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install App::CLI::Toolkit, copy and paste the appropriate command in to your terminal.
cpanm
cpanm App::CLI::Toolkit
CPAN shell
perl -MCPAN -e shell install App::CLI::Toolkit
For more information on module installation, please visit the detailed CPAN module installation guide.