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