Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Getopt Behavior

by gaspodethewonderdog (Monk)
on Dec 05, 2000 at 22:52 UTC ( [id://45051]=perlquestion: print w/replies, xml ) Need Help??

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

I've been looking through the Getopt::Long perlman page and Getopt::Std perlman page as well and haven't come to the answer I want. That either means that what I want to do is impossible with Getopt or I'm looking in the wrong place altogether.

What I want to do is have a command line driven program that can take multiple inputs for a given id and process them. What I sort of want to do is:

use Getopt::Long; use strict; use vars qw/@opt_id/; GetOptions("id=s@"); print join "|", @opt_id;
However I want the id field to be an array delimited by spaces... I want the user to type "<command> -id element1 element2 -status abc" and not "<command> -id element1 -id element2 -status abc". Is there a way to put Getopt into a mode to do this? Can somebody throw me a bone here?

Replies are listed 'Best First'.
Re: Getopt Behavior
by Fastolfe (Vicar) on Dec 05, 2000 at 23:56 UTC
    Note that this is a non-standard way of doing command line options. That's why Getopt::Long is having trouble recognizing what you want to do. You may have better luck changing the syntax of your script so that it supports something like this:
    $ script --id="1.0.0.1 1.0.0.2 1.0.0.3" # split the resulting variable on spaces $ cat >id.dat 1.0.0.1 1.0.0.2 1.0.0.3 $ script --id=@id.dat
    In addition, if your script takes no "filename"-like arguments (arguments not preceeded by an option), consider making IP addresses an optional argument in this respect:
    Usage: script.pl [-abcde] [ip_address [ip_address ...]]
    Or, if your script does depend on, say, one additional argument, consider putting that in a flag, either making that option mandatory or using a suitable default (like stdin for a file), or just add it to the list of non-option arguments:
    Usage: script.pl [-abcde] -f filename [ip_address [ip_address ...]] Usage: script.pl [-abcde] filename [ip_address [ip_address ...]]
    Multiple arguments after a single option tag is kind of strange for people used to "standard" command line options for scripts and utilities. I would think about re-working the interface instead of trying to force Getopt to play by these other rules.
      Non-standard? Getopt::Long supports this with no problems. Here's a snippet from the POD:
      Example of using variable references: $ret = GetOptions ('foo=s', \$foo, 'bar=i', 'ar=s', \@ar); With command line options ``-foo blech -bar 24 -ar xx -ar yy'' this wi +ll result in: $foo = 'blech' $opt_bar = 24 @ar = ('xx','yy')

      Looks to me like Getopt should have no problem with the original question. gaspodethewonderdog just needs to change the variable into a reference.

      Update: MeowChow is right - the example does use the -ar multiple times instead of just once. However, I do know that it can be done, since I have a working example.

      Guildenstern
      Negaterd character class uber alles!
        In this example, the -ar switch must be specified once for each list element, which is exactly what he wants to avoid. He's looking for a syntax which will understand: -foo blech -bar 24 -ar xx yy
(Guildenstern) Re: Getopt Behavior
by Guildenstern (Deacon) on Dec 05, 2000 at 23:01 UTC
    Here's an example from some code that I've written. This will allow the user to specify multiple IP addresses without having to use the command line switch multiple times.
    use Getopt::Long; GetOptions("i:s",\@ignored_IPs);

    This lets me say "-i 1.1.1.1 2.2.2.2" etc and all of the IPs are stored in the @ignored_IPs array. AFAIK, only Getopt::Long does this.

    Update: After taking a better look at your code, it looks like all you really should have to do is make your variable name that will hold the options into a reference.

    Guildenstern
    Negaterd character class uber alles!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://45051]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-19 21:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found