MooX::Options

Celogeek

Tue 21 August, 2012

Slides made for YAPC::Europe 2012

 

Instructions

  • Press space to see the plan and space again to go the slide.
  • Use arrow to move between slide (left, right, down, up).
  • Click on the code area to zoom in, click again to zoom out.

MooX::Options

Create easily command line tools with Mo/Moo/Moose.

Example 1: basic usage

To create a simple tools you can use MooX::Options in a standalone program :

option 'verbose' => (is => 'ro', short => 'v', repeatable => 1);
                    

Download demo1.pl


$ perl slides/2012/08/moox-options-slide3d-demo1.pl -h
USAGE: moox-options-slide3d-demo1.pl [-hv] [long options...]
-v --verbose    no doc for verbose
-h --help       show this help message

$ perl slides/2012/08/moox-options-slide3d-demo1.pl -v
I have a tiny voice.

$ perl slides/2012/08/moox-options-slide3d-demo1.pl -vv
I have a tiny voice.
Oh wait, I can speak lourder. Boo :)

$ perl slides/2012/08/moox-options-slide3d-demo1.pl -vvv
I have a tiny voice.
Oh wait, I can speak louder. Boo :)
You will regret this. Yahhhhhhhhhhh, I speak too much.
                

Example 2: array

Fetch list of values

option 'ids' => (is => 'ro', format => 'i', repeatable => 1,
                 required => 1, autosplit => ',');
                
Download demo2.pl

$ perl slides/2012/08/moox-options-slide3d-demo2.pl --ids=1 
$VAR1 = [ 1 ];

$ perl slides/2012/08/moox-options-slide3d-demo2.pl --ids=1 --ids=2
$VAR1 = [ 1, 2 ];

$ perl slides/2012/08/moox-options-slide3d-demo2.pl --ids=1 --ids=2 --ids=3,4
$VAR1 = [ 1, 2, 3, 4 ];
                

Example 3: pass_through

How to use pass_through with MooX::Options

use MooX::Options flavour => [qw( pass_through )], protect_argv => 0;
option 'output' => (is => 'ro', format => 's', required => 1, short => 'o');
...
say Dumper \@ARGV;
                
Download demo3.pl

$ perl slides/2012/08/moox-options-slide3d-demo3.pl -o my.zip
Output : my.zip
$VAR1 = [];

$ perl slides/2012/08/moox-options-slide3d-demo3.pl -o my.zip a b c d
Output : my.zip
$VAR1 = [ 'a', 'b', 'c', 'd' ];
                

Example 4: Role

MooX::Options support the role of Moo, Moose, Role::Tiny.

{package myOutputRole; use strict; use warnings;
    use Moo::Role;
    use MooX::Options;
    option 'output' => (is => 'ro', format => 's', required => 1, short => 'o');
    1;
}

{package myVerboseRole; use strict; use warnings;
 use Moo::Role;
 use MooX::Options;
 option 'verbose' => (is => 'ro', short => 'v', repeatable => 1);
 1;
}
                

{package myOpt; use strict; use warnings;
use Moo;
use MooX::Options flavour => [qw( pass_through )], protect_argv => 0;
with 'myOutputRole', 'myVerboseRole';
1;
}
                
Download demo4.pl

$ perl moox-options-slide3d-demo4.pl -o test.zip -vvv file1 file2 file3
Output : test.zip
Verbose: 3
$VAR1 = [ 'file1', 'file2', 'file3' ];
                

Example 5: skip_options

You can ignore an option with skip_options. The 'option' keyword will behave like 'has' keyword.

{package myOpt; use strict; use warnings;
use Moo;
use MooX::Options flavour => [qw( pass_through )]
                , protect_argv => 0
                , skip_options => [qw/verbose/];
with 'myOutputRole', 'myVerboseRole';
1;
}
                
Download demo5.pl

$ perl moox-options-slide3d-demo5.pl -o test.zip -vvv file1 file2 file3
Output : test.zip
Verbose: 0
$VAR1 = [ '-vvv', 'file1', 'file2', 'file3' ];
                

Thanks to

  • Matt (mst) Trout (design and advices)
  • Tomas (t0m) Doran (help and using it in Message::Passing)

Thank you!

Celogeek

Tue 21 August, 2012

Slides made for YAPC::Europe 2012