Tuesday, August 3, 2010

RJBC presentation on App::Cmd

Writing Modular Commandline Apps with App::Cmd
RJBS looked into writing command line apps and didn't find many options. (Just one?!). What happened to TMTWWTDOI? So he wrote App::Cmd. I've been looking at this for a few weeks and finally got a chance to dig into it today.

I'm happy to see that there is a ::Simple version for writing 'single command' commands, which seems a nice way to get started.

I hope to get my example app written and pushed to cpan soon.

2 comments:

Tommy Stanton said...

Here is a simple code example of App::Cmd::Simple, based on what RJBS quickly wrote down for me at YAPC. This is what happens:

$ my-script --blortex
This is blortex
$ my-script
This is blort

File "my-script" (you might want to perltidy this since the formatting is lost in this blog comment :( ):

#!/usr/bin/env perl
{
package MyProg;
use base 'App::Cmd::Simple';
sub usage_desc { "app-cmd-simple %o" }
sub opt_spec {
return (
[ "blortex|X", "use the blortex algorithm" ],
);
}
sub execute {
my ($self, $opt, $args) = @_;

$opt->{blortex} ? blortex() : blort();
}

sub blortex { print "This is blortex\n"; }
sub blort { print "This is blort\n"; }
}

MyProg->import; # initialize dispatcher
MyProg->run;

Tommy Stanton said...

Another good tutorial is at:
http://advent.rjbs.manxome.org/2009-12-14.html

And source code for the program that generated that tutorial (sweet!) is at:
http://github.com/rjbs/www-adventcalendar