1N/A#!./perl -wT
1N/A
1N/ABEGIN {
1N/A chdir 't' if -d 't';
1N/A @INC = '../lib';
1N/A}
1N/A
1N/Ause strict;
1N/Ause Test::More tests => 21;
1N/Ause Getopt::Std;
1N/A
1N/Aour ($warning, $opt_f, $opt_i, $opt_o, $opt_x, $opt_y, %opt);
1N/A
1N/A# First we test the getopt function
1N/A@ARGV = qw(-xo -f foo -y file);
1N/Agetopt('f');
1N/A
1N/Ais( "@ARGV", 'file', 'options removed from @ARGV (1)' );
1N/Aok( $opt_x && $opt_o && $opt_y, 'options -x, -o and -y set' );
1N/Ais( $opt_f, 'foo', q/option -f is 'foo'/ );
1N/A
1N/A@ARGV = qw(-hij k -- -l m -n);
1N/Agetopt 'il', \%opt;
1N/A
1N/Ais( "@ARGV", 'k -- -l m -n', 'options removed from @ARGV (2)' );
1N/Aok( $opt{h} && $opt{i} eq 'j', 'option -h and -i correctly set' );
1N/Aok( !defined $opt{l}, 'option -l not set' );
1N/Aok( !defined $opt_i, '$opt_i still undefined' );
1N/A
1N/A# Then we try the getopts
1N/A$opt_o = $opt_i = $opt_f = undef;
1N/A@ARGV = qw(-foi -i file);
1N/A
1N/Aok( getopts('oif:'), 'getopts succeeded (1)' );
1N/Ais( "@ARGV", 'file', 'options removed from @ARGV (3)' );
1N/Aok( $opt_i && $opt_f eq 'oi', 'options -i and -f correctly set' );
1N/Aok( !defined $opt_o, 'option -o not set' );
1N/A
1N/A%opt = (); $opt_i = undef;
1N/A@ARGV = qw(-hij -k -- -l m);
1N/A
1N/Aok( getopts('hi:kl', \%opt), 'getopts succeeded (2)' );
1N/Ais( "@ARGV", '-l m', 'options removed from @ARGV (4)' );
1N/Aok( $opt{h} && $opt{k}, 'options -h and -k set' );
1N/Ais( $opt{i}, 'j', q/option -i is 'j'/ );
1N/Aok( !defined $opt_i, '$opt_i still undefined' );
1N/A
1N/A# Try illegal options, but avoid printing of the error message
1N/A$SIG{__WARN__} = sub { $warning = $_[0] };
1N/A@ARGV = qw(-h help);
1N/A
1N/Aok( !getopts("xf:y"), 'getopts fails for an illegal option' );
1N/Aok( $warning eq "Unknown option: h\n", 'user warned' );
1N/A
1N/A# Then try the Getopt::Long module
1N/A
1N/Ause Getopt::Long;
1N/A
1N/A@ARGV = qw(--help --file foo --foo --nobar --num=5 -- file);
1N/A
1N/Aour ($HELP, $FILE, $FOO, $BAR, $NO);
1N/A
1N/Aok( GetOptions(
1N/A 'help' => \$HELP,
1N/A 'file:s' => \$FILE,
1N/A 'foo!' => \$FOO,
1N/A 'bar!' => \$BAR,
1N/A 'num:i' => \$NO,
1N/A ),
1N/A 'Getopt::Long::GetOptions succeeded'
1N/A);
1N/Ais( "@ARGV", 'file', 'options removed from @ARGV (5)' );
1N/Aok( $HELP && $FOO && !$BAR && $FILE eq 'foo' && $NO == 5, 'options set' );