Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Can anybody tell me how MooX::Cmd is supposed to work?

by tobyink (Canon)
on Mar 14, 2013 at 09:23 UTC ( [id://1023430]=note: print w/replies, xml ) Need Help??


in reply to Can anybody tell me how MooX::Cmd is supposed to work?

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

Replies are listed 'Best First'.
Re^2: Can anybody tell me how MooX::Cmd is supposed to work?
by randian (Acolyte) on Mar 15, 2013 at 01:32 UTC
    Thanks for the example, it was very helpful.
      I have update MooX::Options to support MooX::Cmd The help message include the sub command ! Enjoy :) Celogeek

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1023430]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-25 15:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found