bichonfrise74 has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

This could be a very simple question but I was trying to super search this question in perlmonk but could not find the answer. Also, I tried to search the Getopt::Long documentation but to no avail.

Basically, I want the code to give an exit if the value to the optional argument was not supplied. I have this code.
#!/usr/bin/perl use warnings; use strict; use Getopt::Long; my ($option_test); GetOptions( "test=s" => \$option_test ); print 'option is not defined.' if ( ! $option_test );
See below what happens when I run the code above.
$ test.pl option is not defined. $ test.pl -t Option test requires an argument option is not defined.
So, the 2 tests above shows that if I do not supply a proper argument to the -t option, then it is as if I did not use the -t option at all.

What I want is for Getopt::Long to exit if it gives this message "Option test requires an argument".

Thanks in advance.

Replies are listed 'Best First'.
Re: Requiring option values in Getopt::Long
by markkawika (Monk) on Aug 20, 2009 at 00:04 UTC

    Getopt::Long will not exit for you; it will warn you (and the user) about the error, but it's up to you to exit if there was a problem.

    Be sure to check the return value from GetOptions:

    use Getopt::Long; my $option_test; my $result = GetOptions ( 'test=s' => \$option_test ); if (! $result) { die "Invalid option specifications"; }

    Example:

    $ ./getopt.pl -t Option test requires an argument Invalid option specifications at ./getopt.pl line 9. $
Re: Requiring option values in Getopt::Long
by toolic (Bishop) on Aug 20, 2009 at 00:28 UTC
Re: Requiring option values in Getopt::Long
by JavaFan (Canon) on Aug 20, 2009 at 00:06 UTC
    What I want is for Getopt::Long to exit if it gives this message
    GetOptions( "test=s" => \$option_test ) or exit;
Re: Requiring option values in Getopt::Long
by bichonfrise74 (Vicar) on Aug 20, 2009 at 02:54 UTC
    I re-read the documentation again and figured out that this code actually solves what I want.
    #!/usr/bin/perl use warnings; use strict; use Getopt::Long; my ($option_test); my $check_option = GetOptions( "test=s" => \$option_test ); die 'Invalid option' if ( $check_option ne 1 ); print "next line";
    Here's the result when I ran some tests.
    $ test.pl next line $ test.pl -t abc next line $ test.pl -t Option test requires an argument Invalid option at test.pl line 9.
    Thank you all for your help.

      Why the redundant error error message? A cleaner version was previously suggested to you.

      GetOptions( 'test=s' => \$option_test ) or exit(1); defined($option_test) or die("Missing argument -t");

      At the very least, remove the line number from the error message. It's useless clutter.

      GetOptions( 'test=s' => \$option_test ) or die("Invalid command-line option\n"); defined($option_test) or die("Missing argument -t\nInvalid command-line option\n");

      However, I suggest that you display a useful error message instead:

      use File::Basename qw( basename ); sub help { my $prog = basename($0); print("usage: $prog -t [options]\n"); exit(0); } sub usage { my $msg = shift || ''; my $prog = basename($0); die("${msg}Use $prog --help for help\n"); } GetOptions( 'help|h|?' => \&help, 'test=s' => \$option_test, ) or usage(); defined($option_test) or usage("Missing argument -t\n");

        I usually prefer to write documentation for script as POD and then use Pod::Usage to show help message.

Re: Requiring option values in Getopt::Long
by bichonfrise74 (Vicar) on Aug 20, 2009 at 16:54 UTC
    Hmm, I tested the code of JavaFan last night and I thought it didn't work. I was wrong because it did work and I was probably too sleepy to test it out properly.

    Anyway, thanks JavaFan and ikegami for the help.
      I've tried the sample script: #!/usr/local/bin/perl use Getopt::Long; my $option_test; my $result = GetOptions ( 'test=s' => \$option_test ); if (! $result) { die "Invalid option specifications"; } on two different platforms (OS X and Ubuntu, both perl 5.10) and in both cases Getopt is returning 1 (success) _even if_ the supposedly required parameter isn't provided. has this behavior in Getopt changed?