#!/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; #### $ 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 #### $ git --someopt --anotheropt #### $ git subcommand --someopt --anotheropt