The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

MooX::Options - add option keywords to your object (Mo/Moo/Moose)

VERSION

version 3.90

MooX::Options

Use Getopt::Long::Descritive to provide command line option for your Mo/Moo/Moose Object.

This module will add "option" which act as "has" but support additional feature for getopt.

You will have "new_with_options" to instanciate new object for command line.

METHOD

IMPORT

The import method can take option :

USAGE

First of all, I use Getopt::Long::Descriptive. Everything will be pass to the programs, more specially the format.

{
    package t;
    use Moo;
    use MooX::Options;

    option 'test' => (is => 'ro');

    1;
}

my $t = t->new_with_options(); #parse @ARGV
my $o = t->new_with_options(test => 'override'); #parse ARGV and override any value with the params here

The keyword "option" work exactly like the keyword "has" and take extra argument of Getopt.

You can also use it over a Role.

{
    package tRole;
    use Moo::Role;
    use MooX::Options;

    option 'test' => (is => 'ro');

    1;
}

{
    package t;
    use Moo;
    use MooX::Options; #you have to add this, or the role will not find the necessary methods
    with 'tRole';
    1;
}

my $t = t->new_with_options(); #parse @ARGV
my $o = t->new_with_options(test => 'override'); #parse ARGV and override any value with the params here

If you use Mo, you have a little bit more work to do. Because Mo lack of "with" and "around".

{
    package tRole;
    use Moo::Role;
    use Mo;
    use MooX::Options;

    option 'test' => (is => 'ro');
    1;
}
{

    package t;
    use Mo;
    use Role::Tiny::With;
    with 'tRole';

    1;
}
my $t = t->new_with_options(); #parse @ARGV
my $o = t->new_with_options(test => 'override'); #parse ARGV and override any value with the params here

It's a bit tricky but, hey, you are using Mo !

Keyword 'options_usage'

It display the usage message and return the exit code

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

Params :

Keyword 'new_with_options'

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

You can override the command line params :

Ex:

local @ARGV=('--str=ko');
t->new_with_options(str => 'ok');
t->str; #ok

Keyword 'option' : EXTRA ARGS

namespace::clean

To use namespace::clean you need to add 2 methods as an exception. It is use by MooX::Options when you run the new_with_options methods.

{
    package t;
    use Moo;
    use MooX::Options;
    use namespace::clean -except => [qw/_options_data _options_config/];
    option 'v' => (is => 'rw');
    1;
}
my $r = t->new_with_options;

dash support

You can call the option with underscore or dash in the name.

For example, --start-date or --start_date will fill the option 'start_date'.

no more Mouse support

If you are using Mouse, I'm sorry to say than the rewrite of this module has make it just incompatible. Mouse is not design to by compatible with anything else than Mouse itself. I could just suggest to use Moo instead, which is a great and compatible replacement.

More examples

http://perltalks.celogeek.com/slides/2012/08/moox-options-slide3d.html

THANKS

BUGS

Please report any bugs or feature requests on the bugtracker website https://tasks.celogeek.com/projects/perl-modules-moox-options

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.

AUTHOR

celogeek me@celogeek.com

COPYRIGHT AND LICENSE

This software is copyright (c) 2011 by celogeek me@celogeek.com.

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