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

Dear Monks, I'm currently trying to add functionality to a program, my program is called mkpq and I'd like to add certain options such as mkpq -a <queuename>. I'm wondering if this is better off run as a custom subroutine or defined in @ARGV, here is what I have so far as in printing the command usage but I do not know how to reference the commands and store them for later use in the program.
$VERSION = 0.01; $uname = "uname"; # Set ARGS if (@ARGV < 1) { die "usage: $0 \n -a <queuename> -- Add a queue\n -r <queuename> -- Remove a queue\n -s <queuename> -- Check queue/spooler status\n -f <queuename> -- Force option\n -h print this help file\n"; }
Any help at all would be greatly appreciated!

Update!
#!/usr/bin/perl -w use strict; use Getopt::Long; my ($add,$remove,$status,$force); my %add_opts; GetOptions ( \%add_opts, 'queue=s', 'label=s' ); sub display_help { die "Usage: -a <queuename> -- Add a queue\n -r <queuename> -- Remove a queue\n -s <queuename> -- Check queue/spooler status\n -f <queuename> -- Force option\n -v Version Information\n -h print this help file\n"; } GetOptions ( version => sub{ print "This is mkpq Version 0.01\n"; exit +; } ); GetOptions ( help => \&display_help ); if ($add_opts {'queue'}) { print "Test\n"; } else { print &display_help; }
First of all, thanks for all of the help everyone has provided. I do in fact have another question; since this program needs to be a bit intuitive. My initial thought process behind this program would be:

1.) The first switch will determine the course of the program. (mkpq -a or -r) I was going to have if statements for -a or -r, then sub-switches if you will.

Example mkpa -a -q <printername> (the a switch invokes the system commmand to add a printer -q will hold the string which is the name of the printer.

My question is this: How to I set this up in such a manner? Should I determine if () with subprocess' running under it?
if (a) { # switch to add a queue do system($somecmd $q); #run system cmd with entered string.
}

Replies are listed 'Best First'.
Re: Questions about adding arguements to a program.
by toolic (Bishop) on Dec 10, 2007 at 20:56 UTC
    The Getopt::Std and Getopt::Long standard Perl modules are quite useful for processing command-line arguments. Does that answer your question?
Re: Questions about adding arguements to a program.
by tuxz0r (Pilgrim) on Dec 10, 2007 at 20:57 UTC
    There are a number of Getopt packages to look at depending on what you need. Currently, our group uses the Getopt::Long package for GNU style long options (e.g. --longword=XXX style) which we find our users have an easier time remembering than just the short options. However, Getopt::Long will handle both together so you can combine a short with a longer descriptive option name, too.

    Getopt::Long

    ---
    s;;:<).>|\;\;_>?\\^0<|=!]=,|{\$/.'>|<?.|/"&?=#!>%\$|#/\$%{};;y;,'} -/:-@[-`{-};,'}`-{/" -;;s;;$_;see;
    Warning: Any code posted by tuxz0r is untested, unless otherwise stated, and is used at your own risk.

Re: Questions about adding arguements to a program.
by dwm042 (Priest) on Dec 10, 2007 at 21:36 UTC
    Don't hand code a usage function. You can use Getopt::Long and Pod::Usage and you not only have a usage function, you also have functional documentation. The recommended usage in Pod::Usage is excellent. There are a lot of roads to Rome here, but the way I like to do it is shown here.
Re: Questions about adding arguements to a program.
by naChoZ (Curate) on Dec 10, 2007 at 21:04 UTC

    I posted a Getopt::Long example here just recently. Check it out.

    --
    naChoZ

    Therapy is expensive. Popping bubble wrap is cheap. You choose.

Re: Questions about adding arguments to a program.
by Roy Johnson (Monsignor) on Dec 10, 2007 at 20:53 UTC
    Have a look at getopt.

    Caution: Contents may have been coded under pressure.
Re: Questions about adding arguements to a program.
by ian (Beadle) on Dec 11, 2007 at 08:51 UTC