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

I have this script that I am working on where I need to accept a number of supplier numbers (1 or more). Is there a way to use the getopts so that I don't have to type -supplier in front of each one of them? If I have 10 suppliers, this gets to be quite cumbersome.
#!/usr/bin/perl use strict; use Getopt::Long; my ($dealer, @supplier, $altsupplier, $min); GetOptions("dealer=s" => \$dealer, "supplier:s" => \@supplier, "altsupplier:s" => \$altsupplier, "min=s" => \$min) or usage(); print "\n"; print "You have entered:\n\n"; print "Dealer = $dealer\n"; print "Supplier = @supplier\n" if @supplier; print "Alt Supplier = $altsupplier\n" if $altsupplier; print "Min = $min\n"; # # Message about this program and how to use it # sub usage() { print STDERR << "EOF"; usage: $0 [-dealer -supplier -altsupplier -min] -h : this (help) message -dealer : dealer to run for -supplier : supplier code -altsupplier : alternate supplier code -min : minimum name example: $0 -dealer A875 -supplier CM CX -min TEST123 EOF exit; }

Replies are listed 'Best First'.
Re: getOpts help
by halley (Prior) on Jun 22, 2007 at 15:46 UTC
    Why not just parse some comma separation on your own, after the fact?
    example: $0 -dealer A875 -suppliers CM,CX -suppliers CD,CZ,CA -min TES +T123
    @suppliers = split(/,/, join(',', @suppliers));
    This exact solution is in the perldoc for Getopt::Long which I see on my machine, when discussing \@foo arrays.

    --
    [ e d @ h a l l e y . c c ]

Re: getOpts help
by moritz (Cardinal) on Jun 22, 2007 at 16:44 UTC
    You could treat every command line argument that's not an option (and doesn't belong to one) as a supplier.

    Somehow I think we had a similar discussion here the other day...