This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Clarify documentation re switch expecting an argument but none is provided.
authorJames E Keenan <jkeenan@cpan.org>
Sun, 4 Aug 2013 02:30:42 +0000 (22:30 -0400)
committerJames E Keenan <jkeenan@cpan.org>
Wed, 7 Aug 2013 11:17:34 +0000 (07:17 -0400)
When a switch is expecting an argument but fails to be provided with one, the
value of the corresponding element in the options hash is set to the undefined
value.  The documentation did not make this clear.

The documentation for getopts() and getopt() has been revised and additional
tests have been provided to demonstrate the point.  Tweaked in response to
feedback from Karl Williamson++.

Also, unit tests found in lib/Getopt/Std.t actually for Getopt::Long have been
removed.  A patch holding those tests has been submitted to Getopt-Long's bug
queue on rt.cpan.org.

For: RT #41359

lib/Getopt/Std.pm
lib/Getopt/Std.t

index c0901eb..ecb7ebb 100644 (file)
@@ -19,30 +19,36 @@ getopt, getopts - Process single-character switches with switch clustering
 
 =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
@@ -75,7 +81,7 @@ and version_mess() with the switches string as an 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;
 
index 35922ab..ad47591 100644 (file)
@@ -6,7 +6,7 @@ BEGIN {
 }
 
 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);
@@ -52,22 +52,41 @@ $SIG{__WARN__} = sub { $warning = $_[0] };
 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;
+}