App::Cmd::Tutorial

App::Cmd::Tutorial is a Perl module that will help you get started with App::Cmd.
Download

App::Cmd::Tutorial Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Ricardo Signes
  • Publisher web site:
  • http://search.cpan.org/~rjbs/

App::Cmd::Tutorial Tags


App::Cmd::Tutorial Description

App::Cmd::Tutorial is a Perl module that will help you get started with App::Cmd. App::Cmd::Tutorial is a Perl module that will help you get 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::Cmd; YourApp::Cmd->new->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::Cmd; use base qw(App::Cmd); 1;In fact, you can roll these two together, keeping it all in the script, if you want: #!/usr/bin/perl use strict; use warnings; package YourApp::Cmd; use base qw(App::Cmd); YourApp::Cmd->run; # called on the class, ->run implies ->newWhen 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::Cmd::Command::initialize; use strict; use warnings; use base qw(App::Cmd::Command);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.dbmight result in the following data: $opt = { zero => 1, no_backup => 1, new_seed => 'xyzzy', }; $args = ;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.The first two methods provide configuration passed to GLD's describe_options routine. To improve our command class, we might add the following code: sub usage_desc { "yourcmd %o " } sub opt_spec { return ( } ], ); } sub validate_args { my ($self, $opt, $args) = @_; # we need at least one argument beyond the options die $self->usage->text unless @$args; } Requirements: · Perl


App::Cmd::Tutorial Related Software