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

Using Getopt::Long, I would like to have a non-option argument which is unaffected by surrounding options (my options in this case will be mostly switches).

The way I usually handle options is like this:

my %opt =( shell => "bash", foo => "/some/path", input => undef, # yes, i know this doesn't need to be here ); GetOptions(\%opt, 'shell=s', 'foo=s', 'bip', 'bar', 'baz' );

How can I integrate a non-option argument in this code? Preferably, I would like the argument to be set as $opt{input} so I can act on it one way or another depending on which of the options (bip, bar, baz, or nothing at all) is selected.

Replies are listed 'Best First'.
Re: non-option argument question
by ikegami (Patriarch) on Sep 12, 2012 at 05:16 UTC
    sub usage { print STDERR $_[0] if @_; print STDERR "usage: ...\n"; exit(1); } GetOptions(\%opt, 'shell=s', 'foo=s', 'bip', 'bar', 'baz' ) or usage(); defined($opt{input}) or usage("Argument 'input' is required\n");
Re: non-option argument question
by kcott (Archbishop) on Sep 12, 2012 at 05:23 UTC

    G'day dr_jkl,

    Probably a typo: I'll assume my $opt should be my %opt.

    If %opt is in scope, you can modify it whenever you want. So, the question seems to boil down to how to get the value to assign to $opt{input}.

    The Getopt::Long documentation has a Mixing command line option with other arguments section. Is this of any help?

    An example of what you want to type on the command line would be useful.

    I'm guessing your code looks something like:

    my %opt = ( ... ); GetOptions( ... ); # get value for $opt{input} here if ($opt{input} eq 'something') { # do something here } else { # do something else here }

    Filling in the blanks (or correcting my guess) would probably lead to a better answer.

    -- Ken

      Yes! It should be %opt, and I've corrected it above. Sorry, it's somewhat late here. :)

      I would like to be able to do something like:

       foo.pl ex-post-facto or  foo.pl -v ex-post-facto where ex-post-facto is  $opt{input} and then I can decide what to do with the input based on what switch(es) were passed.

        Beyond the assignment to $opt{input}, your requirement seems to be for the normal operaton of Getopt::Long.

        Consider this code:

        #!/usr/bin/env perl use 5.010; use strict; use warnings; use Getopt::Long; my %opt; GetOptions(\%opt, 'v'); say 'Before assigning $opt{input}:'; if ($opt{v}) { say $ARGV[0], ' (verbose)'; } else { say $ARGV[0], ' (quiet)'; } $opt{input} = $ARGV[0]; say 'After assigning $opt{input}:'; if ($opt{v}) { say $opt{input}, ' (verbose)'; } else { say $opt{input}, ' (quiet)'; }

        Using your two command line examples, this produces:

        $ pm_getopt_arg.pl ex-post-facto Before assigning $opt{input}: ex-post-facto (quiet) After assigning $opt{input}: ex-post-facto (quiet)
        $ pm_getopt_arg.pl -v ex-post-facto Before assigning $opt{input}: ex-post-facto (verbose) After assigning $opt{input}: ex-post-facto (verbose)

        -- Ken