MooX::Options(3) Explicit Options eXtension for Object Class


version 4.022


Create a command line tool with your Mo, Moo, Moose objects.

Everything is explicit. You have an "option" keyword to replace the usual "has" to explicitly use your attribute into the command line.

The "option" keyword takes additional parameters and uses Getopt::Long::Descriptive to generate a command line tool.


In :

package myOptions;
use Moo;
use MooX::Options;
option 'show_this_file' => (
is => 'ro',
format => 's',
required => 1,
doc => 'the file to display'

In :

  use feature 'say';
  use myOptions;
  use Path::Class;
  my $opt = myOptions->new_with_options;
  say "Content of the file : ",

To use it :

  perl --show_this_file=myFile.txt
  Content of the file: myFile content

The help message :

  perl --help
  USAGE: [-h] [long options...]
      --show_this_file: String
          the file to display
      -h --help:
          show this help message
          show the manual

The usage message :

  perl --usage
  USAGE: [ --show_this_file=String ] [ --usage ] [ --help ] [ --man ]

The manual :

  perl --man



Call Carp::croak dynamically


The list of the methods automatically imported into your class.


It will parse your command line params and your inline params, validate and call the "new" method.

  myTool --str=ko
  t->new_with_options()->str # ko
  t->new_with_options(str => 'ok')->str #ok


The "option" keyword replaces the "has" method and adds support for special options for the command line only.

See ``OPTION PARAMETERS'' for the documentation.

options_usage | --help

It displays the usage message and returns the exit code.

  my $t = t->new_with_options();
  my $exit_code = 1;
  my $pre_message = "str is not valid";
  $t->options_usage($exit_code, $pre_message);

This method is also automatically fired if the command option ``--help'' is passed.

  myTool --help

options_man | --man

It displays the manual.

  my $t = t->new_with_options();

This is automatically fired if the command option ``--man'' is passed.

  myTool --man

options_short_usage | --usage

It displays a short version of the help message.

  my $t = t->new_with_options();

This is automatically fired if the command option ``--usage'' is passed.

  myTool --usage


The list of parameters supported by MooX::Options.


Passes extra arguments for Getopt::Long::Descriptive. It is useful if you want to configure Getopt::Long.

  use MooX::Options flavour => [qw( pass_through )];

Any flavour is passed to Getopt::Long as a configuration, check the doc to see what is possible.


By default, @ARGV is protected. If you want to do something else on it, use this option and it will change the real @ARGV.

  use MooX::Options protect_argv => 0;


If you have Role with options and you want to deactivate some of them, you can use this parameter. In that case, the "option" keyword will just work like an "has".

  use MooX::Options skip_options => [qw/multi/];


By default, arguments passed to "new_with_options" have a higher priority than the command line options.

This parameter will give the command line an higher priority.

  use MooX::Options prefer_commandline => 1;


This parameter will load MooX::ConfigFromFile in your module. The config option will be used between the command line and parameters.

myTool :

  use MooX::Options with_config_from_file => 1;

In /etc/myTool.json

  {"test" : 1}


This parameter is passed to Getopt::Long::Descriptive::describe_options() as the first parameter.

It is a ``sprintf''-like string that is used in generating the first line of the usage message. It's a one-line summary of how the command is to be invoked. The default value is ``USAGE: %c %o''.

%c will be replaced with what Getopt::Long::Descriptive thinks is the program name (it's computed from $0, see ``prog_name'').

%o will be replaced with a list of the short options, as well as the text ``[long options...]'' if any have been defined.

The rest of the usage description can be used to summarize what arguments are expected to follow the program's options, and is entirely free-form.

Literal ``%'' characters will need to be written as ``%%'', just like with ``sprintf''.


This indicate the char to use for spacer. Please only use 1 char otherwize the text will be too long.

The default char is `` ''.

  use MooX::Options space => '+'

Then the ``spacer_before'' and ``spacer_after'' will use it for ``man'' and ``help'' message.

  option 'x' => (is => 'ro', spacer_before => 1, spacer_after => 1);


The keyword "option" extend the keyword "has" with specific parameters for the command line.

doc | documentation

Documentation for the command line option.


Documentation for the man page. By default the "doc" parameter will be used.

See also Man parameters to get more examples how to build a nice man page.


This attribute indicates that the parameter is mandatory. This attribute is not really used by MooX::Options but ensures that consistent error message will be displayed.


Format of the params, same as Getopt::Long::Descriptive.
  • i : integer
  • i@: array of integer
  • s : string
  • s@: array of string
  • f : float value

By default, it's a boolean value.

Take a look of available formats with Getopt::Long::Descriptive.

You need to understand that everything is explicit here. If you use Moose and your attribute has "isa => 'Array[Int]'", that will not imply the format "i@".

format json : special format support

The parameter will be treated like a json string.

  option 'hash' => (is => 'ro', json => 1);

You can also use the json format

  option 'hash' => (is => 'ro', format => "json");
  myTool --hash='{"a":1,"b":2}' # hash = { a => 1, b => 2 }


It adds the negative version for the option.

  option 'verbose' => (is => 'ro', negativable => 1);
  myTool --verbose    # verbose = 1
  myTool --no-verbose # verbose = 0


It appends to the ``format'' the array attribute "@".

I advise to add a default value to your attribute to always have an array. Otherwise the default value will be an undefined value.

  option foo => (is => 'rw', format => 's@', default => sub { [] });
  myTool --foo="abc" --foo="def" # foo = ["abc", "def"]


For repeatable option, you can add the autosplit feature with your specific parameters.

  option test => (is => 'ro', format => 'i@', default => sub {[]}, autosplit => ',');
  myTool --test=1 --test=2 # test = (1, 2)
  myTool --test=1,2,3      # test = (1, 2, 3)

It will also handle quoted params with the autosplit.

  option testStr => (is => 'ro', format => 's@', default => sub {[]}, autosplit => ',');
  myTool --testStr='a,b,"c,d",e,f' # testStr ("a", "b", "c,d", "e", "f")


For another repeatable option you can add the autorange feature with your specific parameters. This allows you to pass number ranges instead of passing each individual number.

  option test => (is => 'ro', format => 'i@', default => sub {[]}, autorange => 1);
  myTool --test=1 --test=2 # test = (1, 2)
  myTool --test=1,2,3      # test = (1, 2, 3)
  myTool --test=1,2,3..6   # test = (1, 2, 3, 4, 5, 6)

It will also handle quoted params like "autosplit", and will not rangify them.

  option testStr => (is => 'ro', format => 's@', default => sub {[]}, autorange => 1);
  myTool --testStr='1,2,"3,a,4",5' # testStr (1, 2, "3,a,4", 5)

"autosplit" will be set to ',' if undefined. You may set "autosplit" to a different delimiter than ',' for your group separation, but the range operator '..' cannot be changed.

  option testStr => (is => 'ro', format => 's@', default => sub {[]}, autorange => 1, autosplit => '-');
  myTool --testStr='1-2-3-5..7' # testStr (1, 2, 3, 5, 6, 7)


Long option can also have short version or aliased.

  option 'verbose' => (is => 'ro', short => 'v');
  myTool --verbose # verbose = 1
  myTool -v        # verbose = 1
  option 'account_id' => (is => 'ro', format => 'i', short => 'a|id');
  myTool --account_id=1
  myTool -a=1
  myTool --id=1

You can also use a shorter option without attribute :

  option 'account_id' => (is => 'ro', format => 'i');
  myTool --acc=1
  myTool --account=1


Specifies the order of the attribute. If you want to push some attributes at the end of the list. By default all options have an order set to 0, and options are sorted by their names.

  option 'at_the_end' => (is => 'ro', order => 999);


Hide option from doc but still an option you can use on command line.

  option 'debug' => (is => 'ro', doc => 'hidden');


  option 'debug' => (is => 'ro', hidden => 1);

spacer_before, spacer_after

Add spacer before or after or both the params

  option 'myoption' => (is => 'ro', spacer_before => 1, spacer_after => 1);


  • Man parameters
  • Using namespace::clean
  • Manage your tools with MooX::Cmd



Translation is now supported.

Use the dzil command to update the pot and merge into the po files.

  • dzil msg-init

    Create a new language po

  • dzil msg-scan

    Scan and generate or update the pot file

  • dzil msg-merge

    Update all languages using the pot file


  • sschober

    For implementation and German translation.


  • Matt S. Trout (mst) <[email protected]>

    For his patience and advice.

  • Tomas Doran (t0m) <[email protected]>

    To help me release the new version, and using it :)

  • Torsten Raudssus (Getty)

    to use it a lot in DuckDuckGo <> (go to see MooX module also)

  • Jens Rehsack (REHSACK)

    Use with PkgSrc <>, and many really good idea (MooX::Cmd, MooX::ConfigFromFile, and more to come I'm sure)

  • All contributors

    For improving and add more feature to MooX::Options


Please report any bugs or feature requests on the bugtracker website

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.


celogeek <[email protected]>


This software is copyright (c) 2013 by celogeek <[email protected]>.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.