1use warnings; 2use strict; 3 4use Test::More tests => 15; 5 6require_ok "getopts.pl"; 7 8our($opt_f, $opt_h, $opt_i, $opt_k, $opt_o); 9 10$opt_o = $opt_i = $opt_f = undef; 11@ARGV = qw(-foi -i file); 12ok &Getopts("oif:"), "Getopts succeeded (1)"; 13is_deeply \@ARGV, [qw(file)], "options removed from \@ARGV (1)"; 14ok $opt_i, "option -i set"; 15is $opt_f, "oi", "option -f set correctly"; 16ok !defined($opt_o), "option -o not set"; 17 18$opt_h = $opt_i = $opt_k = undef; 19@ARGV = qw(-hij -k p -- -l m); 20ok &Getopts("hi:kl"), "Getopts succeeded (2)"; 21is_deeply \@ARGV, [qw(p -- -l m)], "options removed from \@ARGV (2)"; 22ok $opt_h, "option -h set"; 23ok $opt_k, "option -k set"; 24is $opt_i, "j", "option -i set correctly"; 25 26SKIP: { 27 skip "can't capture stderr", 4 unless "$]" >= 5.008; 28 my $warning = ""; 29 close(STDERR); 30 open(STDERR, ">", \$warning); 31 @ARGV = qw(-h help); 32 ok !Getopts("xf:y"), "Getopts fails for an illegal option"; 33 is $warning, "Unknown option: h\n", "user warned"; 34 $warning = ""; 35 close(STDERR); 36 open(STDERR, ">", \$warning); 37 @ARGV = qw(-h -- -i j); 38 ok !Getopts("hiy"), "Getopts fails for an illegal option"; 39 is $warning, "Unknown option: -\n", "user warned"; 40} 41 421; 43