=head1 DESCRIPTION
-The getopts() function processes single-character switches with switch
-clustering. Pass one argument which is a string containing all switches
-to be recognized. For each switch found, sets $opt_x (where x is the
-switch name) to the value of the argument if an argument is expected,
-or 1 otherwise. Switches which take an argument don't care whether
-there is a space between the switch and the argument. If unspecified switches
-are found on the command-line, the user will be warned that an unknown
-option was given. The getopts() function returns true unless an invalid
-option was found.
-
-The getopt() function is similar, but its argument is a string containing
-all switches that take an argument. Unspecified switches are silently
-accepted. Its use is not recommended.
+The C<getopts()> function processes single-character switches with switch
+clustering. Pass one argument which is a string containing all switches to be
+recognized. For each switch found, if an argument is expected and provided,
+C<getopts()> sets C<$opt_x> (where C<x> is the switch name) to the value of
+the argument. If an argument is expected but none is provided, C<$opt_x> is
+set to an undefined value. If a switch does not take an argument, C<$opt_x>
+is set to C<1>.
+
+Switches which take an argument don't care whether there is a space between
+the switch and the argument. If unspecified switches are found on the
+command-line, the user will be warned that an unknown option was given.
+
+The C<getopts()> function returns true unless an invalid option was found.
+
+The C<getopt()> function is similar, but its argument is a string containing
+all switches that take an argument. If no argument is provided for a switch,
+say, C<y>, the corresponding C<$opt_y> will be set to an undefined value.
+Unspecified switches are silently accepted. B<Use of C<getopts()> is not
+recommended.>
Note that, if your code is running under the recommended C<use strict
-'vars'> pragma, you will need to declare these package variables
-with "our":
+vars> pragma, you will need to declare these package variables
+with C<our>:
our($opt_x, $opt_y);
-For those of you who don't like additional global variables being created, getopt()
-and getopts() will also accept a hash reference as an optional second argument.
-Hash keys will be x (where x is the switch name) with key values the value of
-the argument or 1 if no argument is specified.
+For those of you who don't like additional global variables being created,
+C<getopt()> and C<getopts()> will also accept a hash reference as an optional
+second argument. Hash keys will be C<x> (where C<x> is the switch name) with
+key values the value of the argument or C<1> if no argument is specified.
To allow programs to process arguments that look like switches, but aren't,
both functions will stop processing switches when they see the argument
@ISA = qw(Exporter);
@EXPORT = qw(getopt getopts);
-$VERSION = '1.09';
+$VERSION = '1.10';
# uncomment the next line to disable 1.03-backward compatibility paranoia
# $STANDARD_HELP_VERSION = 1;
}
use strict;
-use Test::More tests => 21;
+use Test::More tests => 22;
use Getopt::Std;
our ($warning, $opt_f, $opt_i, $opt_o, $opt_x, $opt_y, %opt);
ok( !getopts("xf:y"), 'getopts fails for an illegal option' );
ok( $warning eq "Unknown option: h\n", 'user warned' );
-# Then try the Getopt::Long module
-
-use Getopt::Long;
+# Tests for RT #41359
+undef %opt;
+my $expected;
+{
+ local @ARGV = ( '-a', '-b', 'foo', '-c' );
+ getopts('ab:c:', \%opt);
+ $expected = { 'a' => 1, 'b' => 'foo', 'c' => undef };
+ is_deeply(\%opt, $expected,
+ "getopts: multiple switches; switch expected argument, none provided; value undef");
+ undef %opt;
+}
-@ARGV = qw(--help --file foo --foo --nobar --num=5 -- file);
+{
+ local @ARGV = ( '-c' );
+ getopts('c:', \%opt);
+ $expected = { 'c' => undef };
+ is_deeply(\%opt, $expected,
+ "getopts: single switch; switch expected argument, none provided; value undef");
+ undef %opt;
+}
-our ($HELP, $FILE, $FOO, $BAR, $NO);
+{
+ local @ARGV = ( '-b', 'foo', '-c' );
+ getopt('bc', \%opt);
+ $expected = { 'b' => 'foo', 'c' => undef };
+ is_deeply(\%opt, $expected,
+ "getopt: multiple switches; switch expected argument, none provided; value undef");
+ undef %opt;
+}
-ok( GetOptions(
- 'help' => \$HELP,
- 'file:s' => \$FILE,
- 'foo!' => \$FOO,
- 'bar!' => \$BAR,
- 'num:i' => \$NO,
- ),
- 'Getopt::Long::GetOptions succeeded'
-);
-is( "@ARGV", 'file', 'options removed from @ARGV (5)' );
-ok( $HELP && $FOO && !$BAR && $FILE eq 'foo' && $NO == 5, 'options set' );
+{
+ local @ARGV = ( '-c' );
+ getopt('c', \%opt);
+ $expected = { 'c' => undef };
+ is_deeply(\%opt, $expected,
+ "getopt: single switch; switch expected argument, none provided; value undef");
+ undef %opt;
+}