in reply to Re^2: Parsing options with Getopt & Dispatch Table
in thread Parsing options with Getopt & Dispatch Table
Getopt::Long has lots of configuration options that have their own defaults, but there's no option to define a default option and default value/behavior for it. You need to do that for yourself, but Getopt::Long can help. One of its default behaviors is to flag as errors “Options that are unknown, ambiguous or supplied with an invalid option value” (see the pass_through configuration option).
So if you do nothing special, GetOptions() will tell you when you use an unknown option on the command line.
Using cjb's sample as a start, this shows what happens when you include an unknown option on the command line:
And run like so (only defined options):#!/usr/bin/env perl use 5.010; use strict; use warnings; use Getopt::Long; my $result = GetOptions ( h => \&help, d => sub {print "\nRun DEV COMPARE\n";}, p => \&PROD, ); say "result: $result"; bad_option() unless $result; say "\ncontinuing normally"; say "Doing stuff....."; say "exiting normally"; sub PROD {print "\nRun Production COMPARE\n";} sub help {print "\nHelp text\n";} sub bad_option { print "\nInvalid option detected... Quitting!!!\n"; exit 1; }
./pm-888730 -h -d -p Help text Run DEV COMPARE Run Production COMPARE result: 1 continuing normally Doing stuff..... exiting normally
or thusly (one defined and one undefined option):
./pm-888730 -d -x Run DEV COMPARE Unknown option: x result: Invalid option detected... Quitting!!!
Comments:
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^4: Parsing options with Getopt & Dispatch Table
by MKJ747 (Acolyte) on Feb 22, 2011 at 13:30 UTC |