How about something like this:

#!/usr/bin/perl use warnings; use strict; use Getopt::Long; use Pod::Usage; my $Action; # Will be one of: add, edit, list, view my $Value; # Will be the value of the =s part of the option sub set_action { pod2usage( -message => 'Must use only one of --add, --edit, --list, or --view +', ) if defined $Action; $Action = shift; $Value = shift; } GetOptions( 'add|a=s' => \&set_action, 'edit|e=s' => \&set_action, 'list|l=s' => \&set_action, 'view|v=s' => \&set_action, 'man|m' => sub { pod2usage(-exitstatus => 0, -verbose => 2) }, 'help|h|?' => sub { pod2usage(1) }, ) or pod2usage(2); # Update2: Changed back to pod2usage(2) from exit(2) (see below) pod2usage( -message => 'Must use at least one of --add, --edit, --list, or --vi +ew', -exitval => 1, ) unless defined $Action; # Run program { add => \&add_record, edit => \&edit_record, list => \&list_record, view => \&view_record, }->{$Action}->($Value); sub add_record { my $record = shift; print "Adding record $record.\n"; } sub edit_record { my $record = shift; print "Editing record $record.\n"; } sub list_record { my $record = shift; print "Listing record $record.\n"; } sub view_record { my $record = shift; print "Viewing record $record.\n"; } =head1 NAME Program Name - description =head1 SYNOPSIS program [--man] [--help] --add|--edit|--list|--view RECORD -a, --add RECORD Add a record -e, --edit RECORD Edit a record -l, --list RECORD List record(s) -v, --view RECORD View a record -h, -?, --help Display this help screen -m, --man Display the program's manual Note, you must include one (and only one) of add, edit, list, or view. =head1 DESCRIPTION Detailed program description. =head1 AUTHOR The Author =cut

When I tested this, the pod2usage stuff didn't work at all. I think, however, it's an issue with my installation of Pod::Usage, and the option-related part of this code does work so I am posting it anyway. If you also have a problem with pod2usage not working with this, or similar, code, let me know so I can try to determine where the issue lies.

Update: Made a minor update to the code. Noted in comment.

Update2: Ok, my problems with pod2usage are definately just an issue with my cygwin install of Pod::Usage. I tested the above code with my ActivePerl install, and it works fine. Also, I was mistaken about GetOptions not allowing pod2usage to die properly, so I changed the code back.

bbfu
Black flowers blossom
Fearless on my breath


In reply to Re: More on Processing Command Line Arguments by bbfu
in thread More on Processing Command Line Arguments by Ninthwave

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.