6
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

perl : Smart::Optionsで、コマンドライン引数のパース

Posted at

perl でコマンドラインツールを作る場合の、
コマンドライン引数のパースを便利化してくれるSmart::Optionsのメモです。

手抜きパターン

p1.pl
my $args    = Smart::Options->new->parse;
warn Dumper($args);

結果

$ perl p1.pl --arg1 111 --arg2 222 --arg3 333a --arg3 333b --flag 
$VAR1 = {
          '_' => [],
          'arg1' => '111',
          'arg2' => '222',
          'arg3' => [
                      '333a',
                      '333b'
                    ],
          'flag' => 1
        };

もっと簡略化すると

p2.pl
my $args = argv;
warn Dumper($args);
$ perl p2.pl   --arg1 111 --arg2 222 --arg3 333a --arg3 333b --flag
$VAR1 = {
          '_' => [],
          'arg1' => '111',
          'arg2' => '222',
          'arg3' => [
                      '333a',
                      '333b'
                    ],
          'flag' => 1
        };

ちゃんとやる場合

p3.pl
my $args = opts()->parse(@ARGV);
warn Dumper($args);

sub opts {
    return
    Smart::Options->new->options(
        arg1    => {describe    => "arg1 description", demand  => "arg1"}, # demand  => arg_name
        arg2    => {describe    => "arg2 description", alias   => "a2"},   # alias   => alias_name
        arg3    => {describe    => "arg3 description", default => "ARG3"}, # default => default_value
        bool    => {describe    => "boolean option"  , boolean => "bool"}, # boolean => arg_name
        int     => {describe    => "Integer option"  , type    => "Int"},  # type    => type_name (Bool, Str, Int, Num, ArrayRef, HashRef, Config)
    );
}

結果

$ perl p3.pl
Usage: p3.pl

Options:
  --a2, --arg2  Arg2 description
  --arg1        Arg1 description             [required]
  --arg3        Arg3 description                         [default: ARG3]
  --bool        Boolean option    [boolean]
  -h, --help    Show help
  --int         Integer option


Missing required arguments: arg1
Died at ...


$ perl p3.pl  --arg1 111 -a2 222  --bool 100  --int 999
$VAR1 = {
          '_' => [
                   '100'
                 ],
          'a2' => '222',
          'arg1' => '111',
          'arg3' => 'ARG3',
          'bool' => 1,
          'int' => '999'
        };

$ perl p3.pl  --arg1 111 --arg3 OVERWRITE   --bool 0
$VAR1 = {
          '_' => [
                   '0'
                 ],
          'arg1' => '111',
          'arg3' => 'OVERWRITE',
          'bool' => 1  # 0にはならないようだ
        };
        
$ perl p3.pl --arg1 111  --int string
Value 'string' invalid for option int(Int)
6
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?