File: required.t

package info (click to toggle)
libgetopt-argparse-perl 1.0.6-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 348 kB
  • sloc: perl: 1,133; makefile: 2
file content (133 lines) | stat: -rw-r--r-- 2,943 bytes parent folder | download | duplicates (3)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
use lib 'lib';
use Test::More; # tests => 10;
use Test::Exception;

use Getopt::ArgParse::Parser;

$p = Getopt::ArgParse::Parser->new(
    print_usage_if_help => 0, # no
);
ok($p, "new argparser");

$p->add_argument('--required-option', required => 1);
$p->add_argument('--optional-option');

lives_ok(
    sub {$n = $p->parse_args(split(' ', '--required-option hello'));},
);

ok($n->required_option eq "hello", "required_option");
ok( !defined($n->optional_option), "optional_option is undef");

lives_ok(
    sub { $n = $p->parse_args( split(' ', '--optional-option hello') ); },
);
ok($n->required_option eq "hello", "required_option");
ok($n->optional_option eq 'hello', "optional_option is hello");

$p->namespace(Getopt::ArgParse::Namespace->new()); # Clear out required-option
# multiple parsing preserves previous values
$n = $p->namespace;

$p->add_argument('--optional-option', reset => 1);

throws_ok(
    sub { $n = $p->parse_args( split(' ', '--optional-option hello') ); },
    qr/required/,
    "required option",
);
lives_ok(
    sub { $n = $p->parse_args('--help') },
);

ok($n->help, 'help');

$p = Getopt::ArgParse::Parser->new(
    print_usage_if_help => 0, # no
);

$p->add_subparsers();
$lp = $p->add_parser('list');

$lp->add_arguments(
    [ '--type', required => 1],
    [ 'branch', required => 1],
);

lives_ok(
sub { $n = $p->parse_args('list', '--help'); },
);

ok($n->help, 'list -help');

# postional options
$p = Getopt::ArgParse::Parser->new();

$p->add_argument('-f');
$p->add_argument('boo'); # not required

$n = $p->parse_args(split(' ', '-f 10'));

ok (!$n->boo, 'boo is not required');

$p->add_argument('boo', required => 1, reset => 1);

throws_ok (
    sub { $n = $p->parse_args(split(' ', '-f 10')); },
    qr /required/,
    'required positional arg: boo'
);

lives_ok(
    sub { $n = $p->parse_args(split(' ', '-f 10 100')); },
);

ok($n->boo == 100, 'boo is 100');

throws_ok(
    sub { $p->add_argument('boo', nargs => 2); },
    qr/Redefine option boo without reset/,
    'Redfine option boo'
);

lives_ok(
    sub { $p->add_argument('boo', nargs => 2, reset => 1); },
);

lives_ok(
    sub { $n = $p->parse_args(split(' ', '-f 10')); },
);

throws_ok(
    sub { $n = $p->parse_args(split(' ', '-f 10 111')); },
    qr/Too few arguments for boo/,
    'not enough args for boo',
);

$p->add_argument('boo', type => 'Array', nargs => 2, required => 1, reset => 1);

# $n = $p->parse_args(split(' ', '-f 10'));
$n->set_attr('boo', undef);

# Now it will fail for it's required
throws_ok(
    sub { $n = $p->parse_args(split(' ', '-f 10')); },
    qr/required/,
    'boo is required',
);

throws_ok(
    sub { $n = $p->parse_args(split(' ', '-f 10 100')); },
    qr/Too few arguments for boo/,
    'not enough args for boo',
);

lives_ok(
    sub { $n = $p->parse_args(split(' ', '-f 10 100 20')); },
);

ok($n->boo->[0] == 100, 'boo 0 - 100');
ok($n->boo->[1] == 20, 'boo 1 - 20');

done_testing;