use Getopt::Dakkar::Style qw(test);
use Getopt::Dakkar;
use Getopt::Dakkar::Option;
use Getopt::Dakkar::Parameter;
use Getopt::Dakkar::Subcommand;
subtest 'nothing' => sub {
my $getopt = Getopt::Dakkar->new();
my $stash;
lives_ok { $stash = $getopt->parse([]) }
'nothing parsing nothing should live';
cmp_deeply(
$stash,
methods(
options => {},
arguments => {},
),
'the stash should be empty',
) or explain $stash;
throws_ok { $stash = $getopt->parse([1]) }
'Getopt::Dakkar::X::ExtraArgs',
'too many elements will throw';
};
subtest 'simple option' => sub {
my $getopt = Getopt::Dakkar->new({
options => [
Getopt::Dakkar::Option->new({
name => 'foo',
aliases => ['f'],
}),
],
});
my $stash;
for my $form (qw(-f --foo)) {
lives_ok { $stash = $getopt->parse([$form]) }
"parsing $form should live";
cmp_deeply(
$stash,
methods(
options => { foo => methods(name=>'foo',value=>1) },
arguments => {},
),
'the stash should contain the option value',
) or explain $stash;
}
for my $form (qw(-x -fx --xx)) {
throws_ok { $stash = $getopt->parse([$form]) }
'Getopt::Dakkar::X::BadOption',
"parsing $form should throw";
}
};
subtest 'option bundling' => sub {
my $getopt = Getopt::Dakkar->new({
options => [
Getopt::Dakkar::Option->new({
name => 'foo',
aliases => ['f'],
can_bundle => 1,
}),
Getopt::Dakkar::Option->new({
name => 'bar',
aliases => ['b'],
can_bundle => 1,
}),
],
});
my $stash;
for my $form (
[qw(-fb)], [qw(-bf)],
[qw(-f -b)], [qw(-b -f)],
[qw(--foo -b)], [qw(--bar -f)],
[qw(--foo --bar)],
) {
lives_ok { $stash = $getopt->parse($form) }
"parsing $form->@* should live";
cmp_deeply(
$stash,
methods(
options => {
foo => methods(name=>'foo',value=>1),
bar => methods(name=>'bar',value=>1),
},
arguments => {},
),
'the stash should contain the option value',
) or explain $stash;
}
for my $form (
[qw(--foobar)], [qw(-foo)],
) {
throws_ok { $stash = $getopt->parse($form) }
'Getopt::Dakkar::X::BadOption',
"parsing $form->@* should throw";
}
};
done_testing;