Cool CLI apps with MooseX::App::Cmd
Devin Austin has a nice post up about MooseX::App::Cmd, which I’ve been playing with recently too. Go over and hit his article for the basics; I’m interested in showing you how in addition to MooseX::App::Cmd, you can add in MooseX::Declare to get some super cool stuff:
Note that MooseX::Declare handles the normal unpacking of arguments that would usually be the first line in a Perl subroutine – $self
is just there automagically, and the other two arguments that come in via MooseX::App::Cmd are detected and unpacked because of the way the method is declared in line 14. If the command is invoked without a -n
or –name
option, it will throw an error about a required option being missing, and dump a helpful usage text, like so:
But wait, where are the –configfile
and –file
options coming in from? The parent App::Booklist::CLI::BASE
class, natch. Note that this class is the one actually extending MooseX::App::Cmd::Command, and it’s also using yet another cool Moose extension, MooseX::SimpleConfig, which is where the –configfile
option springs from:
Because of that simple with MooseX::SimpleConfig
line, I get automagic loading and parsing of a config file in a multitude of formats – so if I have a YAML file that looks like this:
That’s sufficient to override the default embedded in the code, and the config itself can be overridden with a simple –file db/nothisfile.db
on the command line when running any command that extends App::Booklist::CLI::BASE
.
MooseX::App::Cmd is the goodness, and MooseX::Declare and MooseX::SimpleConfig are pretty sweet too; the next time you need a CLI app with multiple sub commands you should check them all out…