Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Passing Options

by fruiture (Curate)
on Aug 12, 2002 at 14:30 UTC ( [id://189504]=note: print w/replies, xml ) Need Help??


in reply to Passing Options

check Getopt::Long!

use Getopt::Long; our @realeases; GetOptions( 't=s' => \ our $target, 'b=s' => \ our $buildtype, 'r=s' => \ @releases );
--
http://fruiture.de

Replies are listed 'Best First'.
Re: Passing Options
by Abigail-II (Bishop) on Aug 12, 2002 at 15:04 UTC
    check Getopt::Long!
    I just did. I cannot find anywhere how to tell Getopt::Long to parse the command line as wished.
    GetOptions( 't=s' => \ our $target, 'b=s' => \ our $buildtype, 'r=s' => \ @releases );
    That requires multiple -t, -b and -r arguments. From a command line of
    -r foo bar baz -b dog cat -t pony
    it will put foo in @releases, dog in $buildtype, pony in $target, and leave the rest in @ARGV.
    use Getopt::Long; our @releases; GetOptions( 't=s' => \ our $target, 'b=s' => \ our $buildtype, 'r=s' => \ @releases ); print "Target: $target\n"; print "Buildtype: $buildtype\n"; print "Releases: @releases\n"; print "ARGV: @ARGV\n"; __END__ Target: pony Buildtype: dog Releases: foo ARGV: bar baz cat
    Not at all as wished.

    I don't know any module that will parse the command line as was wished, but it shouldn't be too hard to roll your own.

    Abigail

      Probably the "originally wanted" was only abstract: "Pass more than one value for a commandline option". Why should anybody write a new module that parses the options in a way that differs from nearly all other applications, if he could just change the own program's usage message and make it work with standard modules and methods?

      --
      http://fruiture.de
        Perhaps, but why make such broad suggestions if the example is pretty clear? At least have the decency to write in your reply that your suggestion is violating the given example. Give the readers a hint that you actually thought about the question and answer and that you didn't just try to get a answer - any anwer - just to say something.

        I don't know why anyone whould write a new module, and I don't know what that has to do with the answer. But now we are getting something like:

        • Poster: "How do I do X"? Here's an example.
        • Reply: Use Y!
        • Me: Y doesn't solve X, here look, it does Z.
        • You: Yeah, but perhaps the poster wanted to do Z.
        I'd think this forum would quickly become useless if all threads go this way.

        If you have a solution that doesn't solve the problem, but solves a different one, say so. Don't suggest it solves the problem. That only wastes a lot of time of people.

        (As for wanting options to take multiple arguments, without having to repeat the option, I think that's reasonable. I've wanted that in the past as well. The fact that there's a module to parse options in a different way doesn't invalidate the wish to do it otherwise. This is Perl remember, not Python).

        Abigail

Re: Re: Passing Options
by trs80 (Priest) on Aug 12, 2002 at 17:14 UTC
    I would change the \@release into \$release and use : colon or pipe separated values to send multiple arguments without repeating the option. Then simply create your array with a split.
    use Getopt::Long; our $realeases; GetOptions( 't=s' => \ our $target, 'b=s' => \ our $buildtype, 'r=s' => \ $releases ); our @releases = split(/\:/,$releases);
    Program is called with:
    perl script.pl -r 1:2:3 -b blah -t blah

Log In?
Username:
Password:

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

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

    No recent polls found