The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

MooX::Option - add option keywords to your Moo object

VERSION

version 0.1

SYNOPSIS

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

        option "str" => (
            is => 'ro',
            required => '1',
            doc => "My String Option",
            format => 'Bool',
        );
        1;
    }

    my $opt = t->new_with_options();

MooX::Option

Use Getopt::Long::Descritive to provide command line option for your Mo/Moo/Mouse/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 :

%options

creation_method : call this method after parsing option, default : new

chain_method : call this method to create the attribute, default : has

option_method_name : name of keyword you want to use to create your option, default : option

creation_method_name : name of new method to handle option, default : new_with_options

filter :

by default all params is passed to the chain_method, but you can have warning with some method, you can use the filter I have set to remove some params to the chain method

available filter : Mo, Moo, Mouse, Moose

Example
    use MooX::Option creation_method => 'my_new_method', chain_method => 'my_chain_method';

Filter for specific object model : (Mo and Moo don t need any filter, you can obmit the params) {package pmo; use Mo; use MooX::Option filter => 'Mo'}; {package pmoo; use Moo; use MooX::Option filter => 'Moo'}; {package pmouse; use Mouse; use MooX::Option filter => 'Mouse'}; {package pmoose; use Moose; use MooX::Option filter => 'Moose'};

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::Option;
    
    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.

EXTRA ARGS

doc

Specified the documentation for the attribute

required

Specified if the attribute is needed

format

Format of the params. It is the same as Getopt::Long::Descriptive.

Example :

   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 format with Getopt::Long::Descriptive.

negativable

add the attribute "!" to the name. It will allow negative params.

Ex :

  test --quiet
  => quiet = 1

  test --quiet --no-quiet
  => quiet = 0
repeatable

add the attribute "@" to the name. It will allow repeatable params.

Ex :

  test --verbose
  => verbose = 1

  test --verbose --verbose
  => verbose = 2
autosplit

auto split args to generate multiple value. You need to specified the format with "@" to make it work.

Ex :

    package t;
    use Moo;
    use MooX::Option;
    
    option test => (is => 'ro', format => 'i@', autosplit => 1);
    1;
    
    @ARGV=('--test=1,2,3,4');
    my $t = t->new_with_options;
    t->test # [1,2,3,4]
short

give short name of an attribute.

Ex :

    package t;
    use Moo;
    use MooX::Option;
    
    option 'verbose' => (is => 'ro', repeatable => 1, short => 'v');
    
    1;
    @ARGV=('-vvv');
    my $t = t->new_with_options;
    t->verbose # 3

THANKS

Matt S. Trout (mst) <mst@shadowcat.co.uk>

For his patience and advice.

BUGS

Any bugs or evolution can be submit here :

Github

AUTHOR

Geistteufel <geistteufel@celogeek.fr>

COPYRIGHT AND LICENSE

This software is copyright (c) 2011 by Geistteufel <geistteufel@celogeek.fr>.

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