#!/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
|