The documentation is pretty sparse, isn't it?
Here's an example:
#!/usr/bin/env perl
use 5.010;
use strict;
use warnings;
{
package Calc::Role::BinaryOperation;
use Moo::Role;
use MooX::Options;
option a => (
is => 'ro',
required => 1,
format => 's',
documentation => 'first number',
);
option b => (
is => 'ro',
required => 1,
format => 's',
documentation => 'second number',
);
}
{
package Calc::Cmd::add;
use Moo;
use MooX::Cmd;
use MooX::Options;
with 'Calc::Role::BinaryOperation';
sub execute {
my $self = shift;
say($self->a + $self->b);
exit(0);
}
}
{
package Calc::Cmd::subtract;
use Moo;
use MooX::Cmd;
use MooX::Options;
with 'Calc::Role::BinaryOperation';
sub execute {
my $self = shift;
say($self->a - $self->b);
exit(0);
}
}
{
package Calc;
use Moo;
use MooX::Cmd;
sub execute {
my $self = shift;
my ($args, $chain) = @_;
die "Need to specify a sub-command!\n";
}
}
Calc->new_with_cmd->execute;
If you save that as calc.pl you can run it like this:
$ perl calc.pl
Need to specify a sub-command!
$ echo $?
255
$ perl calc.pl add --help
USAGE: calc.pl [-abh] [long options...]
-a first number
-b second number
-h --help show this help message
$ perl calc.pl subtract --a 9 --b 5
4
$ perl calc.pl add --a 9 --b 5
14
$ echo $?
0
MooX::Cmd doesn't really "do" much. It's really just some glue allowing you to create commands that have subcommands. If you don't know what I mean by subcommands, git is an example. (Pretty much all command-line tools for version control systems are.) You don't type:
$ git --someopt --anotheropt
Instead you need to provide a subcommand:
$ git subcommand --someopt --anotheropt
If you don't need subcommands, then don't use MooX::Cmd; just use MooX::Options (which does the @ARGV handling).
And frankly, if you do need subcommands, I'd rather go with App::Cmd which is IMHO a better, more integrated approach to writing command line apps. My general technique for command-line apps in fact is to use Moo or Moose classes and roles for the "business logic", and then use App::Cmd for the command-line glue. On-demand run-time loading of the classes and roles can result in a pretty snappy command-line interface, even if using the slow old Moose!
package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
|