Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: better cli processing

by flarg (Initiate)
on Nov 02, 2002 at 15:52 UTC ( [id://209963]=note: print w/replies, xml ) Need Help??


in reply to better cli processing

On a related note, the Getopt::Long node says I can specify an option like 'foo --size=24' like this:

>>> GetOptions("size i" => \$offset); <<<

How can I use this feature to specify an option that may look like either of these:

foo --size 24 --verbose --debug

or a more compact version

-vds 24

I can't quite figure it out. The documentation contains alot of examples using references, and I references are a little beyond me right now (but I'm learning!).

Replies are listed 'Best First'.
(bbfu) (hash, bundling, aliases) Re(2): better cli processing
by bbfu (Curate) on Nov 03, 2002 at 04:33 UTC

    A couple of things you will want to read up on.

    First, you should look into storing the options in a hash, instead of passing a reference to a variable for each one. It's much easier. See Storing options in a hash for info on that.

    Second is aliases, or Options_with_multiple_names. Pretty straight-forward.

    Last, you want to look into Bundling for info on allowing the -vds 24 form.

    And here's some example code!

    #!/usr/bin/perl use warnings; use strict; use Getopt::Long; Getopt::Long::Configure('bundling'); our %Options; GetOptions(\%Options, 'size|s=i', # The | separates aliases, ie: s == size 'verbose|v', # What comes before the | is the name, which 'debug|d', # determines what hash entry (or variable) is +set. ); print "Debug mode\n" if $Options{debug}; print "Verbose mode\n" if $Options{verbose}; print "Size is: `$Options{size}'\n" if exists $Options{size}; print "Done.\n";

    This will take --verbose --size=24, or --size 24 -vd, or -vds24, or just about any other combination thereof. :)

    bbfu
    Black flowers blossum
    Fearless on my breath

      Beautiful. That's exactly what I was looking for. Thanks all!
Re^2: better cli processing
by Aristotle (Chancellor) on Nov 02, 2002 at 21:48 UTC
    It's not possible with Getopt::Long. You can only use long options with that, and you can only use short options with Getopt::Std. What you want to do requires Getopt::Mixed.

    Makeshifts last the longest.

      Actually, I believe it is possible with Getopt::Long. Take a look at the bundling option.

      Update: Oh, and of course it helps to use the 'name|n' for defining the short form, since it's not done automatically. :)

      bbfu
      Black flowers blossum
      Fearless on my breath

        I stand corrected. Thanks!

        Makeshifts last the longest.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-25 14:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found