Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Command Line Arguments

by Adam (Vicar)
on Jul 07, 2000 at 01:23 UTC ( [id://21392]=perlquestion: print w/replies, xml ) Need Help??

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

I know I can easily process command line arguments with Getopt::Long or Getopt::Std but I want to work a sort of combination. Is there a better module or should I just do my own dirty work?

To be more specific I have a set of about five or six arguments which have distinct first letters. I want people to be able to use this tool using single letter arguments for convenience, but this tool will also be called by other scripts, and those I want to have use the longer, self documenting, arguments. Any ideas? Suggestions?

Replies are listed 'Best First'.
Re: Command Line Arguments
by ZZamboni (Curate) on Jul 07, 2000 at 06:35 UTC
    Getopt::Long can have single-letter aliases for the long arguments. You separate the aliases with "|" in the argument specification. So for example if you say:
    GetOptions('copies|n=i' => \$copies);
    You will be able to use all of:
    -n 5 --copies=5 --copies 5
    as arguments.


      Thats cool. I didn't see that in the documentation. Sigh. I already wrote my own function to parse the command line aliasing the commands with single chars and allowing the grouping of single chars so you can say -bc instead of -b -c. That was this afternoon while PM was down. Now I don't know if I want to go back and replace that with Getopt::Long... We'll see. Plus my parser recognizes '/' as an alternative parameter lead in addition to '-'... sigh.

      Thanks ZZamboni

Re: Command Line Arguments
by chip (Curate) on Jul 07, 2000 at 21:02 UTC
    FYI, rolling your own Getopt::Std might look like:
    while (@ARGV) { $_ = $ARGV[0]; last unless s/^-//; shift; last if /^-$/; while (length) { if (s/^a//) { ++$aflag } elsif (s/^b(.*)//) { $bflag = length($1) ? $1 : shift } else { die "Unknown flag(s): -$_\n" } } }
Don't forget
by gryng (Hermit) on Jul 07, 2000 at 07:46 UTC
    Also -n5 works as well.

    (Ok I admit it, I'm only writing this for the experience point! no no.. don't, don't hit that negative radio button! Ahhh!)

    Afterall, I did read the entire Getopt::Long manual before I posted this to see if there was anything else useful for Adam. There were quite a few new features implemented since I last read it, it's quite -kewwwl- (as Eric Cartman would say).

      I like your honesty. Quite a virtue of a PerlMonk. =) Shalom.


      p.s. The Legend Will Never Die!
Re: Command Line Arguments
by hel0 (Novice) on Jul 07, 2000 at 18:40 UTC
    I would do something like this:
    my %ArgumentState(); foreach (@ARGV) { SWITCH: for ($_) { /^a/i && do { $ArgumentState{'foo'}='bar'; last; }; /^b/i && do { &Do_something_here; last; }; }; };
    But it's all just a matter of personal preference I guess. I don't like creating objects for simple things like parsing the commandline. Regular expressions will also offer you much greater flexibility! Hel0

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-05-26 02:35 GMT
Find Nodes?
    Voting Booth?

    No recent polls found