CLI-Cmdline
view release on metacpan or search on metacpan
t/03-complex.t view on Meta::CPAN
# t/03-complex.t - Test spec strings with or without -/-- prefixes
use strict;
use warnings;
use Test::More tests => 29;
use Test::NoWarnings 'had_no_warnings';
use CLI::Cmdline qw(parse);
my %opt;
sub run_test {
my ($desc, $opt_ref, $sw, $opt_str, @args) = @_;
local @ARGV = @args;
my $result = CLI::Cmdline::parse($opt_ref, $sw // '', $opt_str // '');
ok($result, $desc) or diag("Failed: @args");
return $result;
}
# 01 - bare names only (no prefixes in spec)
%opt = ( v => 0, quiet => 0 );
run_test('01 - bare spec names', \%opt, 'v|verbose quiet|q', '', '-v', '--quiet');
is($opt{v}, 1, '01 - v from bare spec');
is($opt{quiet}, 1, '01 - quiet from bare spec');
# 02 - traditional prefixed spec
%opt = ( h => 0 );
run_test('02 - prefixed spec -h|--help', \%opt, '-h|--help', '', '--help');
is($opt{h}, 1, '02 - help maps to h');
# 03 - mixed bare and prefixed in same spec
%opt = ( x => 0 );
run_test('03 - mixed styles x|-x|--extract', \%opt, 'x|-x|--extract', '', '--extract', '-x');
is($opt{x}, 2, '03 - both forms increment x');
# 04 - option with bare name in spec
%opt = ( file => '' );
run_test('04 - bare option name file|f', \%opt, '', 'file|f', '--file=data.txt');
is($opt{file}, 'data.txt', '04 - file set from bare spec');
# 05 - option with prefixed spec
%opt = ( input => '' );
run_test('05 - prefixed option --input|-i', \%opt, '', '--input|-i', '-i', 'in.txt');
is($opt{input}, 'in.txt', '05 - input set via short alias');
# 06 - complex real-world example using bare names
%opt = (
verbose => 0,
dryrun => 0,
config => '',
include => [],
);
run_test('06 - complex bare spec', \%opt,
'verbose|v dryrun|n|dry-run',
'config|c include|I|inc',
'-vvv', '--dry-run', '--config=/etc/app.cfg', '-I', 'lib', '--include', 'extra'
);
is($opt{verbose}, 3, '06 - verbose counted');
is($opt{dryrun}, 1, '06 - dry-run set');
is($opt{config}, '/etc/app.cfg', '06 - config set');
is_deeply($opt{include}, ['lib', 'extra'], '06 - include collected via aliases');
# 07 - empty switch string
%opt = ( file => '' );
run_test('07 - empty switch string', \%opt, '', 'file', '--file', 'test.txt');
is($opt{file}, 'test.txt', '07 - option works with no switches');
# 08 - empty option string
%opt = ( v => 0 );
run_test('08 - empty option string', \%opt, 'v|verbose', '', '-vvv');
is($opt{v}, 3, '08 - switches work with no options');
# 09 - both spec strings empty
%opt = ( dummy => 42 );
( run in 1.414 second using v1.01-cache-2.11-cpan-59e3e3084b8 )