This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perl 5.003_06: [patch introduction and re-organisation]
[perl5.git] / t / lib / getopt.t
1 #!./perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     @INC = '../lib';
6 }
7
8 print "1..11\n";
9
10 use Getopt::Std;
11
12 # First we test the getopt function
13 @ARGV = qw(-xo -f foo -y file);
14 getopt('f');
15
16 print "not " if "@ARGV" ne 'file';
17 print "ok 1\n";
18
19 print "not " unless $opt_x && $opt_o && opt_y;
20 print "ok 2\n";
21
22 print "not " unless $opt_f eq 'foo';
23 print "ok 3\n";
24
25
26 # Then we try the getopts
27 $opt_o = $opt_i = $opt_f = undef;
28 @ARGV = qw(-foi -i file);
29 getopts('oif:') or print "not ";
30 print "ok 4\n";
31
32 print "not " unless "@ARGV" eq 'file';
33 print "ok 5\n";
34
35 print "not " unless $opt_i and $opt_f eq 'oi';
36 print "ok 6\n";
37
38 print "not " if $opt_o;
39 print "ok 7\n";
40
41 # Try illegal options, but avoid printing of the error message
42
43 open(STDERR, ">stderr") || die;
44 unlink "stderr";
45
46 @ARGV = qw(-h help);
47
48 !getopts("xf:y") or print "not ";
49 print "ok 8\n";
50
51
52 # Then try the Getopt::Long module
53
54 use Getopt::Long;
55
56 @ARGV = qw(--help --file foo --foo --nobar --num=5 -- file);
57
58 GetOptions(
59    'help'   => \$HELP,
60    'file:s' => \$FILE,
61    'foo!'   => \$FOO,
62    'bar!'   => \$BAR,
63    'num:i'  => \$NO,
64 ) || print "not ";
65 print "ok 9\n";
66
67 print "not " unless $HELP && $FOO && !$BAR && $FILE eq 'foo' && $NO == 5;
68 print "ok 10\n";
69
70 print "not " unless "@ARGV" eq "file";
71 print "ok 11\n";