1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
use lib "lib";
use Test::More; # tests => 4;
use Test::Exception;
use Getopt::ArgParse::Parser;
$p = Getopt::ArgParse::Parser->new();
ok($p, "new argparser");
$p->add_argument(
'boo',
# nargs => 1,
);
$n = $p->parse_args(split(' ', '100'));
ok($n->boo == 100, 'boo is 100 nargs=>1');
$p->add_argument(
'boo1',
type => 'Array',
nargs => 2,
);
$p->add_argument(
'boo2',
nargs => '?',
);
lives_ok(
sub { $n = $p->parse_args(split(' ', 'abc 100 200')); }
);
ok(!$n->boo2, 'boo2 is not taken nargs=>?');
lives_ok(
sub { $n = $p->parse_args(split(' ', 'abc 100 200 300')); }
);
ok($n->boo2 == 300, 'boo2 is 300 nargs=>?');
$p->add_argument(
'boo3',
type => 'Array',
nargs => '+',
);
throws_ok(
sub { $n = $p->parse_args(split(' ', 'abc 100 200 300')); },
qr/Too few arguments/,
'too few arguments for +',
);
lives_ok(
sub { $n = $p->parse_args(split(' ', 'abc 100 200 300 400 500')); },
);
$p = Getopt::ArgParse::Parser->new();
ok($p, "new argparser");
$p->add_argument(
'boo',
# nargs => 1,
);
$p->add_argument(
'boo1',
type => 'Array',
nargs => '*',
);
lives_ok(
sub { $n = $p->parse_args(split(' ', 'abc')); },
);
lives_ok(
sub { $n = $p->parse_args(split(' ', 'abc 100 200 300 400 500')); },
);
ok($n->boo1->[4] == 500, 'nargs=>* boo1->[4] is 500');
throws_ok (
sub { $p->add_argument('-f', nargs => 10) },
qr/only allow/,
'not allowed for optional options',
);
$p->add_argument('f', nargs => 'abc');
throws_ok (
sub { $p->parse_args('abc') },
qr/Invalid nargs/,
'invalid nargs',
);
done_testing;
|