opts - simple command line option parser
# in script.pl use opts; opts my $foo => 'Int'; ./script.pl --foo=4 # $foo => 4 ./script.pl --foo 4 # $foo => 4 ./script.pl -f=4 # $foo => 4 # in script.pl opts my $foo => { isa => 'Int', required => 1, comment => 'this is output to --help' }, my $bar => 'Int'; ./script.pl --foo=3 --bar=4 # $foo => 3, $bar => 4 ./script.pl --foo=4 # $foo => 4, $bar => undef ./script.pl --bar=4 # error! # in script.pl opts my $foo => {isa => 'Int', default => 3}, ./script.pl --foo=4 # $foo => 4 ./script.pl # $foo => 3 # in script.pl opts my $foo => { isa => 'Int', alias => 'x|bar' }; ./script.pl --foo=4 # $foo => 4 ./script.pl --bar=4 # $foo => 4 ./script.pl -f=4 # $foo => 4 ./script.pl -x=4 # $foo => 4
opts is DSL for command line option.
isa define option value type. see $opts::TYPE_CONSTRAINT. if you need more type, see opts::coerce required define option value is required. default define options default value. If passed a coderef, it will be executed if no value is provided on the command line. alias define option param's alias. comment this comment is used to generate help. help can show --help
This subtype is based off of ArrayRef. It will attempt to split any values passed on the command line on a comma: that is,
[ "one", "two,three" ]
will become
[ "one", "two", "three" ].
opts::coerce NewType => SrcType => generater; ex) opts::coerce DateTime => 'Str' => sub { DateTime->strptime("%Y-%m-%d", shift) }; opts my $date => 'DateTime'; $date->ymd; # => yyyy/mm/dd
Kan Fushihara <kan.fushihara at gmail.com>
Chris Weyl http://search.cpan.org/~rsrchboy/
Smart::Args, Getopt::Long
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install opts, copy and paste the appropriate command in to your terminal.
cpanm
cpanm opts
CPAN shell
perl -MCPAN -e shell install opts
For more information on module installation, please visit the detailed CPAN module installation guide.