http://www.perlmonks.org?node_id=79126

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

I have tried various ways of doing command line options, such as sub expressions and using the Getopt modules. However, I'd like to know how my fellow monks go about doing this.
Thanks for any feedback.

Edit: chipmunk 2001-05-09

Replies are listed 'Best First'.
Re: Command Line Options
by isotope (Deacon) on May 09, 2001 at 20:00 UTC
    If I'm going to bother with command line options, I almost always use Getopt::Long. It's big, but it works quite well (I really like that it removes the options from @ARGV so I can use <> later, and that it can do some validation of the options, etc).

    --isotope
    http://www.skylab.org/~isotope/
Re: Command Line Options
by japhy (Canon) on May 09, 2001 at 22:44 UTC
    I like -s. It lets me do things like:
    #!/usr/bin/perl -ws use strict; our ($name, $age, $sex); # ...
    and run the code like: prog.pl -name="Jeff Pinyan" -sex=male -age=19. I even got 10 characters or so added to Perl's source code to allow double-hyphens (if you're into that sort of thing).

    japhy -- Perl and Regex Hacker
Re: Command Line Options
by frag (Hermit) on May 09, 2001 at 23:39 UTC
    I like Getopt::Mixed, which lets you use either "-abc=foo", "--alpha --beta --cletus=foo", or, "--alpha -bc=foo".
    ### EZ way: use Getopt::Mixed; Getopt::Mixed::getOptions qw( help h>help file f>file list=i l>list ); print "$opt_help, $opt_file, $opt_list"; ### 'Harder' way (more flexible, but you have more to write): use Getopt::Mixed "nextOption"; Getopt::Mixed::init qw( help h>help file f>file id=i i>id ); my ($opt_id, $opt_file, $opt_help); while (($option, $value, $pretty) = nextOption()) { if ($option eq 'id') { $opt_id = $value; } elsif ($option eq 'help') { # etc. } }

    -- Frag.

Re: Command Line Options
by robsv (Curate) on May 09, 2001 at 21:10 UTC
    I'm with isotope. I find that command line parms with longer names are easier to remember. I generally use Getopt::Long something like this:
    GetOptions("file=s" => \my $input_file, "verbose" => \$VERBOSE, "help" => \my $HELP) or die "Insert usage message here";
    Getopt::Long is also flexible enough to allow bundling of single-character options like Getopt::Std.

    - robsv