| 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 | |
| 45 | @ARGV = qw(-h help); |
| 46 | |
| 47 | !getopts("xf:y") or print "not "; |
| 48 | print "ok 8\n"; |
| 49 | |
| 50 | |
| 51 | # Then try the Getopt::Long module |
| 52 | |
| 53 | use Getopt::Long; |
| 54 | |
| 55 | @ARGV = qw(--help --file foo --foo --nobar --num=5 -- file); |
| 56 | |
| 57 | GetOptions( |
| 58 | 'help' => \$HELP, |
| 59 | 'file:s' => \$FILE, |
| 60 | 'foo!' => \$FOO, |
| 61 | 'bar!' => \$BAR, |
| 62 | 'num:i' => \$NO, |
| 63 | ) || print "not "; |
| 64 | print "ok 9\n"; |
| 65 | |
| 66 | print "not " unless $HELP && $FOO && !$BAR && $FILE eq 'foo' && $NO == 5; |
| 67 | print "ok 10\n"; |
| 68 | |
| 69 | print "not " unless "@ARGV" eq "file"; |
| 70 | print "ok 11\n"; |
| 71 | |
| 72 | close STDERR; |
| 73 | unlink "stderr"; |