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

In reply to Re: Can anybody tell me how MooX::Cmd is supposed to work? by tobyink
in thread Can anybody tell me how MooX::Cmd is supposed to work? by randian

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.