Smart::Options - smart command line options processor
use Smart::Options;
my $argv = Smart::Options->new->argv;
if ($argv->{rif} - 5 * $argv->{xup} > 7.138) {
say 'Buy more fiffiwobbles';
}
else {
say 'Sell the xupptumblers';
}
# $ ./example.pl --rif=55 --xup=9.52
# Buy more fiffiwobbles
#
# $ ./example.pl --rif 12 --xup 8.1
# Sell the xupptumblers
Smart::Options is a library for option parsing for people tired option parsing. This module is analyzed as people interpret an option intuitively.
Create a parser object.
use Smart::Options;
my $argv = Smart::Options->new->parse(qw(-x 10 -y 2));
parse @args. return hashref of option values. if @args is empty Smart::Options use @ARGV
shortcut method. this method auto export.
use Smart::Options;
say argv(qw(-x 10))->{x};
is the same as
use Smart::Options ();
Smart::Options->new->parse(qw(-x 10))->{x};
set alias for option. you can use "$option" field of argv.
use Smart::Options;
my $argv = Smart::Options->new->alias(f => 'file')->parse(qw(-f /etc/hosts));
$argv->{file} # => '/etc/hosts'
set default value for option.
use Smart::Options;
my $argv = Smart::Options->new->default(y => 5)->parse(qw(-x 10));
$argv->{x} + $argv->{y} # => 15
set option help message.
use Smart::Options;
my $opt = Smart::Options->new()->alias(f => 'file')->describe('Load a file');
say $opt->help;
# Usage: ./example.pl
#
# Options:
# -f, --file Load a file
#
interpret 'option' as a boolean.
use Smart::Options;
my $argv = Smart::Options->new->parse(qw(-x 11 -y 10));
$argv->{x} # => 11
my $argv2 = Smart::Options->new->boolean('x')->parse(qw(-x 11 -y 10));
$argv2->{x} # => true (1)
show usage (showHelp()) and exit if $option wasn't specified in args.
use Smart::Options;
my $opt = Smart::Options->new()->alias(f => 'file')
->demand('file')
->describe('Load a file');
$opt->argv(); # => exit
# Usage: ./example.pl
#
# Options:
# -f, --file Load a file [required]
#
use Smart::Options;
my $opt = Smart::Options->new()
->options( f => { alias => 'file', default => '/etc/passwd' } );
is the same as
use Smart::Options;
my $opt = Smart::Options->new()
->alias(f => 'file')
->default(f => '/etc/passwd');
set type check for option value
use Smart::Options;
my $opt = Smart::Options->new()->type(foo => 'Int');
$opt->parse('--foo=bar') # => fail
$opt->parse('--foo=3.14') # => fail
$opt->parse('--foo=1') # => ok
support type is here.
Bool
Str
Int
Num
ArrayRef
HashRef
Config
'Config' is special type. The contents will be read into each option if a file name is specified as a Config type option.
use Smart::Options;
my $opt = Smart::Options->new()->type(conf => 'Config');
$opt->parse(qw(--conf=.optrc));
config file format is simple. see http://en.wikipedia.org/wiki/INI\_file
; this is comment
[section]
key=value
key2=value2
define new type and convert logic.
use Smart::Options;
use Path::Class; # export 'file'
my $opt = Smart::Options->new()->coerce(File => 'Str', sub { file($_[0]) })
->type(file => 'File');
$opt->parse('--foo=/etc/passwd');
$argv->{file} # => Path::Class::File instance
set a usage message to show which command to use. default is "Usage: $0".
return help message string
print usage message. default output STDERR.
set a sub command. $parser is another Smart::Option object.
use Smart::Options;
my $opt = Smart::Options->new()
->subcmd(add => Smart::Options->new())
->subcmd(minus => Smart::Options->new());
see also Smart::Options::Declare
use '--' to stop parsing.
use Smart::Options;
use Data::Dumper;
my $argv = argv(qw(-a 1 -b 2 -- -c 3 -d 4));
warn Dumper($argv);
# $VAR1 = {
# 'a' => '1',
# 'b' => '2',
# '_' => [
# '-c',
# '3',
# '-d',
# '4'
# ]
# };
'--no-key' set false to $key.
use Smart::Options;
argv(qw(-a --no-b))->{b}; # => 0
If set flag multiple times it will get arrayref.
use Smart::Options;
argv(qw(-x 1 -x 2 -x 3))->{x}; # => [1, 2, 3]
use Smart::Optuions;
argv(qw(--foo.x 1 --foo.y 2)); # => { foo => { x => 1, y => 2 } }
Kan Fushihara [email protected]
https://github.com/substack/node-optimist
GetOpt::Casual, opts, GetOpt::Compat::WithCmd
Copyright (C) Kan Fushihara
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.