xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/lib/Getopt/Long.pm (revision 0:68f95e015346)
1*0Sstevel@tonic-gate# Getopt::Long.pm -- Universal options parsing
2*0Sstevel@tonic-gate
3*0Sstevel@tonic-gatepackage Getopt::Long;
4*0Sstevel@tonic-gate
5*0Sstevel@tonic-gate# RCS Status      : $Id: GetoptLong.pm,v 2.68 2003-09-23 15:24:53+02 jv Exp $
6*0Sstevel@tonic-gate# Author          : Johan Vromans
7*0Sstevel@tonic-gate# Created On      : Tue Sep 11 15:00:12 1990
8*0Sstevel@tonic-gate# Last Modified By: Johan Vromans
9*0Sstevel@tonic-gate# Last Modified On: Tue Sep 23 15:21:23 2003
10*0Sstevel@tonic-gate# Update Count    : 1364
11*0Sstevel@tonic-gate# Status          : Released
12*0Sstevel@tonic-gate
13*0Sstevel@tonic-gate################ Copyright ################
14*0Sstevel@tonic-gate
15*0Sstevel@tonic-gate# This program is Copyright 1990,2002 by Johan Vromans.
16*0Sstevel@tonic-gate# This program is free software; you can redistribute it and/or
17*0Sstevel@tonic-gate# modify it under the terms of the Perl Artistic License or the
18*0Sstevel@tonic-gate# GNU General Public License as published by the Free Software
19*0Sstevel@tonic-gate# Foundation; either version 2 of the License, or (at your option) any
20*0Sstevel@tonic-gate# later version.
21*0Sstevel@tonic-gate#
22*0Sstevel@tonic-gate# This program is distributed in the hope that it will be useful,
23*0Sstevel@tonic-gate# but WITHOUT ANY WARRANTY; without even the implied warranty of
24*0Sstevel@tonic-gate# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25*0Sstevel@tonic-gate# GNU General Public License for more details.
26*0Sstevel@tonic-gate#
27*0Sstevel@tonic-gate# If you do not have a copy of the GNU General Public License write to
28*0Sstevel@tonic-gate# the Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
29*0Sstevel@tonic-gate# MA 02139, USA.
30*0Sstevel@tonic-gate
31*0Sstevel@tonic-gate################ Module Preamble ################
32*0Sstevel@tonic-gate
33*0Sstevel@tonic-gateuse 5.004;
34*0Sstevel@tonic-gate
35*0Sstevel@tonic-gateuse strict;
36*0Sstevel@tonic-gate
37*0Sstevel@tonic-gateuse vars qw($VERSION);
38*0Sstevel@tonic-gate$VERSION        =  2.34;
39*0Sstevel@tonic-gate# For testing versions only.
40*0Sstevel@tonic-gate#use vars qw($VERSION_STRING);
41*0Sstevel@tonic-gate#$VERSION_STRING = "2.33_03";
42*0Sstevel@tonic-gate
43*0Sstevel@tonic-gateuse Exporter;
44*0Sstevel@tonic-gateuse vars qw(@ISA @EXPORT @EXPORT_OK);
45*0Sstevel@tonic-gate@ISA = qw(Exporter);
46*0Sstevel@tonic-gate
47*0Sstevel@tonic-gate# Exported subroutines.
48*0Sstevel@tonic-gatesub GetOptions(@);		# always
49*0Sstevel@tonic-gatesub Configure(@);		# on demand
50*0Sstevel@tonic-gatesub HelpMessage(@);		# on demand
51*0Sstevel@tonic-gatesub VersionMessage(@);		# in demand
52*0Sstevel@tonic-gate
53*0Sstevel@tonic-gateBEGIN {
54*0Sstevel@tonic-gate    # Init immediately so their contents can be used in the 'use vars' below.
55*0Sstevel@tonic-gate    @EXPORT    = qw(&GetOptions $REQUIRE_ORDER $PERMUTE $RETURN_IN_ORDER);
56*0Sstevel@tonic-gate    @EXPORT_OK = qw(&HelpMessage &VersionMessage &Configure);
57*0Sstevel@tonic-gate}
58*0Sstevel@tonic-gate
59*0Sstevel@tonic-gate# User visible variables.
60*0Sstevel@tonic-gateuse vars @EXPORT, @EXPORT_OK;
61*0Sstevel@tonic-gateuse vars qw($error $debug $major_version $minor_version);
62*0Sstevel@tonic-gate# Deprecated visible variables.
63*0Sstevel@tonic-gateuse vars qw($autoabbrev $getopt_compat $ignorecase $bundling $order
64*0Sstevel@tonic-gate	    $passthrough);
65*0Sstevel@tonic-gate# Official invisible variables.
66*0Sstevel@tonic-gateuse vars qw($genprefix $caller $gnu_compat $auto_help $auto_version);
67*0Sstevel@tonic-gate
68*0Sstevel@tonic-gate# Public subroutines.
69*0Sstevel@tonic-gatesub config(@);			# deprecated name
70*0Sstevel@tonic-gate
71*0Sstevel@tonic-gate# Private subroutines.
72*0Sstevel@tonic-gatesub ConfigDefaults();
73*0Sstevel@tonic-gatesub ParseOptionSpec($$);
74*0Sstevel@tonic-gatesub OptCtl($);
75*0Sstevel@tonic-gatesub FindOption($$$$);
76*0Sstevel@tonic-gate
77*0Sstevel@tonic-gate################ Local Variables ################
78*0Sstevel@tonic-gate
79*0Sstevel@tonic-gate# $requested_version holds the version that was mentioned in the 'use'
80*0Sstevel@tonic-gate# or 'require', if any. It can be used to enable or disable specific
81*0Sstevel@tonic-gate# features.
82*0Sstevel@tonic-gatemy $requested_version = 0;
83*0Sstevel@tonic-gate
84*0Sstevel@tonic-gate################ Resident subroutines ################
85*0Sstevel@tonic-gate
86*0Sstevel@tonic-gatesub ConfigDefaults() {
87*0Sstevel@tonic-gate    # Handle POSIX compliancy.
88*0Sstevel@tonic-gate    if ( defined $ENV{"POSIXLY_CORRECT"} ) {
89*0Sstevel@tonic-gate	$genprefix = "(--|-)";
90*0Sstevel@tonic-gate	$autoabbrev = 0;		# no automatic abbrev of options
91*0Sstevel@tonic-gate	$bundling = 0;			# no bundling of single letter switches
92*0Sstevel@tonic-gate	$getopt_compat = 0;		# disallow '+' to start options
93*0Sstevel@tonic-gate	$order = $REQUIRE_ORDER;
94*0Sstevel@tonic-gate    }
95*0Sstevel@tonic-gate    else {
96*0Sstevel@tonic-gate	$genprefix = "(--|-|\\+)";
97*0Sstevel@tonic-gate	$autoabbrev = 1;		# automatic abbrev of options
98*0Sstevel@tonic-gate	$bundling = 0;			# bundling off by default
99*0Sstevel@tonic-gate	$getopt_compat = 1;		# allow '+' to start options
100*0Sstevel@tonic-gate	$order = $PERMUTE;
101*0Sstevel@tonic-gate    }
102*0Sstevel@tonic-gate    # Other configurable settings.
103*0Sstevel@tonic-gate    $debug = 0;			# for debugging
104*0Sstevel@tonic-gate    $error = 0;			# error tally
105*0Sstevel@tonic-gate    $ignorecase = 1;		# ignore case when matching options
106*0Sstevel@tonic-gate    $passthrough = 0;		# leave unrecognized options alone
107*0Sstevel@tonic-gate    $gnu_compat = 0;		# require --opt=val if value is optional
108*0Sstevel@tonic-gate}
109*0Sstevel@tonic-gate
110*0Sstevel@tonic-gate# Override import.
111*0Sstevel@tonic-gatesub import {
112*0Sstevel@tonic-gate    my $pkg = shift;		# package
113*0Sstevel@tonic-gate    my @syms = ();		# symbols to import
114*0Sstevel@tonic-gate    my @config = ();		# configuration
115*0Sstevel@tonic-gate    my $dest = \@syms;		# symbols first
116*0Sstevel@tonic-gate    for ( @_ ) {
117*0Sstevel@tonic-gate	if ( $_ eq ':config' ) {
118*0Sstevel@tonic-gate	    $dest = \@config;	# config next
119*0Sstevel@tonic-gate	    next;
120*0Sstevel@tonic-gate	}
121*0Sstevel@tonic-gate	push(@$dest, $_);	# push
122*0Sstevel@tonic-gate    }
123*0Sstevel@tonic-gate    # Hide one level and call super.
124*0Sstevel@tonic-gate    local $Exporter::ExportLevel = 1;
125*0Sstevel@tonic-gate    push(@syms, qw(&GetOptions)) if @syms; # always export GetOptions
126*0Sstevel@tonic-gate    $pkg->SUPER::import(@syms);
127*0Sstevel@tonic-gate    # And configure.
128*0Sstevel@tonic-gate    Configure(@config) if @config;
129*0Sstevel@tonic-gate}
130*0Sstevel@tonic-gate
131*0Sstevel@tonic-gate################ Initialization ################
132*0Sstevel@tonic-gate
133*0Sstevel@tonic-gate# Values for $order. See GNU getopt.c for details.
134*0Sstevel@tonic-gate($REQUIRE_ORDER, $PERMUTE, $RETURN_IN_ORDER) = (0..2);
135*0Sstevel@tonic-gate# Version major/minor numbers.
136*0Sstevel@tonic-gate($major_version, $minor_version) = $VERSION =~ /^(\d+)\.(\d+)/;
137*0Sstevel@tonic-gate
138*0Sstevel@tonic-gateConfigDefaults();
139*0Sstevel@tonic-gate
140*0Sstevel@tonic-gate################ OO Interface ################
141*0Sstevel@tonic-gate
142*0Sstevel@tonic-gatepackage Getopt::Long::Parser;
143*0Sstevel@tonic-gate
144*0Sstevel@tonic-gate# Store a copy of the default configuration. Since ConfigDefaults has
145*0Sstevel@tonic-gate# just been called, what we get from Configure is the default.
146*0Sstevel@tonic-gatemy $default_config = do {
147*0Sstevel@tonic-gate    Getopt::Long::Configure ()
148*0Sstevel@tonic-gate};
149*0Sstevel@tonic-gate
150*0Sstevel@tonic-gatesub new {
151*0Sstevel@tonic-gate    my $that = shift;
152*0Sstevel@tonic-gate    my $class = ref($that) || $that;
153*0Sstevel@tonic-gate    my %atts = @_;
154*0Sstevel@tonic-gate
155*0Sstevel@tonic-gate    # Register the callers package.
156*0Sstevel@tonic-gate    my $self = { caller_pkg => (caller)[0] };
157*0Sstevel@tonic-gate
158*0Sstevel@tonic-gate    bless ($self, $class);
159*0Sstevel@tonic-gate
160*0Sstevel@tonic-gate    # Process config attributes.
161*0Sstevel@tonic-gate    if ( defined $atts{config} ) {
162*0Sstevel@tonic-gate	my $save = Getopt::Long::Configure ($default_config, @{$atts{config}});
163*0Sstevel@tonic-gate	$self->{settings} = Getopt::Long::Configure ($save);
164*0Sstevel@tonic-gate	delete ($atts{config});
165*0Sstevel@tonic-gate    }
166*0Sstevel@tonic-gate    # Else use default config.
167*0Sstevel@tonic-gate    else {
168*0Sstevel@tonic-gate	$self->{settings} = $default_config;
169*0Sstevel@tonic-gate    }
170*0Sstevel@tonic-gate
171*0Sstevel@tonic-gate    if ( %atts ) {		# Oops
172*0Sstevel@tonic-gate	die(__PACKAGE__.": unhandled attributes: ".
173*0Sstevel@tonic-gate	    join(" ", sort(keys(%atts)))."\n");
174*0Sstevel@tonic-gate    }
175*0Sstevel@tonic-gate
176*0Sstevel@tonic-gate    $self;
177*0Sstevel@tonic-gate}
178*0Sstevel@tonic-gate
179*0Sstevel@tonic-gatesub configure {
180*0Sstevel@tonic-gate    my ($self) = shift;
181*0Sstevel@tonic-gate
182*0Sstevel@tonic-gate    # Restore settings, merge new settings in.
183*0Sstevel@tonic-gate    my $save = Getopt::Long::Configure ($self->{settings}, @_);
184*0Sstevel@tonic-gate
185*0Sstevel@tonic-gate    # Restore orig config and save the new config.
186*0Sstevel@tonic-gate    $self->{settings} = Getopt::Long::Configure ($save);
187*0Sstevel@tonic-gate}
188*0Sstevel@tonic-gate
189*0Sstevel@tonic-gatesub getoptions {
190*0Sstevel@tonic-gate    my ($self) = shift;
191*0Sstevel@tonic-gate
192*0Sstevel@tonic-gate    # Restore config settings.
193*0Sstevel@tonic-gate    my $save = Getopt::Long::Configure ($self->{settings});
194*0Sstevel@tonic-gate
195*0Sstevel@tonic-gate    # Call main routine.
196*0Sstevel@tonic-gate    my $ret = 0;
197*0Sstevel@tonic-gate    $Getopt::Long::caller = $self->{caller_pkg};
198*0Sstevel@tonic-gate
199*0Sstevel@tonic-gate    eval {
200*0Sstevel@tonic-gate	# Locally set exception handler to default, otherwise it will
201*0Sstevel@tonic-gate	# be called implicitly here, and again explicitly when we try
202*0Sstevel@tonic-gate	# to deliver the messages.
203*0Sstevel@tonic-gate	local ($SIG{__DIE__}) = '__DEFAULT__';
204*0Sstevel@tonic-gate	$ret = Getopt::Long::GetOptions (@_);
205*0Sstevel@tonic-gate    };
206*0Sstevel@tonic-gate
207*0Sstevel@tonic-gate    # Restore saved settings.
208*0Sstevel@tonic-gate    Getopt::Long::Configure ($save);
209*0Sstevel@tonic-gate
210*0Sstevel@tonic-gate    # Handle errors and return value.
211*0Sstevel@tonic-gate    die ($@) if $@;
212*0Sstevel@tonic-gate    return $ret;
213*0Sstevel@tonic-gate}
214*0Sstevel@tonic-gate
215*0Sstevel@tonic-gatepackage Getopt::Long;
216*0Sstevel@tonic-gate
217*0Sstevel@tonic-gate################ Back to Normal ################
218*0Sstevel@tonic-gate
219*0Sstevel@tonic-gate# Indices in option control info.
220*0Sstevel@tonic-gate# Note that ParseOptions uses the fields directly. Search for 'hard-wired'.
221*0Sstevel@tonic-gateuse constant CTL_TYPE    => 0;
222*0Sstevel@tonic-gate#use constant   CTL_TYPE_FLAG   => '';
223*0Sstevel@tonic-gate#use constant   CTL_TYPE_NEG    => '!';
224*0Sstevel@tonic-gate#use constant   CTL_TYPE_INCR   => '+';
225*0Sstevel@tonic-gate#use constant   CTL_TYPE_INT    => 'i';
226*0Sstevel@tonic-gate#use constant   CTL_TYPE_INTINC => 'I';
227*0Sstevel@tonic-gate#use constant   CTL_TYPE_XINT   => 'o';
228*0Sstevel@tonic-gate#use constant   CTL_TYPE_FLOAT  => 'f';
229*0Sstevel@tonic-gate#use constant   CTL_TYPE_STRING => 's';
230*0Sstevel@tonic-gate
231*0Sstevel@tonic-gateuse constant CTL_CNAME   => 1;
232*0Sstevel@tonic-gate
233*0Sstevel@tonic-gateuse constant CTL_MAND    => 2;
234*0Sstevel@tonic-gate
235*0Sstevel@tonic-gateuse constant CTL_DEST    => 3;
236*0Sstevel@tonic-gate use constant   CTL_DEST_SCALAR => 0;
237*0Sstevel@tonic-gate use constant   CTL_DEST_ARRAY  => 1;
238*0Sstevel@tonic-gate use constant   CTL_DEST_HASH   => 2;
239*0Sstevel@tonic-gate use constant   CTL_DEST_CODE   => 3;
240*0Sstevel@tonic-gate
241*0Sstevel@tonic-gateuse constant CTL_DEFAULT => 4;
242*0Sstevel@tonic-gate
243*0Sstevel@tonic-gate# FFU.
244*0Sstevel@tonic-gate#use constant CTL_RANGE   => ;
245*0Sstevel@tonic-gate#use constant CTL_REPEAT  => ;
246*0Sstevel@tonic-gate
247*0Sstevel@tonic-gatesub GetOptions(@) {
248*0Sstevel@tonic-gate
249*0Sstevel@tonic-gate    my @optionlist = @_;	# local copy of the option descriptions
250*0Sstevel@tonic-gate    my $argend = '--';		# option list terminator
251*0Sstevel@tonic-gate    my %opctl = ();		# table of option specs
252*0Sstevel@tonic-gate    my $pkg = $caller || (caller)[0];	# current context
253*0Sstevel@tonic-gate				# Needed if linkage is omitted.
254*0Sstevel@tonic-gate    my @ret = ();		# accum for non-options
255*0Sstevel@tonic-gate    my %linkage;		# linkage
256*0Sstevel@tonic-gate    my $userlinkage;		# user supplied HASH
257*0Sstevel@tonic-gate    my $opt;			# current option
258*0Sstevel@tonic-gate    my $prefix = $genprefix;	# current prefix
259*0Sstevel@tonic-gate
260*0Sstevel@tonic-gate    $error = '';
261*0Sstevel@tonic-gate
262*0Sstevel@tonic-gate    if ( $debug ) {
263*0Sstevel@tonic-gate	# Avoid some warnings if debugging.
264*0Sstevel@tonic-gate	local ($^W) = 0;
265*0Sstevel@tonic-gate	print STDERR
266*0Sstevel@tonic-gate	  ("Getopt::Long $Getopt::Long::VERSION (",
267*0Sstevel@tonic-gate	   '$Revision: 2.68 $', ") ",
268*0Sstevel@tonic-gate	   "called from package \"$pkg\".",
269*0Sstevel@tonic-gate	   "\n  ",
270*0Sstevel@tonic-gate	   "ARGV: (@ARGV)",
271*0Sstevel@tonic-gate	   "\n  ",
272*0Sstevel@tonic-gate	   "autoabbrev=$autoabbrev,".
273*0Sstevel@tonic-gate	   "bundling=$bundling,",
274*0Sstevel@tonic-gate	   "getopt_compat=$getopt_compat,",
275*0Sstevel@tonic-gate	   "gnu_compat=$gnu_compat,",
276*0Sstevel@tonic-gate	   "order=$order,",
277*0Sstevel@tonic-gate	   "\n  ",
278*0Sstevel@tonic-gate	   "ignorecase=$ignorecase,",
279*0Sstevel@tonic-gate	   "requested_version=$requested_version,",
280*0Sstevel@tonic-gate	   "passthrough=$passthrough,",
281*0Sstevel@tonic-gate	   "genprefix=\"$genprefix\".",
282*0Sstevel@tonic-gate	   "\n");
283*0Sstevel@tonic-gate    }
284*0Sstevel@tonic-gate
285*0Sstevel@tonic-gate    # Check for ref HASH as first argument.
286*0Sstevel@tonic-gate    # First argument may be an object. It's OK to use this as long
287*0Sstevel@tonic-gate    # as it is really a hash underneath.
288*0Sstevel@tonic-gate    $userlinkage = undef;
289*0Sstevel@tonic-gate    if ( @optionlist && ref($optionlist[0]) and
290*0Sstevel@tonic-gate	 "$optionlist[0]" =~ /^(?:.*\=)?HASH\([^\(]*\)$/ ) {
291*0Sstevel@tonic-gate	$userlinkage = shift (@optionlist);
292*0Sstevel@tonic-gate	print STDERR ("=> user linkage: $userlinkage\n") if $debug;
293*0Sstevel@tonic-gate    }
294*0Sstevel@tonic-gate
295*0Sstevel@tonic-gate    # See if the first element of the optionlist contains option
296*0Sstevel@tonic-gate    # starter characters.
297*0Sstevel@tonic-gate    # Be careful not to interpret '<>' as option starters.
298*0Sstevel@tonic-gate    if ( @optionlist && $optionlist[0] =~ /^\W+$/
299*0Sstevel@tonic-gate	 && !($optionlist[0] eq '<>'
300*0Sstevel@tonic-gate	      && @optionlist > 0
301*0Sstevel@tonic-gate	      && ref($optionlist[1])) ) {
302*0Sstevel@tonic-gate	$prefix = shift (@optionlist);
303*0Sstevel@tonic-gate	# Turn into regexp. Needs to be parenthesized!
304*0Sstevel@tonic-gate	$prefix =~ s/(\W)/\\$1/g;
305*0Sstevel@tonic-gate	$prefix = "([" . $prefix . "])";
306*0Sstevel@tonic-gate	print STDERR ("=> prefix=\"$prefix\"\n") if $debug;
307*0Sstevel@tonic-gate    }
308*0Sstevel@tonic-gate
309*0Sstevel@tonic-gate    # Verify correctness of optionlist.
310*0Sstevel@tonic-gate    %opctl = ();
311*0Sstevel@tonic-gate    while ( @optionlist ) {
312*0Sstevel@tonic-gate	my $opt = shift (@optionlist);
313*0Sstevel@tonic-gate
314*0Sstevel@tonic-gate	# Strip leading prefix so people can specify "--foo=i" if they like.
315*0Sstevel@tonic-gate	$opt = $+ if $opt =~ /^$prefix+(.*)$/s;
316*0Sstevel@tonic-gate
317*0Sstevel@tonic-gate	if ( $opt eq '<>' ) {
318*0Sstevel@tonic-gate	    if ( (defined $userlinkage)
319*0Sstevel@tonic-gate		&& !(@optionlist > 0 && ref($optionlist[0]))
320*0Sstevel@tonic-gate		&& (exists $userlinkage->{$opt})
321*0Sstevel@tonic-gate		&& ref($userlinkage->{$opt}) ) {
322*0Sstevel@tonic-gate		unshift (@optionlist, $userlinkage->{$opt});
323*0Sstevel@tonic-gate	    }
324*0Sstevel@tonic-gate	    unless ( @optionlist > 0
325*0Sstevel@tonic-gate		    && ref($optionlist[0]) && ref($optionlist[0]) eq 'CODE' ) {
326*0Sstevel@tonic-gate		$error .= "Option spec <> requires a reference to a subroutine\n";
327*0Sstevel@tonic-gate		# Kill the linkage (to avoid another error).
328*0Sstevel@tonic-gate		shift (@optionlist)
329*0Sstevel@tonic-gate		  if @optionlist && ref($optionlist[0]);
330*0Sstevel@tonic-gate		next;
331*0Sstevel@tonic-gate	    }
332*0Sstevel@tonic-gate	    $linkage{'<>'} = shift (@optionlist);
333*0Sstevel@tonic-gate	    next;
334*0Sstevel@tonic-gate	}
335*0Sstevel@tonic-gate
336*0Sstevel@tonic-gate	# Parse option spec.
337*0Sstevel@tonic-gate	my ($name, $orig) = ParseOptionSpec ($opt, \%opctl);
338*0Sstevel@tonic-gate	unless ( defined $name ) {
339*0Sstevel@tonic-gate	    # Failed. $orig contains the error message. Sorry for the abuse.
340*0Sstevel@tonic-gate	    $error .= $orig;
341*0Sstevel@tonic-gate	    # Kill the linkage (to avoid another error).
342*0Sstevel@tonic-gate	    shift (@optionlist)
343*0Sstevel@tonic-gate	      if @optionlist && ref($optionlist[0]);
344*0Sstevel@tonic-gate	    next;
345*0Sstevel@tonic-gate	}
346*0Sstevel@tonic-gate
347*0Sstevel@tonic-gate	# If no linkage is supplied in the @optionlist, copy it from
348*0Sstevel@tonic-gate	# the userlinkage if available.
349*0Sstevel@tonic-gate	if ( defined $userlinkage ) {
350*0Sstevel@tonic-gate	    unless ( @optionlist > 0 && ref($optionlist[0]) ) {
351*0Sstevel@tonic-gate		if ( exists $userlinkage->{$orig} &&
352*0Sstevel@tonic-gate		     ref($userlinkage->{$orig}) ) {
353*0Sstevel@tonic-gate		    print STDERR ("=> found userlinkage for \"$orig\": ",
354*0Sstevel@tonic-gate				  "$userlinkage->{$orig}\n")
355*0Sstevel@tonic-gate			if $debug;
356*0Sstevel@tonic-gate		    unshift (@optionlist, $userlinkage->{$orig});
357*0Sstevel@tonic-gate		}
358*0Sstevel@tonic-gate		else {
359*0Sstevel@tonic-gate		    # Do nothing. Being undefined will be handled later.
360*0Sstevel@tonic-gate		    next;
361*0Sstevel@tonic-gate		}
362*0Sstevel@tonic-gate	    }
363*0Sstevel@tonic-gate	}
364*0Sstevel@tonic-gate
365*0Sstevel@tonic-gate	# Copy the linkage. If omitted, link to global variable.
366*0Sstevel@tonic-gate	if ( @optionlist > 0 && ref($optionlist[0]) ) {
367*0Sstevel@tonic-gate	    print STDERR ("=> link \"$orig\" to $optionlist[0]\n")
368*0Sstevel@tonic-gate		if $debug;
369*0Sstevel@tonic-gate	    my $rl = ref($linkage{$orig} = shift (@optionlist));
370*0Sstevel@tonic-gate
371*0Sstevel@tonic-gate	    if ( $rl eq "ARRAY" ) {
372*0Sstevel@tonic-gate		$opctl{$name}[CTL_DEST] = CTL_DEST_ARRAY;
373*0Sstevel@tonic-gate	    }
374*0Sstevel@tonic-gate	    elsif ( $rl eq "HASH" ) {
375*0Sstevel@tonic-gate		$opctl{$name}[CTL_DEST] = CTL_DEST_HASH;
376*0Sstevel@tonic-gate	    }
377*0Sstevel@tonic-gate	    elsif ( $rl eq "SCALAR" ) {
378*0Sstevel@tonic-gate#		if ( $opctl{$name}[CTL_DEST] == CTL_DEST_ARRAY ) {
379*0Sstevel@tonic-gate#		    my $t = $linkage{$orig};
380*0Sstevel@tonic-gate#		    $$t = $linkage{$orig} = [];
381*0Sstevel@tonic-gate#		}
382*0Sstevel@tonic-gate#		elsif ( $opctl{$name}[CTL_DEST] == CTL_DEST_HASH ) {
383*0Sstevel@tonic-gate#		}
384*0Sstevel@tonic-gate#		else {
385*0Sstevel@tonic-gate		    # Ok.
386*0Sstevel@tonic-gate#		}
387*0Sstevel@tonic-gate	    }
388*0Sstevel@tonic-gate	    elsif ( $rl eq "CODE" ) {
389*0Sstevel@tonic-gate		# Ok.
390*0Sstevel@tonic-gate	    }
391*0Sstevel@tonic-gate	    else {
392*0Sstevel@tonic-gate		$error .= "Invalid option linkage for \"$opt\"\n";
393*0Sstevel@tonic-gate	    }
394*0Sstevel@tonic-gate	}
395*0Sstevel@tonic-gate	else {
396*0Sstevel@tonic-gate	    # Link to global $opt_XXX variable.
397*0Sstevel@tonic-gate	    # Make sure a valid perl identifier results.
398*0Sstevel@tonic-gate	    my $ov = $orig;
399*0Sstevel@tonic-gate	    $ov =~ s/\W/_/g;
400*0Sstevel@tonic-gate	    if ( $opctl{$name}[CTL_DEST] == CTL_DEST_ARRAY ) {
401*0Sstevel@tonic-gate		print STDERR ("=> link \"$orig\" to \@$pkg","::opt_$ov\n")
402*0Sstevel@tonic-gate		    if $debug;
403*0Sstevel@tonic-gate		eval ("\$linkage{\$orig} = \\\@".$pkg."::opt_$ov;");
404*0Sstevel@tonic-gate	    }
405*0Sstevel@tonic-gate	    elsif ( $opctl{$name}[CTL_DEST] == CTL_DEST_HASH ) {
406*0Sstevel@tonic-gate		print STDERR ("=> link \"$orig\" to \%$pkg","::opt_$ov\n")
407*0Sstevel@tonic-gate		    if $debug;
408*0Sstevel@tonic-gate		eval ("\$linkage{\$orig} = \\\%".$pkg."::opt_$ov;");
409*0Sstevel@tonic-gate	    }
410*0Sstevel@tonic-gate	    else {
411*0Sstevel@tonic-gate		print STDERR ("=> link \"$orig\" to \$$pkg","::opt_$ov\n")
412*0Sstevel@tonic-gate		    if $debug;
413*0Sstevel@tonic-gate		eval ("\$linkage{\$orig} = \\\$".$pkg."::opt_$ov;");
414*0Sstevel@tonic-gate	    }
415*0Sstevel@tonic-gate	}
416*0Sstevel@tonic-gate    }
417*0Sstevel@tonic-gate
418*0Sstevel@tonic-gate    # Bail out if errors found.
419*0Sstevel@tonic-gate    die ($error) if $error;
420*0Sstevel@tonic-gate    $error = 0;
421*0Sstevel@tonic-gate
422*0Sstevel@tonic-gate    # Supply --version and --help support, if needed and allowed.
423*0Sstevel@tonic-gate    if ( defined($auto_version) ? $auto_version : ($requested_version >= 2.3203) ) {
424*0Sstevel@tonic-gate	if ( !defined($opctl{version}) ) {
425*0Sstevel@tonic-gate	    $opctl{version} = ['','version',0,CTL_DEST_CODE,undef];
426*0Sstevel@tonic-gate	    $linkage{version} = \&VersionMessage;
427*0Sstevel@tonic-gate	}
428*0Sstevel@tonic-gate	$auto_version = 1;
429*0Sstevel@tonic-gate    }
430*0Sstevel@tonic-gate    if ( defined($auto_help) ? $auto_help : ($requested_version >= 2.3203) ) {
431*0Sstevel@tonic-gate	if ( !defined($opctl{help}) && !defined($opctl{'?'}) ) {
432*0Sstevel@tonic-gate	    $opctl{help} = $opctl{'?'} = ['','help',0,CTL_DEST_CODE,undef];
433*0Sstevel@tonic-gate	    $linkage{help} = \&HelpMessage;
434*0Sstevel@tonic-gate	}
435*0Sstevel@tonic-gate	$auto_help = 1;
436*0Sstevel@tonic-gate    }
437*0Sstevel@tonic-gate
438*0Sstevel@tonic-gate    # Show the options tables if debugging.
439*0Sstevel@tonic-gate    if ( $debug ) {
440*0Sstevel@tonic-gate	my ($arrow, $k, $v);
441*0Sstevel@tonic-gate	$arrow = "=> ";
442*0Sstevel@tonic-gate	while ( ($k,$v) = each(%opctl) ) {
443*0Sstevel@tonic-gate	    print STDERR ($arrow, "\$opctl{$k} = $v ", OptCtl($v), "\n");
444*0Sstevel@tonic-gate	    $arrow = "   ";
445*0Sstevel@tonic-gate	}
446*0Sstevel@tonic-gate    }
447*0Sstevel@tonic-gate
448*0Sstevel@tonic-gate    # Process argument list
449*0Sstevel@tonic-gate    my $goon = 1;
450*0Sstevel@tonic-gate    while ( $goon && @ARGV > 0 ) {
451*0Sstevel@tonic-gate
452*0Sstevel@tonic-gate	# Get next argument.
453*0Sstevel@tonic-gate	$opt = shift (@ARGV);
454*0Sstevel@tonic-gate	print STDERR ("=> arg \"", $opt, "\"\n") if $debug;
455*0Sstevel@tonic-gate
456*0Sstevel@tonic-gate	# Double dash is option list terminator.
457*0Sstevel@tonic-gate	if ( $opt eq $argend ) {
458*0Sstevel@tonic-gate	  push (@ret, $argend) if $passthrough;
459*0Sstevel@tonic-gate	  last;
460*0Sstevel@tonic-gate	}
461*0Sstevel@tonic-gate
462*0Sstevel@tonic-gate	# Look it up.
463*0Sstevel@tonic-gate	my $tryopt = $opt;
464*0Sstevel@tonic-gate	my $found;		# success status
465*0Sstevel@tonic-gate	my $key;		# key (if hash type)
466*0Sstevel@tonic-gate	my $arg;		# option argument
467*0Sstevel@tonic-gate	my $ctl;		# the opctl entry
468*0Sstevel@tonic-gate
469*0Sstevel@tonic-gate	($found, $opt, $ctl, $arg, $key) =
470*0Sstevel@tonic-gate	  FindOption ($prefix, $argend, $opt, \%opctl);
471*0Sstevel@tonic-gate
472*0Sstevel@tonic-gate	if ( $found ) {
473*0Sstevel@tonic-gate
474*0Sstevel@tonic-gate	    # FindOption undefines $opt in case of errors.
475*0Sstevel@tonic-gate	    next unless defined $opt;
476*0Sstevel@tonic-gate
477*0Sstevel@tonic-gate	    if ( defined $arg ) {
478*0Sstevel@tonic-gate
479*0Sstevel@tonic-gate		# Get the canonical name.
480*0Sstevel@tonic-gate		print STDERR ("=> cname for \"$opt\" is ") if $debug;
481*0Sstevel@tonic-gate		$opt = $ctl->[CTL_CNAME];
482*0Sstevel@tonic-gate		print STDERR ("\"$ctl->[CTL_CNAME]\"\n") if $debug;
483*0Sstevel@tonic-gate
484*0Sstevel@tonic-gate		if ( defined $linkage{$opt} ) {
485*0Sstevel@tonic-gate		    print STDERR ("=> ref(\$L{$opt}) -> ",
486*0Sstevel@tonic-gate				  ref($linkage{$opt}), "\n") if $debug;
487*0Sstevel@tonic-gate
488*0Sstevel@tonic-gate		    if ( ref($linkage{$opt}) eq 'SCALAR' ) {
489*0Sstevel@tonic-gate			if ( $ctl->[CTL_TYPE] eq '+' ) {
490*0Sstevel@tonic-gate			    print STDERR ("=> \$\$L{$opt} += \"$arg\"\n")
491*0Sstevel@tonic-gate			      if $debug;
492*0Sstevel@tonic-gate			    if ( defined ${$linkage{$opt}} ) {
493*0Sstevel@tonic-gate			        ${$linkage{$opt}} += $arg;
494*0Sstevel@tonic-gate			    }
495*0Sstevel@tonic-gate		            else {
496*0Sstevel@tonic-gate			        ${$linkage{$opt}} = $arg;
497*0Sstevel@tonic-gate			    }
498*0Sstevel@tonic-gate			}
499*0Sstevel@tonic-gate			elsif ( $ctl->[CTL_DEST] == CTL_DEST_ARRAY ) {
500*0Sstevel@tonic-gate			    print STDERR ("=> ref(\$L{$opt}) auto-vivified",
501*0Sstevel@tonic-gate					  " to ARRAY\n")
502*0Sstevel@tonic-gate			      if $debug;
503*0Sstevel@tonic-gate			    my $t = $linkage{$opt};
504*0Sstevel@tonic-gate			    $$t = $linkage{$opt} = [];
505*0Sstevel@tonic-gate			    print STDERR ("=> push(\@{\$L{$opt}, \"$arg\")\n")
506*0Sstevel@tonic-gate			      if $debug;
507*0Sstevel@tonic-gate			    push (@{$linkage{$opt}}, $arg);
508*0Sstevel@tonic-gate			}
509*0Sstevel@tonic-gate			elsif ( $ctl->[CTL_DEST] == CTL_DEST_HASH ) {
510*0Sstevel@tonic-gate			    print STDERR ("=> ref(\$L{$opt}) auto-vivified",
511*0Sstevel@tonic-gate					  " to HASH\n")
512*0Sstevel@tonic-gate			      if $debug;
513*0Sstevel@tonic-gate			    my $t = $linkage{$opt};
514*0Sstevel@tonic-gate			    $$t = $linkage{$opt} = {};
515*0Sstevel@tonic-gate			    print STDERR ("=> \$\$L{$opt}->{$key} = \"$arg\"\n")
516*0Sstevel@tonic-gate			      if $debug;
517*0Sstevel@tonic-gate			    $linkage{$opt}->{$key} = $arg;
518*0Sstevel@tonic-gate			}
519*0Sstevel@tonic-gate			else {
520*0Sstevel@tonic-gate			    print STDERR ("=> \$\$L{$opt} = \"$arg\"\n")
521*0Sstevel@tonic-gate			      if $debug;
522*0Sstevel@tonic-gate			    ${$linkage{$opt}} = $arg;
523*0Sstevel@tonic-gate		        }
524*0Sstevel@tonic-gate		    }
525*0Sstevel@tonic-gate		    elsif ( ref($linkage{$opt}) eq 'ARRAY' ) {
526*0Sstevel@tonic-gate			print STDERR ("=> push(\@{\$L{$opt}, \"$arg\")\n")
527*0Sstevel@tonic-gate			    if $debug;
528*0Sstevel@tonic-gate			push (@{$linkage{$opt}}, $arg);
529*0Sstevel@tonic-gate		    }
530*0Sstevel@tonic-gate		    elsif ( ref($linkage{$opt}) eq 'HASH' ) {
531*0Sstevel@tonic-gate			print STDERR ("=> \$\$L{$opt}->{$key} = \"$arg\"\n")
532*0Sstevel@tonic-gate			    if $debug;
533*0Sstevel@tonic-gate			$linkage{$opt}->{$key} = $arg;
534*0Sstevel@tonic-gate		    }
535*0Sstevel@tonic-gate		    elsif ( ref($linkage{$opt}) eq 'CODE' ) {
536*0Sstevel@tonic-gate			print STDERR ("=> &L{$opt}(\"$opt\"",
537*0Sstevel@tonic-gate				      $ctl->[CTL_DEST] == CTL_DEST_HASH ? ", \"$key\"" : "",
538*0Sstevel@tonic-gate				      ", \"$arg\")\n")
539*0Sstevel@tonic-gate			    if $debug;
540*0Sstevel@tonic-gate			my $eval_error = do {
541*0Sstevel@tonic-gate			    local $@;
542*0Sstevel@tonic-gate			    local $SIG{__DIE__}  = '__DEFAULT__';
543*0Sstevel@tonic-gate			    eval {
544*0Sstevel@tonic-gate				&{$linkage{$opt}}($opt,
545*0Sstevel@tonic-gate						  $ctl->[CTL_DEST] == CTL_DEST_HASH ? ($key) : (),
546*0Sstevel@tonic-gate						  $arg);
547*0Sstevel@tonic-gate			    };
548*0Sstevel@tonic-gate			    $@;
549*0Sstevel@tonic-gate			};
550*0Sstevel@tonic-gate			print STDERR ("=> die($eval_error)\n")
551*0Sstevel@tonic-gate			  if $debug && $eval_error ne '';
552*0Sstevel@tonic-gate			if ( $eval_error =~ /^!/ ) {
553*0Sstevel@tonic-gate			    if ( $eval_error =~ /^!FINISH\b/ ) {
554*0Sstevel@tonic-gate				$goon = 0;
555*0Sstevel@tonic-gate			    }
556*0Sstevel@tonic-gate			}
557*0Sstevel@tonic-gate			elsif ( $eval_error ne '' ) {
558*0Sstevel@tonic-gate			    warn ($eval_error);
559*0Sstevel@tonic-gate			    $error++;
560*0Sstevel@tonic-gate			}
561*0Sstevel@tonic-gate		    }
562*0Sstevel@tonic-gate		    else {
563*0Sstevel@tonic-gate			print STDERR ("Invalid REF type \"", ref($linkage{$opt}),
564*0Sstevel@tonic-gate				      "\" in linkage\n");
565*0Sstevel@tonic-gate			die("Getopt::Long -- internal error!\n");
566*0Sstevel@tonic-gate		    }
567*0Sstevel@tonic-gate		}
568*0Sstevel@tonic-gate		# No entry in linkage means entry in userlinkage.
569*0Sstevel@tonic-gate		elsif ( $ctl->[CTL_DEST] == CTL_DEST_ARRAY ) {
570*0Sstevel@tonic-gate		    if ( defined $userlinkage->{$opt} ) {
571*0Sstevel@tonic-gate			print STDERR ("=> push(\@{\$L{$opt}}, \"$arg\")\n")
572*0Sstevel@tonic-gate			    if $debug;
573*0Sstevel@tonic-gate			push (@{$userlinkage->{$opt}}, $arg);
574*0Sstevel@tonic-gate		    }
575*0Sstevel@tonic-gate		    else {
576*0Sstevel@tonic-gate			print STDERR ("=>\$L{$opt} = [\"$arg\"]\n")
577*0Sstevel@tonic-gate			    if $debug;
578*0Sstevel@tonic-gate			$userlinkage->{$opt} = [$arg];
579*0Sstevel@tonic-gate		    }
580*0Sstevel@tonic-gate		}
581*0Sstevel@tonic-gate		elsif ( $ctl->[CTL_DEST] == CTL_DEST_HASH ) {
582*0Sstevel@tonic-gate		    if ( defined $userlinkage->{$opt} ) {
583*0Sstevel@tonic-gate			print STDERR ("=> \$L{$opt}->{$key} = \"$arg\"\n")
584*0Sstevel@tonic-gate			    if $debug;
585*0Sstevel@tonic-gate			$userlinkage->{$opt}->{$key} = $arg;
586*0Sstevel@tonic-gate		    }
587*0Sstevel@tonic-gate		    else {
588*0Sstevel@tonic-gate			print STDERR ("=>\$L{$opt} = {$key => \"$arg\"}\n")
589*0Sstevel@tonic-gate			    if $debug;
590*0Sstevel@tonic-gate			$userlinkage->{$opt} = {$key => $arg};
591*0Sstevel@tonic-gate		    }
592*0Sstevel@tonic-gate		}
593*0Sstevel@tonic-gate		else {
594*0Sstevel@tonic-gate		    if ( $ctl->[CTL_TYPE] eq '+' ) {
595*0Sstevel@tonic-gate			print STDERR ("=> \$L{$opt} += \"$arg\"\n")
596*0Sstevel@tonic-gate			  if $debug;
597*0Sstevel@tonic-gate			if ( defined $userlinkage->{$opt} ) {
598*0Sstevel@tonic-gate			    $userlinkage->{$opt} += $arg;
599*0Sstevel@tonic-gate			}
600*0Sstevel@tonic-gate			else {
601*0Sstevel@tonic-gate			    $userlinkage->{$opt} = $arg;
602*0Sstevel@tonic-gate			}
603*0Sstevel@tonic-gate		    }
604*0Sstevel@tonic-gate		    else {
605*0Sstevel@tonic-gate			print STDERR ("=>\$L{$opt} = \"$arg\"\n") if $debug;
606*0Sstevel@tonic-gate			$userlinkage->{$opt} = $arg;
607*0Sstevel@tonic-gate		    }
608*0Sstevel@tonic-gate		}
609*0Sstevel@tonic-gate	    }
610*0Sstevel@tonic-gate	}
611*0Sstevel@tonic-gate
612*0Sstevel@tonic-gate	# Not an option. Save it if we $PERMUTE and don't have a <>.
613*0Sstevel@tonic-gate	elsif ( $order == $PERMUTE ) {
614*0Sstevel@tonic-gate	    # Try non-options call-back.
615*0Sstevel@tonic-gate	    my $cb;
616*0Sstevel@tonic-gate	    if ( (defined ($cb = $linkage{'<>'})) ) {
617*0Sstevel@tonic-gate		print STDERR ("=> &L{$tryopt}(\"$tryopt\")\n")
618*0Sstevel@tonic-gate		  if $debug;
619*0Sstevel@tonic-gate		my $eval_error = do {
620*0Sstevel@tonic-gate		    local $@;
621*0Sstevel@tonic-gate		    local $SIG{__DIE__}  = '__DEFAULT__';
622*0Sstevel@tonic-gate		    eval { &$cb ($tryopt) };
623*0Sstevel@tonic-gate		    $@;
624*0Sstevel@tonic-gate		};
625*0Sstevel@tonic-gate		print STDERR ("=> die($eval_error)\n")
626*0Sstevel@tonic-gate		  if $debug && $eval_error ne '';
627*0Sstevel@tonic-gate		if ( $eval_error =~ /^!/ ) {
628*0Sstevel@tonic-gate		    if ( $eval_error =~ /^!FINISH\b/ ) {
629*0Sstevel@tonic-gate			$goon = 0;
630*0Sstevel@tonic-gate		    }
631*0Sstevel@tonic-gate		}
632*0Sstevel@tonic-gate		elsif ( $eval_error ne '' ) {
633*0Sstevel@tonic-gate		    warn ($eval_error);
634*0Sstevel@tonic-gate		    $error++;
635*0Sstevel@tonic-gate		}
636*0Sstevel@tonic-gate	    }
637*0Sstevel@tonic-gate	    else {
638*0Sstevel@tonic-gate		print STDERR ("=> saving \"$tryopt\" ",
639*0Sstevel@tonic-gate			      "(not an option, may permute)\n") if $debug;
640*0Sstevel@tonic-gate		push (@ret, $tryopt);
641*0Sstevel@tonic-gate	    }
642*0Sstevel@tonic-gate	    next;
643*0Sstevel@tonic-gate	}
644*0Sstevel@tonic-gate
645*0Sstevel@tonic-gate	# ...otherwise, terminate.
646*0Sstevel@tonic-gate	else {
647*0Sstevel@tonic-gate	    # Push this one back and exit.
648*0Sstevel@tonic-gate	    unshift (@ARGV, $tryopt);
649*0Sstevel@tonic-gate	    return ($error == 0);
650*0Sstevel@tonic-gate	}
651*0Sstevel@tonic-gate
652*0Sstevel@tonic-gate    }
653*0Sstevel@tonic-gate
654*0Sstevel@tonic-gate    # Finish.
655*0Sstevel@tonic-gate    if ( @ret && $order == $PERMUTE ) {
656*0Sstevel@tonic-gate	#  Push back accumulated arguments
657*0Sstevel@tonic-gate	print STDERR ("=> restoring \"", join('" "', @ret), "\"\n")
658*0Sstevel@tonic-gate	    if $debug;
659*0Sstevel@tonic-gate	unshift (@ARGV, @ret);
660*0Sstevel@tonic-gate    }
661*0Sstevel@tonic-gate
662*0Sstevel@tonic-gate    return ($error == 0);
663*0Sstevel@tonic-gate}
664*0Sstevel@tonic-gate
665*0Sstevel@tonic-gate# A readable representation of what's in an optbl.
666*0Sstevel@tonic-gatesub OptCtl ($) {
667*0Sstevel@tonic-gate    my ($v) = @_;
668*0Sstevel@tonic-gate    my @v = map { defined($_) ? ($_) : ("<undef>") } @$v;
669*0Sstevel@tonic-gate    "[".
670*0Sstevel@tonic-gate      join(",",
671*0Sstevel@tonic-gate	   "\"$v[CTL_TYPE]\"",
672*0Sstevel@tonic-gate	   "\"$v[CTL_CNAME]\"",
673*0Sstevel@tonic-gate	   $v[CTL_MAND] ? "O" : "M",
674*0Sstevel@tonic-gate	   ("\$","\@","\%","\&")[$v[CTL_DEST] || 0],
675*0Sstevel@tonic-gate	   "\"$v[CTL_DEFAULT]\"",
676*0Sstevel@tonic-gate#	   $v[CTL_RANGE] || '',
677*0Sstevel@tonic-gate#	   $v[CTL_REPEAT] || '',
678*0Sstevel@tonic-gate	  ). "]";
679*0Sstevel@tonic-gate}
680*0Sstevel@tonic-gate
681*0Sstevel@tonic-gate# Parse an option specification and fill the tables.
682*0Sstevel@tonic-gatesub ParseOptionSpec ($$) {
683*0Sstevel@tonic-gate    my ($opt, $opctl) = @_;
684*0Sstevel@tonic-gate
685*0Sstevel@tonic-gate    # Match option spec.
686*0Sstevel@tonic-gate    if ( $opt !~ m;^
687*0Sstevel@tonic-gate		   (
688*0Sstevel@tonic-gate		     # Option name
689*0Sstevel@tonic-gate		     (?: \w+[-\w]* )
690*0Sstevel@tonic-gate		     # Alias names, or "?"
691*0Sstevel@tonic-gate		     (?: \| (?: \? | \w[-\w]* )? )*
692*0Sstevel@tonic-gate		   )?
693*0Sstevel@tonic-gate		   (
694*0Sstevel@tonic-gate		     # Either modifiers ...
695*0Sstevel@tonic-gate		     [!+]
696*0Sstevel@tonic-gate		     |
697*0Sstevel@tonic-gate		     # ... or a value/dest specification
698*0Sstevel@tonic-gate		     [=:] [ionfs] [@%]?
699*0Sstevel@tonic-gate		     |
700*0Sstevel@tonic-gate		     # ... or an optional-with-default spec
701*0Sstevel@tonic-gate		     : (?: -?\d+ | \+ ) [@%]?
702*0Sstevel@tonic-gate		   )?
703*0Sstevel@tonic-gate		   $;x ) {
704*0Sstevel@tonic-gate	return (undef, "Error in option spec: \"$opt\"\n");
705*0Sstevel@tonic-gate    }
706*0Sstevel@tonic-gate
707*0Sstevel@tonic-gate    my ($names, $spec) = ($1, $2);
708*0Sstevel@tonic-gate    $spec = '' unless defined $spec;
709*0Sstevel@tonic-gate
710*0Sstevel@tonic-gate    # $orig keeps track of the primary name the user specified.
711*0Sstevel@tonic-gate    # This name will be used for the internal or external linkage.
712*0Sstevel@tonic-gate    # In other words, if the user specifies "FoO|BaR", it will
713*0Sstevel@tonic-gate    # match any case combinations of 'foo' and 'bar', but if a global
714*0Sstevel@tonic-gate    # variable needs to be set, it will be $opt_FoO in the exact case
715*0Sstevel@tonic-gate    # as specified.
716*0Sstevel@tonic-gate    my $orig;
717*0Sstevel@tonic-gate
718*0Sstevel@tonic-gate    my @names;
719*0Sstevel@tonic-gate    if ( defined $names ) {
720*0Sstevel@tonic-gate	@names =  split (/\|/, $names);
721*0Sstevel@tonic-gate	$orig = $names[0];
722*0Sstevel@tonic-gate    }
723*0Sstevel@tonic-gate    else {
724*0Sstevel@tonic-gate	@names = ('');
725*0Sstevel@tonic-gate	$orig = '';
726*0Sstevel@tonic-gate    }
727*0Sstevel@tonic-gate
728*0Sstevel@tonic-gate    # Construct the opctl entries.
729*0Sstevel@tonic-gate    my $entry;
730*0Sstevel@tonic-gate    if ( $spec eq '' || $spec eq '+' || $spec eq '!' ) {
731*0Sstevel@tonic-gate	# Fields are hard-wired here.
732*0Sstevel@tonic-gate	$entry = [$spec,$orig,0,CTL_DEST_SCALAR,undef];
733*0Sstevel@tonic-gate    }
734*0Sstevel@tonic-gate    elsif ( $spec =~ /:(-?\d+|\+)([@%])?/ ) {
735*0Sstevel@tonic-gate	my $def = $1;
736*0Sstevel@tonic-gate	my $dest = $2;
737*0Sstevel@tonic-gate	my $type = $def eq '+' ? 'I' : 'i';
738*0Sstevel@tonic-gate	$dest ||= '$';
739*0Sstevel@tonic-gate	$dest = $dest eq '@' ? CTL_DEST_ARRAY
740*0Sstevel@tonic-gate	  : $dest eq '%' ? CTL_DEST_HASH : CTL_DEST_SCALAR;
741*0Sstevel@tonic-gate	# Fields are hard-wired here.
742*0Sstevel@tonic-gate	$entry = [$type,$orig,0,$dest,$def eq '+' ? undef : $def];
743*0Sstevel@tonic-gate    }
744*0Sstevel@tonic-gate    else {
745*0Sstevel@tonic-gate	my ($mand, $type, $dest) = $spec =~ /([=:])([ionfs])([@%])?/;
746*0Sstevel@tonic-gate	$type = 'i' if $type eq 'n';
747*0Sstevel@tonic-gate	$dest ||= '$';
748*0Sstevel@tonic-gate	$dest = $dest eq '@' ? CTL_DEST_ARRAY
749*0Sstevel@tonic-gate	  : $dest eq '%' ? CTL_DEST_HASH : CTL_DEST_SCALAR;
750*0Sstevel@tonic-gate	# Fields are hard-wired here.
751*0Sstevel@tonic-gate	$entry = [$type,$orig,$mand eq '=',$dest,undef];
752*0Sstevel@tonic-gate    }
753*0Sstevel@tonic-gate
754*0Sstevel@tonic-gate    # Process all names. First is canonical, the rest are aliases.
755*0Sstevel@tonic-gate    my $dups = '';
756*0Sstevel@tonic-gate    foreach ( @names ) {
757*0Sstevel@tonic-gate
758*0Sstevel@tonic-gate	$_ = lc ($_)
759*0Sstevel@tonic-gate	  if $ignorecase > (($bundling && length($_) == 1) ? 1 : 0);
760*0Sstevel@tonic-gate
761*0Sstevel@tonic-gate	if ( exists $opctl->{$_} ) {
762*0Sstevel@tonic-gate	    $dups .= "Duplicate specification \"$opt\" for option \"$_\"\n";
763*0Sstevel@tonic-gate	}
764*0Sstevel@tonic-gate
765*0Sstevel@tonic-gate	if ( $spec eq '!' ) {
766*0Sstevel@tonic-gate	    $opctl->{"no$_"} = $entry;
767*0Sstevel@tonic-gate	    $opctl->{"no-$_"} = $entry;
768*0Sstevel@tonic-gate	    $opctl->{$_} = [@$entry];
769*0Sstevel@tonic-gate	    $opctl->{$_}->[CTL_TYPE] = '';
770*0Sstevel@tonic-gate	}
771*0Sstevel@tonic-gate	else {
772*0Sstevel@tonic-gate	    $opctl->{$_} = $entry;
773*0Sstevel@tonic-gate	}
774*0Sstevel@tonic-gate    }
775*0Sstevel@tonic-gate
776*0Sstevel@tonic-gate    if ( $dups && $^W ) {
777*0Sstevel@tonic-gate	foreach ( split(/\n+/, $dups) ) {
778*0Sstevel@tonic-gate	    warn($_."\n");
779*0Sstevel@tonic-gate	}
780*0Sstevel@tonic-gate    }
781*0Sstevel@tonic-gate    ($names[0], $orig);
782*0Sstevel@tonic-gate}
783*0Sstevel@tonic-gate
784*0Sstevel@tonic-gate# Option lookup.
785*0Sstevel@tonic-gatesub FindOption ($$$$) {
786*0Sstevel@tonic-gate
787*0Sstevel@tonic-gate    # returns (1, $opt, $ctl, $arg, $key) if okay,
788*0Sstevel@tonic-gate    # returns (1, undef) if option in error,
789*0Sstevel@tonic-gate    # returns (0) otherwise.
790*0Sstevel@tonic-gate
791*0Sstevel@tonic-gate    my ($prefix, $argend, $opt, $opctl) = @_;
792*0Sstevel@tonic-gate
793*0Sstevel@tonic-gate    print STDERR ("=> find \"$opt\"\n") if $debug;
794*0Sstevel@tonic-gate
795*0Sstevel@tonic-gate    return (0) unless $opt =~ /^$prefix(.*)$/s;
796*0Sstevel@tonic-gate    return (0) if $opt eq "-" && !defined $opctl->{''};
797*0Sstevel@tonic-gate
798*0Sstevel@tonic-gate    $opt = $+;
799*0Sstevel@tonic-gate    my $starter = $1;
800*0Sstevel@tonic-gate
801*0Sstevel@tonic-gate    print STDERR ("=> split \"$starter\"+\"$opt\"\n") if $debug;
802*0Sstevel@tonic-gate
803*0Sstevel@tonic-gate    my $optarg;			# value supplied with --opt=value
804*0Sstevel@tonic-gate    my $rest;			# remainder from unbundling
805*0Sstevel@tonic-gate
806*0Sstevel@tonic-gate    # If it is a long option, it may include the value.
807*0Sstevel@tonic-gate    # With getopt_compat, only if not bundling.
808*0Sstevel@tonic-gate    if ( ($starter eq "--"
809*0Sstevel@tonic-gate          || ($getopt_compat && ($bundling == 0 || $bundling == 2)))
810*0Sstevel@tonic-gate	  && $opt =~ /^([^=]+)=(.*)$/s ) {
811*0Sstevel@tonic-gate	$opt = $1;
812*0Sstevel@tonic-gate	$optarg = $2;
813*0Sstevel@tonic-gate	print STDERR ("=> option \"", $opt,
814*0Sstevel@tonic-gate		      "\", optarg = \"$optarg\"\n") if $debug;
815*0Sstevel@tonic-gate    }
816*0Sstevel@tonic-gate
817*0Sstevel@tonic-gate    #### Look it up ###
818*0Sstevel@tonic-gate
819*0Sstevel@tonic-gate    my $tryopt = $opt;		# option to try
820*0Sstevel@tonic-gate
821*0Sstevel@tonic-gate    if ( $bundling && $starter eq '-' ) {
822*0Sstevel@tonic-gate
823*0Sstevel@tonic-gate	# To try overrides, obey case ignore.
824*0Sstevel@tonic-gate	$tryopt = $ignorecase ? lc($opt) : $opt;
825*0Sstevel@tonic-gate
826*0Sstevel@tonic-gate	# If bundling == 2, long options can override bundles.
827*0Sstevel@tonic-gate	if ( $bundling == 2 && length($tryopt) > 1
828*0Sstevel@tonic-gate	     && defined ($opctl->{$tryopt}) ) {
829*0Sstevel@tonic-gate	    print STDERR ("=> $starter$tryopt overrides unbundling\n")
830*0Sstevel@tonic-gate	      if $debug;
831*0Sstevel@tonic-gate	}
832*0Sstevel@tonic-gate	else {
833*0Sstevel@tonic-gate	    $tryopt = $opt;
834*0Sstevel@tonic-gate	    # Unbundle single letter option.
835*0Sstevel@tonic-gate	    $rest = length ($tryopt) > 0 ? substr ($tryopt, 1) : '';
836*0Sstevel@tonic-gate	    $tryopt = substr ($tryopt, 0, 1);
837*0Sstevel@tonic-gate	    $tryopt = lc ($tryopt) if $ignorecase > 1;
838*0Sstevel@tonic-gate	    print STDERR ("=> $starter$tryopt unbundled from ",
839*0Sstevel@tonic-gate			  "$starter$tryopt$rest\n") if $debug;
840*0Sstevel@tonic-gate	    $rest = undef unless $rest ne '';
841*0Sstevel@tonic-gate	}
842*0Sstevel@tonic-gate    }
843*0Sstevel@tonic-gate
844*0Sstevel@tonic-gate    # Try auto-abbreviation.
845*0Sstevel@tonic-gate    elsif ( $autoabbrev ) {
846*0Sstevel@tonic-gate	# Sort the possible long option names.
847*0Sstevel@tonic-gate	my @names = sort(keys (%$opctl));
848*0Sstevel@tonic-gate	# Downcase if allowed.
849*0Sstevel@tonic-gate	$opt = lc ($opt) if $ignorecase;
850*0Sstevel@tonic-gate	$tryopt = $opt;
851*0Sstevel@tonic-gate	# Turn option name into pattern.
852*0Sstevel@tonic-gate	my $pat = quotemeta ($opt);
853*0Sstevel@tonic-gate	# Look up in option names.
854*0Sstevel@tonic-gate	my @hits = grep (/^$pat/, @names);
855*0Sstevel@tonic-gate	print STDERR ("=> ", scalar(@hits), " hits (@hits) with \"$pat\" ",
856*0Sstevel@tonic-gate		      "out of ", scalar(@names), "\n") if $debug;
857*0Sstevel@tonic-gate
858*0Sstevel@tonic-gate	# Check for ambiguous results.
859*0Sstevel@tonic-gate	unless ( (@hits <= 1) || (grep ($_ eq $opt, @hits) == 1) ) {
860*0Sstevel@tonic-gate	    # See if all matches are for the same option.
861*0Sstevel@tonic-gate	    my %hit;
862*0Sstevel@tonic-gate	    foreach ( @hits ) {
863*0Sstevel@tonic-gate		$_ = $opctl->{$_}->[CTL_CNAME]
864*0Sstevel@tonic-gate		  if defined $opctl->{$_}->[CTL_CNAME];
865*0Sstevel@tonic-gate		$hit{$_} = 1;
866*0Sstevel@tonic-gate	    }
867*0Sstevel@tonic-gate	    # Remove auto-supplied options (version, help).
868*0Sstevel@tonic-gate	    if ( keys(%hit) == 2 ) {
869*0Sstevel@tonic-gate		if ( $auto_version && exists($hit{version}) ) {
870*0Sstevel@tonic-gate		    delete $hit{version};
871*0Sstevel@tonic-gate		}
872*0Sstevel@tonic-gate		elsif ( $auto_help && exists($hit{help}) ) {
873*0Sstevel@tonic-gate		    delete $hit{help};
874*0Sstevel@tonic-gate		}
875*0Sstevel@tonic-gate	    }
876*0Sstevel@tonic-gate	    # Now see if it really is ambiguous.
877*0Sstevel@tonic-gate	    unless ( keys(%hit) == 1 ) {
878*0Sstevel@tonic-gate		return (0) if $passthrough;
879*0Sstevel@tonic-gate		warn ("Option ", $opt, " is ambiguous (",
880*0Sstevel@tonic-gate		      join(", ", @hits), ")\n");
881*0Sstevel@tonic-gate		$error++;
882*0Sstevel@tonic-gate		return (1, undef);
883*0Sstevel@tonic-gate	    }
884*0Sstevel@tonic-gate	    @hits = keys(%hit);
885*0Sstevel@tonic-gate	}
886*0Sstevel@tonic-gate
887*0Sstevel@tonic-gate	# Complete the option name, if appropriate.
888*0Sstevel@tonic-gate	if ( @hits == 1 && $hits[0] ne $opt ) {
889*0Sstevel@tonic-gate	    $tryopt = $hits[0];
890*0Sstevel@tonic-gate	    $tryopt = lc ($tryopt) if $ignorecase;
891*0Sstevel@tonic-gate	    print STDERR ("=> option \"$opt\" -> \"$tryopt\"\n")
892*0Sstevel@tonic-gate		if $debug;
893*0Sstevel@tonic-gate	}
894*0Sstevel@tonic-gate    }
895*0Sstevel@tonic-gate
896*0Sstevel@tonic-gate    # Map to all lowercase if ignoring case.
897*0Sstevel@tonic-gate    elsif ( $ignorecase ) {
898*0Sstevel@tonic-gate	$tryopt = lc ($opt);
899*0Sstevel@tonic-gate    }
900*0Sstevel@tonic-gate
901*0Sstevel@tonic-gate    # Check validity by fetching the info.
902*0Sstevel@tonic-gate    my $ctl = $opctl->{$tryopt};
903*0Sstevel@tonic-gate    unless  ( defined $ctl ) {
904*0Sstevel@tonic-gate	return (0) if $passthrough;
905*0Sstevel@tonic-gate	# Pretend one char when bundling.
906*0Sstevel@tonic-gate	if ( $bundling == 1) {
907*0Sstevel@tonic-gate	    $opt = substr($opt,0,1);
908*0Sstevel@tonic-gate            unshift (@ARGV, $starter.$rest) if defined $rest;
909*0Sstevel@tonic-gate	}
910*0Sstevel@tonic-gate	warn ("Unknown option: ", $opt, "\n");
911*0Sstevel@tonic-gate	$error++;
912*0Sstevel@tonic-gate	return (1, undef);
913*0Sstevel@tonic-gate    }
914*0Sstevel@tonic-gate    # Apparently valid.
915*0Sstevel@tonic-gate    $opt = $tryopt;
916*0Sstevel@tonic-gate    print STDERR ("=> found ", OptCtl($ctl),
917*0Sstevel@tonic-gate		  " for \"", $opt, "\"\n") if $debug;
918*0Sstevel@tonic-gate
919*0Sstevel@tonic-gate    #### Determine argument status ####
920*0Sstevel@tonic-gate
921*0Sstevel@tonic-gate    # If it is an option w/o argument, we're almost finished with it.
922*0Sstevel@tonic-gate    my $type = $ctl->[CTL_TYPE];
923*0Sstevel@tonic-gate    my $arg;
924*0Sstevel@tonic-gate
925*0Sstevel@tonic-gate    if ( $type eq '' || $type eq '!' || $type eq '+' ) {
926*0Sstevel@tonic-gate	if ( defined $optarg ) {
927*0Sstevel@tonic-gate	    return (0) if $passthrough;
928*0Sstevel@tonic-gate	    warn ("Option ", $opt, " does not take an argument\n");
929*0Sstevel@tonic-gate	    $error++;
930*0Sstevel@tonic-gate	    undef $opt;
931*0Sstevel@tonic-gate	}
932*0Sstevel@tonic-gate	elsif ( $type eq '' || $type eq '+' ) {
933*0Sstevel@tonic-gate	    # Supply explicit value.
934*0Sstevel@tonic-gate	    $arg = 1;
935*0Sstevel@tonic-gate	}
936*0Sstevel@tonic-gate	else {
937*0Sstevel@tonic-gate	    $opt =~ s/^no-?//i;	# strip NO prefix
938*0Sstevel@tonic-gate	    $arg = 0;		# supply explicit value
939*0Sstevel@tonic-gate	}
940*0Sstevel@tonic-gate	unshift (@ARGV, $starter.$rest) if defined $rest;
941*0Sstevel@tonic-gate	return (1, $opt, $ctl, $arg);
942*0Sstevel@tonic-gate    }
943*0Sstevel@tonic-gate
944*0Sstevel@tonic-gate    # Get mandatory status and type info.
945*0Sstevel@tonic-gate    my $mand = $ctl->[CTL_MAND];
946*0Sstevel@tonic-gate
947*0Sstevel@tonic-gate    # Check if there is an option argument available.
948*0Sstevel@tonic-gate    if ( $gnu_compat && defined $optarg && $optarg eq '' ) {
949*0Sstevel@tonic-gate	return (1, $opt, $ctl, $type eq 's' ? '' : 0) unless $mand;
950*0Sstevel@tonic-gate	$optarg = 0 unless $type eq 's';
951*0Sstevel@tonic-gate    }
952*0Sstevel@tonic-gate
953*0Sstevel@tonic-gate    # Check if there is an option argument available.
954*0Sstevel@tonic-gate    if ( defined $optarg
955*0Sstevel@tonic-gate	 ? ($optarg eq '')
956*0Sstevel@tonic-gate	 : !(defined $rest || @ARGV > 0) ) {
957*0Sstevel@tonic-gate	# Complain if this option needs an argument.
958*0Sstevel@tonic-gate	if ( $mand ) {
959*0Sstevel@tonic-gate	    return (0) if $passthrough;
960*0Sstevel@tonic-gate	    warn ("Option ", $opt, " requires an argument\n");
961*0Sstevel@tonic-gate	    $error++;
962*0Sstevel@tonic-gate	    return (1, undef);
963*0Sstevel@tonic-gate	}
964*0Sstevel@tonic-gate	if ( $type eq 'I' ) {
965*0Sstevel@tonic-gate	    # Fake incremental type.
966*0Sstevel@tonic-gate	    my @c = @$ctl;
967*0Sstevel@tonic-gate	    $c[CTL_TYPE] = '+';
968*0Sstevel@tonic-gate	    return (1, $opt, \@c, 1);
969*0Sstevel@tonic-gate	}
970*0Sstevel@tonic-gate	return (1, $opt, $ctl,
971*0Sstevel@tonic-gate		defined($ctl->[CTL_DEFAULT]) ? $ctl->[CTL_DEFAULT] :
972*0Sstevel@tonic-gate		$type eq 's' ? '' : 0);
973*0Sstevel@tonic-gate    }
974*0Sstevel@tonic-gate
975*0Sstevel@tonic-gate    # Get (possibly optional) argument.
976*0Sstevel@tonic-gate    $arg = (defined $rest ? $rest
977*0Sstevel@tonic-gate	    : (defined $optarg ? $optarg : shift (@ARGV)));
978*0Sstevel@tonic-gate
979*0Sstevel@tonic-gate    # Get key if this is a "name=value" pair for a hash option.
980*0Sstevel@tonic-gate    my $key;
981*0Sstevel@tonic-gate    if ($ctl->[CTL_DEST] == CTL_DEST_HASH && defined $arg) {
982*0Sstevel@tonic-gate	($key, $arg) = ($arg =~ /^([^=]*)=(.*)$/s) ? ($1, $2)
983*0Sstevel@tonic-gate	  : ($arg, defined($ctl->[CTL_DEFAULT]) ? $ctl->[CTL_DEFAULT] :
984*0Sstevel@tonic-gate	     ($mand ? undef : ($type eq 's' ? "" : 1)));
985*0Sstevel@tonic-gate	if (! defined $arg) {
986*0Sstevel@tonic-gate	    warn ("Option $opt, key \"$key\", requires a value\n");
987*0Sstevel@tonic-gate	    $error++;
988*0Sstevel@tonic-gate	    # Push back.
989*0Sstevel@tonic-gate	    unshift (@ARGV, $starter.$rest) if defined $rest;
990*0Sstevel@tonic-gate	    return (1, undef);
991*0Sstevel@tonic-gate	}
992*0Sstevel@tonic-gate    }
993*0Sstevel@tonic-gate
994*0Sstevel@tonic-gate    #### Check if the argument is valid for this option ####
995*0Sstevel@tonic-gate
996*0Sstevel@tonic-gate    my $key_valid = $ctl->[CTL_DEST] == CTL_DEST_HASH ? "[^=]+=" : "";
997*0Sstevel@tonic-gate
998*0Sstevel@tonic-gate    if ( $type eq 's' ) {	# string
999*0Sstevel@tonic-gate	# A mandatory string takes anything.
1000*0Sstevel@tonic-gate	return (1, $opt, $ctl, $arg, $key) if $mand;
1001*0Sstevel@tonic-gate
1002*0Sstevel@tonic-gate	# An optional string takes almost anything.
1003*0Sstevel@tonic-gate	return (1, $opt, $ctl, $arg, $key)
1004*0Sstevel@tonic-gate	  if defined $optarg || defined $rest;
1005*0Sstevel@tonic-gate	return (1, $opt, $ctl, $arg, $key) if $arg eq "-"; # ??
1006*0Sstevel@tonic-gate
1007*0Sstevel@tonic-gate	# Check for option or option list terminator.
1008*0Sstevel@tonic-gate	if ($arg eq $argend ||
1009*0Sstevel@tonic-gate	    $arg =~ /^$prefix.+/) {
1010*0Sstevel@tonic-gate	    # Push back.
1011*0Sstevel@tonic-gate	    unshift (@ARGV, $arg);
1012*0Sstevel@tonic-gate	    # Supply empty value.
1013*0Sstevel@tonic-gate	    $arg = '';
1014*0Sstevel@tonic-gate	}
1015*0Sstevel@tonic-gate    }
1016*0Sstevel@tonic-gate
1017*0Sstevel@tonic-gate    elsif ( $type eq 'i'	# numeric/integer
1018*0Sstevel@tonic-gate            || $type eq 'I'	# numeric/integer w/ incr default
1019*0Sstevel@tonic-gate	    || $type eq 'o' ) { # dec/oct/hex/bin value
1020*0Sstevel@tonic-gate
1021*0Sstevel@tonic-gate	my $o_valid =
1022*0Sstevel@tonic-gate	  $type eq 'o' ? "[-+]?[1-9][0-9]*|0x[0-9a-f]+|0b[01]+|0[0-7]*"
1023*0Sstevel@tonic-gate	    : "[-+]?[0-9]+";
1024*0Sstevel@tonic-gate
1025*0Sstevel@tonic-gate	if ( $bundling && defined $rest
1026*0Sstevel@tonic-gate	     && $rest =~ /^($key_valid)($o_valid)(.*)$/si ) {
1027*0Sstevel@tonic-gate	    ($key, $arg, $rest) = ($1, $2, $+);
1028*0Sstevel@tonic-gate	    chop($key) if $key;
1029*0Sstevel@tonic-gate	    $arg = ($type eq 'o' && $arg =~ /^0/) ? oct($arg) : 0+$arg;
1030*0Sstevel@tonic-gate	    unshift (@ARGV, $starter.$rest) if defined $rest && $rest ne '';
1031*0Sstevel@tonic-gate	}
1032*0Sstevel@tonic-gate	elsif ( $arg =~ /^($o_valid)$/si ) {
1033*0Sstevel@tonic-gate	    $arg = ($type eq 'o' && $arg =~ /^0/) ? oct($arg) : 0+$arg;
1034*0Sstevel@tonic-gate	}
1035*0Sstevel@tonic-gate	else {
1036*0Sstevel@tonic-gate	    if ( defined $optarg || $mand ) {
1037*0Sstevel@tonic-gate		if ( $passthrough ) {
1038*0Sstevel@tonic-gate		    unshift (@ARGV, defined $rest ? $starter.$rest : $arg)
1039*0Sstevel@tonic-gate		      unless defined $optarg;
1040*0Sstevel@tonic-gate		    return (0);
1041*0Sstevel@tonic-gate		}
1042*0Sstevel@tonic-gate		warn ("Value \"", $arg, "\" invalid for option ",
1043*0Sstevel@tonic-gate		      $opt, " (",
1044*0Sstevel@tonic-gate		      $type eq 'o' ? "extended " : '',
1045*0Sstevel@tonic-gate		      "number expected)\n");
1046*0Sstevel@tonic-gate		$error++;
1047*0Sstevel@tonic-gate		# Push back.
1048*0Sstevel@tonic-gate		unshift (@ARGV, $starter.$rest) if defined $rest;
1049*0Sstevel@tonic-gate		return (1, undef);
1050*0Sstevel@tonic-gate	    }
1051*0Sstevel@tonic-gate	    else {
1052*0Sstevel@tonic-gate		# Push back.
1053*0Sstevel@tonic-gate		unshift (@ARGV, defined $rest ? $starter.$rest : $arg);
1054*0Sstevel@tonic-gate		if ( $type eq 'I' ) {
1055*0Sstevel@tonic-gate		    # Fake incremental type.
1056*0Sstevel@tonic-gate		    my @c = @$ctl;
1057*0Sstevel@tonic-gate		    $c[CTL_TYPE] = '+';
1058*0Sstevel@tonic-gate		    return (1, $opt, \@c, 1);
1059*0Sstevel@tonic-gate		}
1060*0Sstevel@tonic-gate		# Supply default value.
1061*0Sstevel@tonic-gate		$arg = defined($ctl->[CTL_DEFAULT]) ? $ctl->[CTL_DEFAULT] : 0;
1062*0Sstevel@tonic-gate	    }
1063*0Sstevel@tonic-gate	}
1064*0Sstevel@tonic-gate    }
1065*0Sstevel@tonic-gate
1066*0Sstevel@tonic-gate    elsif ( $type eq 'f' ) { # real number, int is also ok
1067*0Sstevel@tonic-gate	# We require at least one digit before a point or 'e',
1068*0Sstevel@tonic-gate	# and at least one digit following the point and 'e'.
1069*0Sstevel@tonic-gate	# [-]NN[.NN][eNN]
1070*0Sstevel@tonic-gate	if ( $bundling && defined $rest &&
1071*0Sstevel@tonic-gate	     $rest =~ /^($key_valid)([-+]?[0-9]+(\.[0-9]+)?([eE][-+]?[0-9]+)?)(.*)$/s ) {
1072*0Sstevel@tonic-gate	    ($key, $arg, $rest) = ($1, $2, $+);
1073*0Sstevel@tonic-gate	    chop($key) if $key;
1074*0Sstevel@tonic-gate	    unshift (@ARGV, $starter.$rest) if defined $rest && $rest ne '';
1075*0Sstevel@tonic-gate	}
1076*0Sstevel@tonic-gate	elsif ( $arg !~ /^[-+]?[0-9.]+(\.[0-9]+)?([eE][-+]?[0-9]+)?$/ ) {
1077*0Sstevel@tonic-gate	    if ( defined $optarg || $mand ) {
1078*0Sstevel@tonic-gate		if ( $passthrough ) {
1079*0Sstevel@tonic-gate		    unshift (@ARGV, defined $rest ? $starter.$rest : $arg)
1080*0Sstevel@tonic-gate		      unless defined $optarg;
1081*0Sstevel@tonic-gate		    return (0);
1082*0Sstevel@tonic-gate		}
1083*0Sstevel@tonic-gate		warn ("Value \"", $arg, "\" invalid for option ",
1084*0Sstevel@tonic-gate		      $opt, " (real number expected)\n");
1085*0Sstevel@tonic-gate		$error++;
1086*0Sstevel@tonic-gate		# Push back.
1087*0Sstevel@tonic-gate		unshift (@ARGV, $starter.$rest) if defined $rest;
1088*0Sstevel@tonic-gate		return (1, undef);
1089*0Sstevel@tonic-gate	    }
1090*0Sstevel@tonic-gate	    else {
1091*0Sstevel@tonic-gate		# Push back.
1092*0Sstevel@tonic-gate		unshift (@ARGV, defined $rest ? $starter.$rest : $arg);
1093*0Sstevel@tonic-gate		# Supply default value.
1094*0Sstevel@tonic-gate		$arg = 0.0;
1095*0Sstevel@tonic-gate	    }
1096*0Sstevel@tonic-gate	}
1097*0Sstevel@tonic-gate    }
1098*0Sstevel@tonic-gate    else {
1099*0Sstevel@tonic-gate	die("Getopt::Long internal error (Can't happen)\n");
1100*0Sstevel@tonic-gate    }
1101*0Sstevel@tonic-gate    return (1, $opt, $ctl, $arg, $key);
1102*0Sstevel@tonic-gate}
1103*0Sstevel@tonic-gate
1104*0Sstevel@tonic-gate# Getopt::Long Configuration.
1105*0Sstevel@tonic-gatesub Configure (@) {
1106*0Sstevel@tonic-gate    my (@options) = @_;
1107*0Sstevel@tonic-gate
1108*0Sstevel@tonic-gate    my $prevconfig =
1109*0Sstevel@tonic-gate      [ $error, $debug, $major_version, $minor_version,
1110*0Sstevel@tonic-gate	$autoabbrev, $getopt_compat, $ignorecase, $bundling, $order,
1111*0Sstevel@tonic-gate	$gnu_compat, $passthrough, $genprefix, $auto_version, $auto_help ];
1112*0Sstevel@tonic-gate
1113*0Sstevel@tonic-gate    if ( ref($options[0]) eq 'ARRAY' ) {
1114*0Sstevel@tonic-gate	( $error, $debug, $major_version, $minor_version,
1115*0Sstevel@tonic-gate	  $autoabbrev, $getopt_compat, $ignorecase, $bundling, $order,
1116*0Sstevel@tonic-gate	  $gnu_compat, $passthrough, $genprefix, $auto_version, $auto_help ) =
1117*0Sstevel@tonic-gate	    @{shift(@options)};
1118*0Sstevel@tonic-gate    }
1119*0Sstevel@tonic-gate
1120*0Sstevel@tonic-gate    my $opt;
1121*0Sstevel@tonic-gate    foreach $opt ( @options ) {
1122*0Sstevel@tonic-gate	my $try = lc ($opt);
1123*0Sstevel@tonic-gate	my $action = 1;
1124*0Sstevel@tonic-gate	if ( $try =~ /^no_?(.*)$/s ) {
1125*0Sstevel@tonic-gate	    $action = 0;
1126*0Sstevel@tonic-gate	    $try = $+;
1127*0Sstevel@tonic-gate	}
1128*0Sstevel@tonic-gate	if ( ($try eq 'default' or $try eq 'defaults') && $action ) {
1129*0Sstevel@tonic-gate	    ConfigDefaults ();
1130*0Sstevel@tonic-gate	}
1131*0Sstevel@tonic-gate	elsif ( ($try eq 'posix_default' or $try eq 'posix_defaults') ) {
1132*0Sstevel@tonic-gate	    local $ENV{POSIXLY_CORRECT};
1133*0Sstevel@tonic-gate	    $ENV{POSIXLY_CORRECT} = 1 if $action;
1134*0Sstevel@tonic-gate	    ConfigDefaults ();
1135*0Sstevel@tonic-gate	}
1136*0Sstevel@tonic-gate	elsif ( $try eq 'auto_abbrev' or $try eq 'autoabbrev' ) {
1137*0Sstevel@tonic-gate	    $autoabbrev = $action;
1138*0Sstevel@tonic-gate	}
1139*0Sstevel@tonic-gate	elsif ( $try eq 'getopt_compat' ) {
1140*0Sstevel@tonic-gate	    $getopt_compat = $action;
1141*0Sstevel@tonic-gate	}
1142*0Sstevel@tonic-gate	elsif ( $try eq 'gnu_getopt' ) {
1143*0Sstevel@tonic-gate	    if ( $action ) {
1144*0Sstevel@tonic-gate		$gnu_compat = 1;
1145*0Sstevel@tonic-gate		$bundling = 1;
1146*0Sstevel@tonic-gate		$getopt_compat = 0;
1147*0Sstevel@tonic-gate		$order = $PERMUTE;
1148*0Sstevel@tonic-gate	    }
1149*0Sstevel@tonic-gate	}
1150*0Sstevel@tonic-gate	elsif ( $try eq 'gnu_compat' ) {
1151*0Sstevel@tonic-gate	    $gnu_compat = $action;
1152*0Sstevel@tonic-gate	}
1153*0Sstevel@tonic-gate	elsif ( $try =~ /^(auto_?)?version$/ ) {
1154*0Sstevel@tonic-gate	    $auto_version = $action;
1155*0Sstevel@tonic-gate	}
1156*0Sstevel@tonic-gate	elsif ( $try =~ /^(auto_?)?help$/ ) {
1157*0Sstevel@tonic-gate	    $auto_help = $action;
1158*0Sstevel@tonic-gate	}
1159*0Sstevel@tonic-gate	elsif ( $try eq 'ignorecase' or $try eq 'ignore_case' ) {
1160*0Sstevel@tonic-gate	    $ignorecase = $action;
1161*0Sstevel@tonic-gate	}
1162*0Sstevel@tonic-gate	elsif ( $try eq 'ignore_case_always' ) {
1163*0Sstevel@tonic-gate	    $ignorecase = $action ? 2 : 0;
1164*0Sstevel@tonic-gate	}
1165*0Sstevel@tonic-gate	elsif ( $try eq 'bundling' ) {
1166*0Sstevel@tonic-gate	    $bundling = $action;
1167*0Sstevel@tonic-gate	}
1168*0Sstevel@tonic-gate	elsif ( $try eq 'bundling_override' ) {
1169*0Sstevel@tonic-gate	    $bundling = $action ? 2 : 0;
1170*0Sstevel@tonic-gate	}
1171*0Sstevel@tonic-gate	elsif ( $try eq 'require_order' ) {
1172*0Sstevel@tonic-gate	    $order = $action ? $REQUIRE_ORDER : $PERMUTE;
1173*0Sstevel@tonic-gate	}
1174*0Sstevel@tonic-gate	elsif ( $try eq 'permute' ) {
1175*0Sstevel@tonic-gate	    $order = $action ? $PERMUTE : $REQUIRE_ORDER;
1176*0Sstevel@tonic-gate	}
1177*0Sstevel@tonic-gate	elsif ( $try eq 'pass_through' or $try eq 'passthrough' ) {
1178*0Sstevel@tonic-gate	    $passthrough = $action;
1179*0Sstevel@tonic-gate	}
1180*0Sstevel@tonic-gate	elsif ( $try =~ /^prefix=(.+)$/ && $action ) {
1181*0Sstevel@tonic-gate	    $genprefix = $1;
1182*0Sstevel@tonic-gate	    # Turn into regexp. Needs to be parenthesized!
1183*0Sstevel@tonic-gate	    $genprefix = "(" . quotemeta($genprefix) . ")";
1184*0Sstevel@tonic-gate	    eval { '' =~ /$genprefix/; };
1185*0Sstevel@tonic-gate	    die("Getopt::Long: invalid pattern \"$genprefix\"") if $@;
1186*0Sstevel@tonic-gate	}
1187*0Sstevel@tonic-gate	elsif ( $try =~ /^prefix_pattern=(.+)$/ && $action ) {
1188*0Sstevel@tonic-gate	    $genprefix = $1;
1189*0Sstevel@tonic-gate	    # Parenthesize if needed.
1190*0Sstevel@tonic-gate	    $genprefix = "(" . $genprefix . ")"
1191*0Sstevel@tonic-gate	      unless $genprefix =~ /^\(.*\)$/;
1192*0Sstevel@tonic-gate	    eval { '' =~ /$genprefix/; };
1193*0Sstevel@tonic-gate	    die("Getopt::Long: invalid pattern \"$genprefix\"") if $@;
1194*0Sstevel@tonic-gate	}
1195*0Sstevel@tonic-gate	elsif ( $try eq 'debug' ) {
1196*0Sstevel@tonic-gate	    $debug = $action;
1197*0Sstevel@tonic-gate	}
1198*0Sstevel@tonic-gate	else {
1199*0Sstevel@tonic-gate	    die("Getopt::Long: unknown config parameter \"$opt\"")
1200*0Sstevel@tonic-gate	}
1201*0Sstevel@tonic-gate    }
1202*0Sstevel@tonic-gate    $prevconfig;
1203*0Sstevel@tonic-gate}
1204*0Sstevel@tonic-gate
1205*0Sstevel@tonic-gate# Deprecated name.
1206*0Sstevel@tonic-gatesub config (@) {
1207*0Sstevel@tonic-gate    Configure (@_);
1208*0Sstevel@tonic-gate}
1209*0Sstevel@tonic-gate
1210*0Sstevel@tonic-gate# Issue a standard message for --version.
1211*0Sstevel@tonic-gate#
1212*0Sstevel@tonic-gate# The arguments are mostly the same as for Pod::Usage::pod2usage:
1213*0Sstevel@tonic-gate#
1214*0Sstevel@tonic-gate#  - a number (exit value)
1215*0Sstevel@tonic-gate#  - a string (lead in message)
1216*0Sstevel@tonic-gate#  - a hash with options. See Pod::Usage for details.
1217*0Sstevel@tonic-gate#
1218*0Sstevel@tonic-gatesub VersionMessage(@) {
1219*0Sstevel@tonic-gate    # Massage args.
1220*0Sstevel@tonic-gate    my $pa = setup_pa_args("version", @_);
1221*0Sstevel@tonic-gate
1222*0Sstevel@tonic-gate    my $v = $main::VERSION;
1223*0Sstevel@tonic-gate    my $fh = $pa->{-output} ||
1224*0Sstevel@tonic-gate      ($pa->{-exitval} eq "NOEXIT" || $pa->{-exitval} < 2) ? \*STDOUT : \*STDERR;
1225*0Sstevel@tonic-gate
1226*0Sstevel@tonic-gate    print $fh (defined($pa->{-message}) ? $pa->{-message} : (),
1227*0Sstevel@tonic-gate	       $0, defined $v ? " version $v" : (),
1228*0Sstevel@tonic-gate	       "\n",
1229*0Sstevel@tonic-gate	       "(", __PACKAGE__, "::", "GetOptions",
1230*0Sstevel@tonic-gate	       " version ",
1231*0Sstevel@tonic-gate	       defined($Getopt::Long::VERSION_STRING)
1232*0Sstevel@tonic-gate	         ? $Getopt::Long::VERSION_STRING : $VERSION, ";",
1233*0Sstevel@tonic-gate	       " Perl version ",
1234*0Sstevel@tonic-gate	       $] >= 5.006 ? sprintf("%vd", $^V) : $],
1235*0Sstevel@tonic-gate	       ")\n");
1236*0Sstevel@tonic-gate    exit($pa->{-exitval}) unless $pa->{-exitval} eq "NOEXIT";
1237*0Sstevel@tonic-gate}
1238*0Sstevel@tonic-gate
1239*0Sstevel@tonic-gate# Issue a standard message for --help.
1240*0Sstevel@tonic-gate#
1241*0Sstevel@tonic-gate# The arguments are the same as for Pod::Usage::pod2usage:
1242*0Sstevel@tonic-gate#
1243*0Sstevel@tonic-gate#  - a number (exit value)
1244*0Sstevel@tonic-gate#  - a string (lead in message)
1245*0Sstevel@tonic-gate#  - a hash with options. See Pod::Usage for details.
1246*0Sstevel@tonic-gate#
1247*0Sstevel@tonic-gatesub HelpMessage(@) {
1248*0Sstevel@tonic-gate    eval {
1249*0Sstevel@tonic-gate	require Pod::Usage;
1250*0Sstevel@tonic-gate	import Pod::Usage;
1251*0Sstevel@tonic-gate	1;
1252*0Sstevel@tonic-gate    } || die("Cannot provide help: cannot load Pod::Usage\n");
1253*0Sstevel@tonic-gate
1254*0Sstevel@tonic-gate    # Note that pod2usage will issue a warning if -exitval => NOEXIT.
1255*0Sstevel@tonic-gate    pod2usage(setup_pa_args("help", @_));
1256*0Sstevel@tonic-gate
1257*0Sstevel@tonic-gate}
1258*0Sstevel@tonic-gate
1259*0Sstevel@tonic-gate# Helper routine to set up a normalized hash ref to be used as
1260*0Sstevel@tonic-gate# argument to pod2usage.
1261*0Sstevel@tonic-gatesub setup_pa_args($@) {
1262*0Sstevel@tonic-gate    my $tag = shift;		# who's calling
1263*0Sstevel@tonic-gate
1264*0Sstevel@tonic-gate    # If called by direct binding to an option, it will get the option
1265*0Sstevel@tonic-gate    # name and value as arguments. Remove these, if so.
1266*0Sstevel@tonic-gate    @_ = () if @_ == 2 && $_[0] eq $tag;
1267*0Sstevel@tonic-gate
1268*0Sstevel@tonic-gate    my $pa;
1269*0Sstevel@tonic-gate    if ( @_ > 1 ) {
1270*0Sstevel@tonic-gate	$pa = { @_ };
1271*0Sstevel@tonic-gate    }
1272*0Sstevel@tonic-gate    else {
1273*0Sstevel@tonic-gate	$pa = shift || {};
1274*0Sstevel@tonic-gate    }
1275*0Sstevel@tonic-gate
1276*0Sstevel@tonic-gate    # At this point, $pa can be a number (exit value), string
1277*0Sstevel@tonic-gate    # (message) or hash with options.
1278*0Sstevel@tonic-gate
1279*0Sstevel@tonic-gate    if ( UNIVERSAL::isa($pa, 'HASH') ) {
1280*0Sstevel@tonic-gate	# Get rid of -msg vs. -message ambiguity.
1281*0Sstevel@tonic-gate	$pa->{-message} = $pa->{-msg};
1282*0Sstevel@tonic-gate	delete($pa->{-msg});
1283*0Sstevel@tonic-gate    }
1284*0Sstevel@tonic-gate    elsif ( $pa =~ /^-?\d+$/ ) {
1285*0Sstevel@tonic-gate	$pa = { -exitval => $pa };
1286*0Sstevel@tonic-gate    }
1287*0Sstevel@tonic-gate    else {
1288*0Sstevel@tonic-gate	$pa = { -message => $pa };
1289*0Sstevel@tonic-gate    }
1290*0Sstevel@tonic-gate
1291*0Sstevel@tonic-gate    # These are _our_ defaults.
1292*0Sstevel@tonic-gate    $pa->{-verbose} = 0 unless exists($pa->{-verbose});
1293*0Sstevel@tonic-gate    $pa->{-exitval} = 0 unless exists($pa->{-exitval});
1294*0Sstevel@tonic-gate    $pa;
1295*0Sstevel@tonic-gate}
1296*0Sstevel@tonic-gate
1297*0Sstevel@tonic-gate# Sneak way to know what version the user requested.
1298*0Sstevel@tonic-gatesub VERSION {
1299*0Sstevel@tonic-gate    $requested_version = $_[1];
1300*0Sstevel@tonic-gate    shift->SUPER::VERSION(@_);
1301*0Sstevel@tonic-gate}
1302*0Sstevel@tonic-gate
1303*0Sstevel@tonic-gate1;
1304*0Sstevel@tonic-gate
1305*0Sstevel@tonic-gate################ Documentation ################
1306*0Sstevel@tonic-gate
1307*0Sstevel@tonic-gate=head1 NAME
1308*0Sstevel@tonic-gate
1309*0Sstevel@tonic-gateGetopt::Long - Extended processing of command line options
1310*0Sstevel@tonic-gate
1311*0Sstevel@tonic-gate=head1 SYNOPSIS
1312*0Sstevel@tonic-gate
1313*0Sstevel@tonic-gate  use Getopt::Long;
1314*0Sstevel@tonic-gate  my $data   = "file.dat";
1315*0Sstevel@tonic-gate  my $length = 24;
1316*0Sstevel@tonic-gate  my $verbose;
1317*0Sstevel@tonic-gate  $result = GetOptions ("length=i" => \$length,    # numeric
1318*0Sstevel@tonic-gate                        "file=s"   => \$data,      # string
1319*0Sstevel@tonic-gate			"verbose"  => \$verbose);  # flag
1320*0Sstevel@tonic-gate
1321*0Sstevel@tonic-gate=head1 DESCRIPTION
1322*0Sstevel@tonic-gate
1323*0Sstevel@tonic-gateThe Getopt::Long module implements an extended getopt function called
1324*0Sstevel@tonic-gateGetOptions(). This function adheres to the POSIX syntax for command
1325*0Sstevel@tonic-gateline options, with GNU extensions. In general, this means that options
1326*0Sstevel@tonic-gatehave long names instead of single letters, and are introduced with a
1327*0Sstevel@tonic-gatedouble dash "--". Support for bundling of command line options, as was
1328*0Sstevel@tonic-gatethe case with the more traditional single-letter approach, is provided
1329*0Sstevel@tonic-gatebut not enabled by default.
1330*0Sstevel@tonic-gate
1331*0Sstevel@tonic-gate=head1 Command Line Options, an Introduction
1332*0Sstevel@tonic-gate
1333*0Sstevel@tonic-gateCommand line operated programs traditionally take their arguments from
1334*0Sstevel@tonic-gatethe command line, for example filenames or other information that the
1335*0Sstevel@tonic-gateprogram needs to know. Besides arguments, these programs often take
1336*0Sstevel@tonic-gatecommand line I<options> as well. Options are not necessary for the
1337*0Sstevel@tonic-gateprogram to work, hence the name 'option', but are used to modify its
1338*0Sstevel@tonic-gatedefault behaviour. For example, a program could do its job quietly,
1339*0Sstevel@tonic-gatebut with a suitable option it could provide verbose information about
1340*0Sstevel@tonic-gatewhat it did.
1341*0Sstevel@tonic-gate
1342*0Sstevel@tonic-gateCommand line options come in several flavours. Historically, they are
1343*0Sstevel@tonic-gatepreceded by a single dash C<->, and consist of a single letter.
1344*0Sstevel@tonic-gate
1345*0Sstevel@tonic-gate    -l -a -c
1346*0Sstevel@tonic-gate
1347*0Sstevel@tonic-gateUsually, these single-character options can be bundled:
1348*0Sstevel@tonic-gate
1349*0Sstevel@tonic-gate    -lac
1350*0Sstevel@tonic-gate
1351*0Sstevel@tonic-gateOptions can have values, the value is placed after the option
1352*0Sstevel@tonic-gatecharacter. Sometimes with whitespace in between, sometimes not:
1353*0Sstevel@tonic-gate
1354*0Sstevel@tonic-gate    -s 24 -s24
1355*0Sstevel@tonic-gate
1356*0Sstevel@tonic-gateDue to the very cryptic nature of these options, another style was
1357*0Sstevel@tonic-gatedeveloped that used long names. So instead of a cryptic C<-l> one
1358*0Sstevel@tonic-gatecould use the more descriptive C<--long>. To distinguish between a
1359*0Sstevel@tonic-gatebundle of single-character options and a long one, two dashes are used
1360*0Sstevel@tonic-gateto precede the option name. Early implementations of long options used
1361*0Sstevel@tonic-gatea plus C<+> instead. Also, option values could be specified either
1362*0Sstevel@tonic-gatelike
1363*0Sstevel@tonic-gate
1364*0Sstevel@tonic-gate    --size=24
1365*0Sstevel@tonic-gate
1366*0Sstevel@tonic-gateor
1367*0Sstevel@tonic-gate
1368*0Sstevel@tonic-gate    --size 24
1369*0Sstevel@tonic-gate
1370*0Sstevel@tonic-gateThe C<+> form is now obsolete and strongly deprecated.
1371*0Sstevel@tonic-gate
1372*0Sstevel@tonic-gate=head1 Getting Started with Getopt::Long
1373*0Sstevel@tonic-gate
1374*0Sstevel@tonic-gateGetopt::Long is the Perl5 successor of C<newgetopt.pl>. This was
1375*0Sstevel@tonic-gatethe first Perl module that provided support for handling the new style
1376*0Sstevel@tonic-gateof command line options, hence the name Getopt::Long. This module
1377*0Sstevel@tonic-gatealso supports single-character options and bundling. In this case, the
1378*0Sstevel@tonic-gateoptions are restricted to alphabetic characters only, and the
1379*0Sstevel@tonic-gatecharacters C<?> and C<->.
1380*0Sstevel@tonic-gate
1381*0Sstevel@tonic-gateTo use Getopt::Long from a Perl program, you must include the
1382*0Sstevel@tonic-gatefollowing line in your Perl program:
1383*0Sstevel@tonic-gate
1384*0Sstevel@tonic-gate    use Getopt::Long;
1385*0Sstevel@tonic-gate
1386*0Sstevel@tonic-gateThis will load the core of the Getopt::Long module and prepare your
1387*0Sstevel@tonic-gateprogram for using it. Most of the actual Getopt::Long code is not
1388*0Sstevel@tonic-gateloaded until you really call one of its functions.
1389*0Sstevel@tonic-gate
1390*0Sstevel@tonic-gateIn the default configuration, options names may be abbreviated to
1391*0Sstevel@tonic-gateuniqueness, case does not matter, and a single dash is sufficient,
1392*0Sstevel@tonic-gateeven for long option names. Also, options may be placed between
1393*0Sstevel@tonic-gatenon-option arguments. See L<Configuring Getopt::Long> for more
1394*0Sstevel@tonic-gatedetails on how to configure Getopt::Long.
1395*0Sstevel@tonic-gate
1396*0Sstevel@tonic-gate=head2 Simple options
1397*0Sstevel@tonic-gate
1398*0Sstevel@tonic-gateThe most simple options are the ones that take no values. Their mere
1399*0Sstevel@tonic-gatepresence on the command line enables the option. Popular examples are:
1400*0Sstevel@tonic-gate
1401*0Sstevel@tonic-gate    --all --verbose --quiet --debug
1402*0Sstevel@tonic-gate
1403*0Sstevel@tonic-gateHandling simple options is straightforward:
1404*0Sstevel@tonic-gate
1405*0Sstevel@tonic-gate    my $verbose = '';	# option variable with default value (false)
1406*0Sstevel@tonic-gate    my $all = '';	# option variable with default value (false)
1407*0Sstevel@tonic-gate    GetOptions ('verbose' => \$verbose, 'all' => \$all);
1408*0Sstevel@tonic-gate
1409*0Sstevel@tonic-gateThe call to GetOptions() parses the command line arguments that are
1410*0Sstevel@tonic-gatepresent in C<@ARGV> and sets the option variable to the value C<1> if
1411*0Sstevel@tonic-gatethe option did occur on the command line. Otherwise, the option
1412*0Sstevel@tonic-gatevariable is not touched. Setting the option value to true is often
1413*0Sstevel@tonic-gatecalled I<enabling> the option.
1414*0Sstevel@tonic-gate
1415*0Sstevel@tonic-gateThe option name as specified to the GetOptions() function is called
1416*0Sstevel@tonic-gatethe option I<specification>. Later we'll see that this specification
1417*0Sstevel@tonic-gatecan contain more than just the option name. The reference to the
1418*0Sstevel@tonic-gatevariable is called the option I<destination>.
1419*0Sstevel@tonic-gate
1420*0Sstevel@tonic-gateGetOptions() will return a true value if the command line could be
1421*0Sstevel@tonic-gateprocessed successfully. Otherwise, it will write error messages to
1422*0Sstevel@tonic-gateSTDERR, and return a false result.
1423*0Sstevel@tonic-gate
1424*0Sstevel@tonic-gate=head2 A little bit less simple options
1425*0Sstevel@tonic-gate
1426*0Sstevel@tonic-gateGetopt::Long supports two useful variants of simple options:
1427*0Sstevel@tonic-gateI<negatable> options and I<incremental> options.
1428*0Sstevel@tonic-gate
1429*0Sstevel@tonic-gateA negatable option is specified with an exclamation mark C<!> after the
1430*0Sstevel@tonic-gateoption name:
1431*0Sstevel@tonic-gate
1432*0Sstevel@tonic-gate    my $verbose = '';	# option variable with default value (false)
1433*0Sstevel@tonic-gate    GetOptions ('verbose!' => \$verbose);
1434*0Sstevel@tonic-gate
1435*0Sstevel@tonic-gateNow, using C<--verbose> on the command line will enable C<$verbose>,
1436*0Sstevel@tonic-gateas expected. But it is also allowed to use C<--noverbose>, which will
1437*0Sstevel@tonic-gatedisable C<$verbose> by setting its value to C<0>. Using a suitable
1438*0Sstevel@tonic-gatedefault value, the program can find out whether C<$verbose> is false
1439*0Sstevel@tonic-gateby default, or disabled by using C<--noverbose>.
1440*0Sstevel@tonic-gate
1441*0Sstevel@tonic-gateAn incremental option is specified with a plus C<+> after the
1442*0Sstevel@tonic-gateoption name:
1443*0Sstevel@tonic-gate
1444*0Sstevel@tonic-gate    my $verbose = '';	# option variable with default value (false)
1445*0Sstevel@tonic-gate    GetOptions ('verbose+' => \$verbose);
1446*0Sstevel@tonic-gate
1447*0Sstevel@tonic-gateUsing C<--verbose> on the command line will increment the value of
1448*0Sstevel@tonic-gateC<$verbose>. This way the program can keep track of how many times the
1449*0Sstevel@tonic-gateoption occurred on the command line. For example, each occurrence of
1450*0Sstevel@tonic-gateC<--verbose> could increase the verbosity level of the program.
1451*0Sstevel@tonic-gate
1452*0Sstevel@tonic-gate=head2 Mixing command line option with other arguments
1453*0Sstevel@tonic-gate
1454*0Sstevel@tonic-gateUsually programs take command line options as well as other arguments,
1455*0Sstevel@tonic-gatefor example, file names. It is good practice to always specify the
1456*0Sstevel@tonic-gateoptions first, and the other arguments last. Getopt::Long will,
1457*0Sstevel@tonic-gatehowever, allow the options and arguments to be mixed and 'filter out'
1458*0Sstevel@tonic-gateall the options before passing the rest of the arguments to the
1459*0Sstevel@tonic-gateprogram. To stop Getopt::Long from processing further arguments,
1460*0Sstevel@tonic-gateinsert a double dash C<--> on the command line:
1461*0Sstevel@tonic-gate
1462*0Sstevel@tonic-gate    --size 24 -- --all
1463*0Sstevel@tonic-gate
1464*0Sstevel@tonic-gateIn this example, C<--all> will I<not> be treated as an option, but
1465*0Sstevel@tonic-gatepassed to the program unharmed, in C<@ARGV>.
1466*0Sstevel@tonic-gate
1467*0Sstevel@tonic-gate=head2 Options with values
1468*0Sstevel@tonic-gate
1469*0Sstevel@tonic-gateFor options that take values it must be specified whether the option
1470*0Sstevel@tonic-gatevalue is required or not, and what kind of value the option expects.
1471*0Sstevel@tonic-gate
1472*0Sstevel@tonic-gateThree kinds of values are supported: integer numbers, floating point
1473*0Sstevel@tonic-gatenumbers, and strings.
1474*0Sstevel@tonic-gate
1475*0Sstevel@tonic-gateIf the option value is required, Getopt::Long will take the
1476*0Sstevel@tonic-gatecommand line argument that follows the option and assign this to the
1477*0Sstevel@tonic-gateoption variable. If, however, the option value is specified as
1478*0Sstevel@tonic-gateoptional, this will only be done if that value does not look like a
1479*0Sstevel@tonic-gatevalid command line option itself.
1480*0Sstevel@tonic-gate
1481*0Sstevel@tonic-gate    my $tag = '';	# option variable with default value
1482*0Sstevel@tonic-gate    GetOptions ('tag=s' => \$tag);
1483*0Sstevel@tonic-gate
1484*0Sstevel@tonic-gateIn the option specification, the option name is followed by an equals
1485*0Sstevel@tonic-gatesign C<=> and the letter C<s>. The equals sign indicates that this
1486*0Sstevel@tonic-gateoption requires a value. The letter C<s> indicates that this value is
1487*0Sstevel@tonic-gatean arbitrary string. Other possible value types are C<i> for integer
1488*0Sstevel@tonic-gatevalues, and C<f> for floating point values. Using a colon C<:> instead
1489*0Sstevel@tonic-gateof the equals sign indicates that the option value is optional. In
1490*0Sstevel@tonic-gatethis case, if no suitable value is supplied, string valued options get
1491*0Sstevel@tonic-gatean empty string C<''> assigned, while numeric options are set to C<0>.
1492*0Sstevel@tonic-gate
1493*0Sstevel@tonic-gate=head2 Options with multiple values
1494*0Sstevel@tonic-gate
1495*0Sstevel@tonic-gateOptions sometimes take several values. For example, a program could
1496*0Sstevel@tonic-gateuse multiple directories to search for library files:
1497*0Sstevel@tonic-gate
1498*0Sstevel@tonic-gate    --library lib/stdlib --library lib/extlib
1499*0Sstevel@tonic-gate
1500*0Sstevel@tonic-gateTo accomplish this behaviour, simply specify an array reference as the
1501*0Sstevel@tonic-gatedestination for the option:
1502*0Sstevel@tonic-gate
1503*0Sstevel@tonic-gate    GetOptions ("library=s" => \@libfiles);
1504*0Sstevel@tonic-gate
1505*0Sstevel@tonic-gateAlternatively, you can specify that the option can have multiple
1506*0Sstevel@tonic-gatevalues by adding a "@", and pass a scalar reference as the
1507*0Sstevel@tonic-gatedestination:
1508*0Sstevel@tonic-gate
1509*0Sstevel@tonic-gate    GetOptions ("library=s@" => \$libfiles);
1510*0Sstevel@tonic-gate
1511*0Sstevel@tonic-gateUsed with the example above, C<@libfiles> (or C<@$libfiles>) would
1512*0Sstevel@tonic-gatecontain two strings upon completion: C<"lib/srdlib"> and
1513*0Sstevel@tonic-gateC<"lib/extlib">, in that order. It is also possible to specify that
1514*0Sstevel@tonic-gateonly integer or floating point numbers are acceptible values.
1515*0Sstevel@tonic-gate
1516*0Sstevel@tonic-gateOften it is useful to allow comma-separated lists of values as well as
1517*0Sstevel@tonic-gatemultiple occurrences of the options. This is easy using Perl's split()
1518*0Sstevel@tonic-gateand join() operators:
1519*0Sstevel@tonic-gate
1520*0Sstevel@tonic-gate    GetOptions ("library=s" => \@libfiles);
1521*0Sstevel@tonic-gate    @libfiles = split(/,/,join(',',@libfiles));
1522*0Sstevel@tonic-gate
1523*0Sstevel@tonic-gateOf course, it is important to choose the right separator string for
1524*0Sstevel@tonic-gateeach purpose.
1525*0Sstevel@tonic-gate
1526*0Sstevel@tonic-gate=head2 Options with hash values
1527*0Sstevel@tonic-gate
1528*0Sstevel@tonic-gateIf the option destination is a reference to a hash, the option will
1529*0Sstevel@tonic-gatetake, as value, strings of the form I<key>C<=>I<value>. The value will
1530*0Sstevel@tonic-gatebe stored with the specified key in the hash.
1531*0Sstevel@tonic-gate
1532*0Sstevel@tonic-gate    GetOptions ("define=s" => \%defines);
1533*0Sstevel@tonic-gate
1534*0Sstevel@tonic-gateAlternatively you can use:
1535*0Sstevel@tonic-gate
1536*0Sstevel@tonic-gate    GetOptions ("define=s%" => \$defines);
1537*0Sstevel@tonic-gate
1538*0Sstevel@tonic-gateWhen used with command line options:
1539*0Sstevel@tonic-gate
1540*0Sstevel@tonic-gate    --define os=linux --define vendor=redhat
1541*0Sstevel@tonic-gate
1542*0Sstevel@tonic-gatethe hash C<%defines> (or C<%$defines>) will contain two keys, C<"os">
1543*0Sstevel@tonic-gatewith value C<"linux> and C<"vendor"> with value C<"redhat">. It is
1544*0Sstevel@tonic-gatealso possible to specify that only integer or floating point numbers
1545*0Sstevel@tonic-gateare acceptible values. The keys are always taken to be strings.
1546*0Sstevel@tonic-gate
1547*0Sstevel@tonic-gate=head2 User-defined subroutines to handle options
1548*0Sstevel@tonic-gate
1549*0Sstevel@tonic-gateUltimate control over what should be done when (actually: each time)
1550*0Sstevel@tonic-gatean option is encountered on the command line can be achieved by
1551*0Sstevel@tonic-gatedesignating a reference to a subroutine (or an anonymous subroutine)
1552*0Sstevel@tonic-gateas the option destination. When GetOptions() encounters the option, it
1553*0Sstevel@tonic-gatewill call the subroutine with two or three arguments. The first
1554*0Sstevel@tonic-gateargument is the name of the option. For a scalar or array destination,
1555*0Sstevel@tonic-gatethe second argument is the value to be stored. For a hash destination,
1556*0Sstevel@tonic-gatethe second arguments is the key to the hash, and the third argument
1557*0Sstevel@tonic-gatethe value to be stored. It is up to the subroutine to store the value,
1558*0Sstevel@tonic-gateor do whatever it thinks is appropriate.
1559*0Sstevel@tonic-gate
1560*0Sstevel@tonic-gateA trivial application of this mechanism is to implement options that
1561*0Sstevel@tonic-gateare related to each other. For example:
1562*0Sstevel@tonic-gate
1563*0Sstevel@tonic-gate    my $verbose = '';	# option variable with default value (false)
1564*0Sstevel@tonic-gate    GetOptions ('verbose' => \$verbose,
1565*0Sstevel@tonic-gate	        'quiet'   => sub { $verbose = 0 });
1566*0Sstevel@tonic-gate
1567*0Sstevel@tonic-gateHere C<--verbose> and C<--quiet> control the same variable
1568*0Sstevel@tonic-gateC<$verbose>, but with opposite values.
1569*0Sstevel@tonic-gate
1570*0Sstevel@tonic-gateIf the subroutine needs to signal an error, it should call die() with
1571*0Sstevel@tonic-gatethe desired error message as its argument. GetOptions() will catch the
1572*0Sstevel@tonic-gatedie(), issue the error message, and record that an error result must
1573*0Sstevel@tonic-gatebe returned upon completion.
1574*0Sstevel@tonic-gate
1575*0Sstevel@tonic-gateIf the text of the error message starts with an exclamantion mark C<!>
1576*0Sstevel@tonic-gateit is interpreted specially by GetOptions(). There is currently one
1577*0Sstevel@tonic-gatespecial command implemented: C<die("!FINISH")> will cause GetOptions()
1578*0Sstevel@tonic-gateto stop processing options, as if it encountered a double dash C<-->.
1579*0Sstevel@tonic-gate
1580*0Sstevel@tonic-gate=head2 Options with multiple names
1581*0Sstevel@tonic-gate
1582*0Sstevel@tonic-gateOften it is user friendly to supply alternate mnemonic names for
1583*0Sstevel@tonic-gateoptions. For example C<--height> could be an alternate name for
1584*0Sstevel@tonic-gateC<--length>. Alternate names can be included in the option
1585*0Sstevel@tonic-gatespecification, separated by vertical bar C<|> characters. To implement
1586*0Sstevel@tonic-gatethe above example:
1587*0Sstevel@tonic-gate
1588*0Sstevel@tonic-gate    GetOptions ('length|height=f' => \$length);
1589*0Sstevel@tonic-gate
1590*0Sstevel@tonic-gateThe first name is called the I<primary> name, the other names are
1591*0Sstevel@tonic-gatecalled I<aliases>.
1592*0Sstevel@tonic-gate
1593*0Sstevel@tonic-gateMultiple alternate names are possible.
1594*0Sstevel@tonic-gate
1595*0Sstevel@tonic-gate=head2 Case and abbreviations
1596*0Sstevel@tonic-gate
1597*0Sstevel@tonic-gateWithout additional configuration, GetOptions() will ignore the case of
1598*0Sstevel@tonic-gateoption names, and allow the options to be abbreviated to uniqueness.
1599*0Sstevel@tonic-gate
1600*0Sstevel@tonic-gate    GetOptions ('length|height=f' => \$length, "head" => \$head);
1601*0Sstevel@tonic-gate
1602*0Sstevel@tonic-gateThis call will allow C<--l> and C<--L> for the length option, but
1603*0Sstevel@tonic-gaterequires a least C<--hea> and C<--hei> for the head and height options.
1604*0Sstevel@tonic-gate
1605*0Sstevel@tonic-gate=head2 Summary of Option Specifications
1606*0Sstevel@tonic-gate
1607*0Sstevel@tonic-gateEach option specifier consists of two parts: the name specification
1608*0Sstevel@tonic-gateand the argument specification.
1609*0Sstevel@tonic-gate
1610*0Sstevel@tonic-gateThe name specification contains the name of the option, optionally
1611*0Sstevel@tonic-gatefollowed by a list of alternative names separated by vertical bar
1612*0Sstevel@tonic-gatecharacters.
1613*0Sstevel@tonic-gate
1614*0Sstevel@tonic-gate    length	      option name is "length"
1615*0Sstevel@tonic-gate    length|size|l     name is "length", aliases are "size" and "l"
1616*0Sstevel@tonic-gate
1617*0Sstevel@tonic-gateThe argument specification is optional. If omitted, the option is
1618*0Sstevel@tonic-gateconsidered boolean, a value of 1 will be assigned when the option is
1619*0Sstevel@tonic-gateused on the command line.
1620*0Sstevel@tonic-gate
1621*0Sstevel@tonic-gateThe argument specification can be
1622*0Sstevel@tonic-gate
1623*0Sstevel@tonic-gate=over 4
1624*0Sstevel@tonic-gate
1625*0Sstevel@tonic-gate=item !
1626*0Sstevel@tonic-gate
1627*0Sstevel@tonic-gateThe option does not take an argument and may be negated, i.e. prefixed
1628*0Sstevel@tonic-gateby "no". E.g. C<"foo!"> will allow C<--foo> (a value of 1 will be
1629*0Sstevel@tonic-gateassigned) and C<--nofoo> and C<--no-foo> (a value of 0 will be assigned). If the
1630*0Sstevel@tonic-gateoption has aliases, this applies to the aliases as well.
1631*0Sstevel@tonic-gate
1632*0Sstevel@tonic-gateUsing negation on a single letter option when bundling is in effect is
1633*0Sstevel@tonic-gatepointless and will result in a warning.
1634*0Sstevel@tonic-gate
1635*0Sstevel@tonic-gate=item +
1636*0Sstevel@tonic-gate
1637*0Sstevel@tonic-gateThe option does not take an argument and will be incremented by 1
1638*0Sstevel@tonic-gateevery time it appears on the command line. E.g. C<"more+">, when used
1639*0Sstevel@tonic-gatewith C<--more --more --more>, will increment the value three times,
1640*0Sstevel@tonic-gateresulting in a value of 3 (provided it was 0 or undefined at first).
1641*0Sstevel@tonic-gate
1642*0Sstevel@tonic-gateThe C<+> specifier is ignored if the option destination is not a scalar.
1643*0Sstevel@tonic-gate
1644*0Sstevel@tonic-gate=item = I<type> [ I<desttype> ]
1645*0Sstevel@tonic-gate
1646*0Sstevel@tonic-gateThe option requires an argument of the given type. Supported types
1647*0Sstevel@tonic-gateare:
1648*0Sstevel@tonic-gate
1649*0Sstevel@tonic-gate=over 4
1650*0Sstevel@tonic-gate
1651*0Sstevel@tonic-gate=item s
1652*0Sstevel@tonic-gate
1653*0Sstevel@tonic-gateString. An arbitrary sequence of characters. It is valid for the
1654*0Sstevel@tonic-gateargument to start with C<-> or C<-->.
1655*0Sstevel@tonic-gate
1656*0Sstevel@tonic-gate=item i
1657*0Sstevel@tonic-gate
1658*0Sstevel@tonic-gateInteger. An optional leading plus or minus sign, followed by a
1659*0Sstevel@tonic-gatesequence of digits.
1660*0Sstevel@tonic-gate
1661*0Sstevel@tonic-gate=item o
1662*0Sstevel@tonic-gate
1663*0Sstevel@tonic-gateExtended integer, Perl style. This can be either an optional leading
1664*0Sstevel@tonic-gateplus or minus sign, followed by a sequence of digits, or an octal
1665*0Sstevel@tonic-gatestring (a zero, optionally followed by '0', '1', .. '7'), or a
1666*0Sstevel@tonic-gatehexadecimal string (C<0x> followed by '0' .. '9', 'a' .. 'f', case
1667*0Sstevel@tonic-gateinsensitive), or a binary string (C<0b> followed by a series of '0'
1668*0Sstevel@tonic-gateand '1').
1669*0Sstevel@tonic-gate
1670*0Sstevel@tonic-gate=item f
1671*0Sstevel@tonic-gate
1672*0Sstevel@tonic-gateReal number. For example C<3.14>, C<-6.23E24> and so on.
1673*0Sstevel@tonic-gate
1674*0Sstevel@tonic-gate=back
1675*0Sstevel@tonic-gate
1676*0Sstevel@tonic-gateThe I<desttype> can be C<@> or C<%> to specify that the option is
1677*0Sstevel@tonic-gatelist or a hash valued. This is only needed when the destination for
1678*0Sstevel@tonic-gatethe option value is not otherwise specified. It should be omitted when
1679*0Sstevel@tonic-gatenot needed.
1680*0Sstevel@tonic-gate
1681*0Sstevel@tonic-gate=item : I<type> [ I<desttype> ]
1682*0Sstevel@tonic-gate
1683*0Sstevel@tonic-gateLike C<=>, but designates the argument as optional.
1684*0Sstevel@tonic-gateIf omitted, an empty string will be assigned to string values options,
1685*0Sstevel@tonic-gateand the value zero to numeric options.
1686*0Sstevel@tonic-gate
1687*0Sstevel@tonic-gateNote that if a string argument starts with C<-> or C<-->, it will be
1688*0Sstevel@tonic-gateconsidered an option on itself.
1689*0Sstevel@tonic-gate
1690*0Sstevel@tonic-gate=item : I<number> [ I<desttype> ]
1691*0Sstevel@tonic-gate
1692*0Sstevel@tonic-gateLike C<:i>, but if the value is omitted, the I<number> will be assigned.
1693*0Sstevel@tonic-gate
1694*0Sstevel@tonic-gate=item : + [ I<desttype> ]
1695*0Sstevel@tonic-gate
1696*0Sstevel@tonic-gateLike C<:i>, but if the value is omitted, the current value for the
1697*0Sstevel@tonic-gateoption will be incremented.
1698*0Sstevel@tonic-gate
1699*0Sstevel@tonic-gate=back
1700*0Sstevel@tonic-gate
1701*0Sstevel@tonic-gate=head1 Advanced Possibilities
1702*0Sstevel@tonic-gate
1703*0Sstevel@tonic-gate=head2 Object oriented interface
1704*0Sstevel@tonic-gate
1705*0Sstevel@tonic-gateGetopt::Long can be used in an object oriented way as well:
1706*0Sstevel@tonic-gate
1707*0Sstevel@tonic-gate    use Getopt::Long;
1708*0Sstevel@tonic-gate    $p = new Getopt::Long::Parser;
1709*0Sstevel@tonic-gate    $p->configure(...configuration options...);
1710*0Sstevel@tonic-gate    if ($p->getoptions(...options descriptions...)) ...
1711*0Sstevel@tonic-gate
1712*0Sstevel@tonic-gateConfiguration options can be passed to the constructor:
1713*0Sstevel@tonic-gate
1714*0Sstevel@tonic-gate    $p = new Getopt::Long::Parser
1715*0Sstevel@tonic-gate             config => [...configuration options...];
1716*0Sstevel@tonic-gate
1717*0Sstevel@tonic-gate=head2 Thread Safety
1718*0Sstevel@tonic-gate
1719*0Sstevel@tonic-gateGetopt::Long is thread safe when using ithreads as of Perl 5.8.  It is
1720*0Sstevel@tonic-gateI<not> thread safe when using the older (experimental and now
1721*0Sstevel@tonic-gateobsolete) threads implementation that was added to Perl 5.005.
1722*0Sstevel@tonic-gate
1723*0Sstevel@tonic-gate=head2 Documentation and help texts
1724*0Sstevel@tonic-gate
1725*0Sstevel@tonic-gateGetopt::Long encourages the use of Pod::Usage to produce help
1726*0Sstevel@tonic-gatemessages. For example:
1727*0Sstevel@tonic-gate
1728*0Sstevel@tonic-gate    use Getopt::Long;
1729*0Sstevel@tonic-gate    use Pod::Usage;
1730*0Sstevel@tonic-gate
1731*0Sstevel@tonic-gate    my $man = 0;
1732*0Sstevel@tonic-gate    my $help = 0;
1733*0Sstevel@tonic-gate
1734*0Sstevel@tonic-gate    GetOptions('help|?' => \$help, man => \$man) or pod2usage(2);
1735*0Sstevel@tonic-gate    pod2usage(1) if $help;
1736*0Sstevel@tonic-gate    pod2usage(-exitstatus => 0, -verbose => 2) if $man;
1737*0Sstevel@tonic-gate
1738*0Sstevel@tonic-gate    __END__
1739*0Sstevel@tonic-gate
1740*0Sstevel@tonic-gate    =head1 NAME
1741*0Sstevel@tonic-gate
1742*0Sstevel@tonic-gate    sample - Using Getopt::Long and Pod::Usage
1743*0Sstevel@tonic-gate
1744*0Sstevel@tonic-gate    =head1 SYNOPSIS
1745*0Sstevel@tonic-gate
1746*0Sstevel@tonic-gate    sample [options] [file ...]
1747*0Sstevel@tonic-gate
1748*0Sstevel@tonic-gate     Options:
1749*0Sstevel@tonic-gate       -help            brief help message
1750*0Sstevel@tonic-gate       -man             full documentation
1751*0Sstevel@tonic-gate
1752*0Sstevel@tonic-gate    =head1 OPTIONS
1753*0Sstevel@tonic-gate
1754*0Sstevel@tonic-gate    =over 8
1755*0Sstevel@tonic-gate
1756*0Sstevel@tonic-gate    =item B<-help>
1757*0Sstevel@tonic-gate
1758*0Sstevel@tonic-gate    Print a brief help message and exits.
1759*0Sstevel@tonic-gate
1760*0Sstevel@tonic-gate    =item B<-man>
1761*0Sstevel@tonic-gate
1762*0Sstevel@tonic-gate    Prints the manual page and exits.
1763*0Sstevel@tonic-gate
1764*0Sstevel@tonic-gate    =back
1765*0Sstevel@tonic-gate
1766*0Sstevel@tonic-gate    =head1 DESCRIPTION
1767*0Sstevel@tonic-gate
1768*0Sstevel@tonic-gate    B<This program> will read the given input file(s) and do someting
1769*0Sstevel@tonic-gate    useful with the contents thereof.
1770*0Sstevel@tonic-gate
1771*0Sstevel@tonic-gate    =cut
1772*0Sstevel@tonic-gate
1773*0Sstevel@tonic-gateSee L<Pod::Usage> for details.
1774*0Sstevel@tonic-gate
1775*0Sstevel@tonic-gate=head2 Storing options in a hash
1776*0Sstevel@tonic-gate
1777*0Sstevel@tonic-gateSometimes, for example when there are a lot of options, having a
1778*0Sstevel@tonic-gateseparate variable for each of them can be cumbersome. GetOptions()
1779*0Sstevel@tonic-gatesupports, as an alternative mechanism, storing options in a hash.
1780*0Sstevel@tonic-gate
1781*0Sstevel@tonic-gateTo obtain this, a reference to a hash must be passed I<as the first
1782*0Sstevel@tonic-gateargument> to GetOptions(). For each option that is specified on the
1783*0Sstevel@tonic-gatecommand line, the option value will be stored in the hash with the
1784*0Sstevel@tonic-gateoption name as key. Options that are not actually used on the command
1785*0Sstevel@tonic-gateline will not be put in the hash, on other words,
1786*0Sstevel@tonic-gateC<exists($h{option})> (or defined()) can be used to test if an option
1787*0Sstevel@tonic-gatewas used. The drawback is that warnings will be issued if the program
1788*0Sstevel@tonic-gateruns under C<use strict> and uses C<$h{option}> without testing with
1789*0Sstevel@tonic-gateexists() or defined() first.
1790*0Sstevel@tonic-gate
1791*0Sstevel@tonic-gate    my %h = ();
1792*0Sstevel@tonic-gate    GetOptions (\%h, 'length=i');	# will store in $h{length}
1793*0Sstevel@tonic-gate
1794*0Sstevel@tonic-gateFor options that take list or hash values, it is necessary to indicate
1795*0Sstevel@tonic-gatethis by appending an C<@> or C<%> sign after the type:
1796*0Sstevel@tonic-gate
1797*0Sstevel@tonic-gate    GetOptions (\%h, 'colours=s@');	# will push to @{$h{colours}}
1798*0Sstevel@tonic-gate
1799*0Sstevel@tonic-gateTo make things more complicated, the hash may contain references to
1800*0Sstevel@tonic-gatethe actual destinations, for example:
1801*0Sstevel@tonic-gate
1802*0Sstevel@tonic-gate    my $len = 0;
1803*0Sstevel@tonic-gate    my %h = ('length' => \$len);
1804*0Sstevel@tonic-gate    GetOptions (\%h, 'length=i');	# will store in $len
1805*0Sstevel@tonic-gate
1806*0Sstevel@tonic-gateThis example is fully equivalent with:
1807*0Sstevel@tonic-gate
1808*0Sstevel@tonic-gate    my $len = 0;
1809*0Sstevel@tonic-gate    GetOptions ('length=i' => \$len);	# will store in $len
1810*0Sstevel@tonic-gate
1811*0Sstevel@tonic-gateAny mixture is possible. For example, the most frequently used options
1812*0Sstevel@tonic-gatecould be stored in variables while all other options get stored in the
1813*0Sstevel@tonic-gatehash:
1814*0Sstevel@tonic-gate
1815*0Sstevel@tonic-gate    my $verbose = 0;			# frequently referred
1816*0Sstevel@tonic-gate    my $debug = 0;			# frequently referred
1817*0Sstevel@tonic-gate    my %h = ('verbose' => \$verbose, 'debug' => \$debug);
1818*0Sstevel@tonic-gate    GetOptions (\%h, 'verbose', 'debug', 'filter', 'size=i');
1819*0Sstevel@tonic-gate    if ( $verbose ) { ... }
1820*0Sstevel@tonic-gate    if ( exists $h{filter} ) { ... option 'filter' was specified ... }
1821*0Sstevel@tonic-gate
1822*0Sstevel@tonic-gate=head2 Bundling
1823*0Sstevel@tonic-gate
1824*0Sstevel@tonic-gateWith bundling it is possible to set several single-character options
1825*0Sstevel@tonic-gateat once. For example if C<a>, C<v> and C<x> are all valid options,
1826*0Sstevel@tonic-gate
1827*0Sstevel@tonic-gate    -vax
1828*0Sstevel@tonic-gate
1829*0Sstevel@tonic-gatewould set all three.
1830*0Sstevel@tonic-gate
1831*0Sstevel@tonic-gateGetopt::Long supports two levels of bundling. To enable bundling, a
1832*0Sstevel@tonic-gatecall to Getopt::Long::Configure is required.
1833*0Sstevel@tonic-gate
1834*0Sstevel@tonic-gateThe first level of bundling can be enabled with:
1835*0Sstevel@tonic-gate
1836*0Sstevel@tonic-gate    Getopt::Long::Configure ("bundling");
1837*0Sstevel@tonic-gate
1838*0Sstevel@tonic-gateConfigured this way, single-character options can be bundled but long
1839*0Sstevel@tonic-gateoptions B<must> always start with a double dash C<--> to avoid
1840*0Sstevel@tonic-gateabiguity. For example, when C<vax>, C<a>, C<v> and C<x> are all valid
1841*0Sstevel@tonic-gateoptions,
1842*0Sstevel@tonic-gate
1843*0Sstevel@tonic-gate    -vax
1844*0Sstevel@tonic-gate
1845*0Sstevel@tonic-gatewould set C<a>, C<v> and C<x>, but
1846*0Sstevel@tonic-gate
1847*0Sstevel@tonic-gate    --vax
1848*0Sstevel@tonic-gate
1849*0Sstevel@tonic-gatewould set C<vax>.
1850*0Sstevel@tonic-gate
1851*0Sstevel@tonic-gateThe second level of bundling lifts this restriction. It can be enabled
1852*0Sstevel@tonic-gatewith:
1853*0Sstevel@tonic-gate
1854*0Sstevel@tonic-gate    Getopt::Long::Configure ("bundling_override");
1855*0Sstevel@tonic-gate
1856*0Sstevel@tonic-gateNow, C<-vax> would set the option C<vax>.
1857*0Sstevel@tonic-gate
1858*0Sstevel@tonic-gateWhen any level of bundling is enabled, option values may be inserted
1859*0Sstevel@tonic-gatein the bundle. For example:
1860*0Sstevel@tonic-gate
1861*0Sstevel@tonic-gate    -h24w80
1862*0Sstevel@tonic-gate
1863*0Sstevel@tonic-gateis equivalent to
1864*0Sstevel@tonic-gate
1865*0Sstevel@tonic-gate    -h 24 -w 80
1866*0Sstevel@tonic-gate
1867*0Sstevel@tonic-gateWhen configured for bundling, single-character options are matched
1868*0Sstevel@tonic-gatecase sensitive while long options are matched case insensitive. To
1869*0Sstevel@tonic-gatehave the single-character options matched case insensitive as well,
1870*0Sstevel@tonic-gateuse:
1871*0Sstevel@tonic-gate
1872*0Sstevel@tonic-gate    Getopt::Long::Configure ("bundling", "ignorecase_always");
1873*0Sstevel@tonic-gate
1874*0Sstevel@tonic-gateIt goes without saying that bundling can be quite confusing.
1875*0Sstevel@tonic-gate
1876*0Sstevel@tonic-gate=head2 The lonesome dash
1877*0Sstevel@tonic-gate
1878*0Sstevel@tonic-gateNormally, a lone dash C<-> on the command line will not be considered
1879*0Sstevel@tonic-gatean option. Option processing will terminate (unless "permute" is
1880*0Sstevel@tonic-gateconfigured) and the dash will be left in C<@ARGV>.
1881*0Sstevel@tonic-gate
1882*0Sstevel@tonic-gateIt is possible to get special treatment for a lone dash. This can be
1883*0Sstevel@tonic-gateachieved by adding an option specification with an empty name, for
1884*0Sstevel@tonic-gateexample:
1885*0Sstevel@tonic-gate
1886*0Sstevel@tonic-gate    GetOptions ('' => \$stdio);
1887*0Sstevel@tonic-gate
1888*0Sstevel@tonic-gateA lone dash on the command line will now be a legal option, and using
1889*0Sstevel@tonic-gateit will set variable C<$stdio>.
1890*0Sstevel@tonic-gate
1891*0Sstevel@tonic-gate=head2 Argument callback
1892*0Sstevel@tonic-gate
1893*0Sstevel@tonic-gateA special option 'name' C<< <> >> can be used to designate a subroutine
1894*0Sstevel@tonic-gateto handle non-option arguments. When GetOptions() encounters an
1895*0Sstevel@tonic-gateargument that does not look like an option, it will immediately call this
1896*0Sstevel@tonic-gatesubroutine and passes it one parameter: the argument name.
1897*0Sstevel@tonic-gate
1898*0Sstevel@tonic-gateFor example:
1899*0Sstevel@tonic-gate
1900*0Sstevel@tonic-gate    my $width = 80;
1901*0Sstevel@tonic-gate    sub process { ... }
1902*0Sstevel@tonic-gate    GetOptions ('width=i' => \$width, '<>' => \&process);
1903*0Sstevel@tonic-gate
1904*0Sstevel@tonic-gateWhen applied to the following command line:
1905*0Sstevel@tonic-gate
1906*0Sstevel@tonic-gate    arg1 --width=72 arg2 --width=60 arg3
1907*0Sstevel@tonic-gate
1908*0Sstevel@tonic-gateThis will call
1909*0Sstevel@tonic-gateC<process("arg1")> while C<$width> is C<80>,
1910*0Sstevel@tonic-gateC<process("arg2")> while C<$width> is C<72>, and
1911*0Sstevel@tonic-gateC<process("arg3")> while C<$width> is C<60>.
1912*0Sstevel@tonic-gate
1913*0Sstevel@tonic-gateThis feature requires configuration option B<permute>, see section
1914*0Sstevel@tonic-gateL<Configuring Getopt::Long>.
1915*0Sstevel@tonic-gate
1916*0Sstevel@tonic-gate=head1 Configuring Getopt::Long
1917*0Sstevel@tonic-gate
1918*0Sstevel@tonic-gateGetopt::Long can be configured by calling subroutine
1919*0Sstevel@tonic-gateGetopt::Long::Configure(). This subroutine takes a list of quoted
1920*0Sstevel@tonic-gatestrings, each specifying a configuration option to be enabled, e.g.
1921*0Sstevel@tonic-gateC<ignore_case>, or disabled, e.g. C<no_ignore_case>. Case does not
1922*0Sstevel@tonic-gatematter. Multiple calls to Configure() are possible.
1923*0Sstevel@tonic-gate
1924*0Sstevel@tonic-gateAlternatively, as of version 2.24, the configuration options may be
1925*0Sstevel@tonic-gatepassed together with the C<use> statement:
1926*0Sstevel@tonic-gate
1927*0Sstevel@tonic-gate    use Getopt::Long qw(:config no_ignore_case bundling);
1928*0Sstevel@tonic-gate
1929*0Sstevel@tonic-gateThe following options are available:
1930*0Sstevel@tonic-gate
1931*0Sstevel@tonic-gate=over 12
1932*0Sstevel@tonic-gate
1933*0Sstevel@tonic-gate=item default
1934*0Sstevel@tonic-gate
1935*0Sstevel@tonic-gateThis option causes all configuration options to be reset to their
1936*0Sstevel@tonic-gatedefault values.
1937*0Sstevel@tonic-gate
1938*0Sstevel@tonic-gate=item posix_default
1939*0Sstevel@tonic-gate
1940*0Sstevel@tonic-gateThis option causes all configuration options to be reset to their
1941*0Sstevel@tonic-gatedefault values as if the environment variable POSIXLY_CORRECT had
1942*0Sstevel@tonic-gatebeen set.
1943*0Sstevel@tonic-gate
1944*0Sstevel@tonic-gate=item auto_abbrev
1945*0Sstevel@tonic-gate
1946*0Sstevel@tonic-gateAllow option names to be abbreviated to uniqueness.
1947*0Sstevel@tonic-gateDefault is enabled unless environment variable
1948*0Sstevel@tonic-gatePOSIXLY_CORRECT has been set, in which case C<auto_abbrev> is disabled.
1949*0Sstevel@tonic-gate
1950*0Sstevel@tonic-gate=item getopt_compat
1951*0Sstevel@tonic-gate
1952*0Sstevel@tonic-gateAllow C<+> to start options.
1953*0Sstevel@tonic-gateDefault is enabled unless environment variable
1954*0Sstevel@tonic-gatePOSIXLY_CORRECT has been set, in which case C<getopt_compat> is disabled.
1955*0Sstevel@tonic-gate
1956*0Sstevel@tonic-gate=item gnu_compat
1957*0Sstevel@tonic-gate
1958*0Sstevel@tonic-gateC<gnu_compat> controls whether C<--opt=> is allowed, and what it should
1959*0Sstevel@tonic-gatedo. Without C<gnu_compat>, C<--opt=> gives an error. With C<gnu_compat>,
1960*0Sstevel@tonic-gateC<--opt=> will give option C<opt> and empty value.
1961*0Sstevel@tonic-gateThis is the way GNU getopt_long() does it.
1962*0Sstevel@tonic-gate
1963*0Sstevel@tonic-gate=item gnu_getopt
1964*0Sstevel@tonic-gate
1965*0Sstevel@tonic-gateThis is a short way of setting C<gnu_compat> C<bundling> C<permute>
1966*0Sstevel@tonic-gateC<no_getopt_compat>. With C<gnu_getopt>, command line handling should be
1967*0Sstevel@tonic-gatefully compatible with GNU getopt_long().
1968*0Sstevel@tonic-gate
1969*0Sstevel@tonic-gate=item require_order
1970*0Sstevel@tonic-gate
1971*0Sstevel@tonic-gateWhether command line arguments are allowed to be mixed with options.
1972*0Sstevel@tonic-gateDefault is disabled unless environment variable
1973*0Sstevel@tonic-gatePOSIXLY_CORRECT has been set, in which case C<require_order> is enabled.
1974*0Sstevel@tonic-gate
1975*0Sstevel@tonic-gateSee also C<permute>, which is the opposite of C<require_order>.
1976*0Sstevel@tonic-gate
1977*0Sstevel@tonic-gate=item permute
1978*0Sstevel@tonic-gate
1979*0Sstevel@tonic-gateWhether command line arguments are allowed to be mixed with options.
1980*0Sstevel@tonic-gateDefault is enabled unless environment variable
1981*0Sstevel@tonic-gatePOSIXLY_CORRECT has been set, in which case C<permute> is disabled.
1982*0Sstevel@tonic-gateNote that C<permute> is the opposite of C<require_order>.
1983*0Sstevel@tonic-gate
1984*0Sstevel@tonic-gateIf C<permute> is enabled, this means that
1985*0Sstevel@tonic-gate
1986*0Sstevel@tonic-gate    --foo arg1 --bar arg2 arg3
1987*0Sstevel@tonic-gate
1988*0Sstevel@tonic-gateis equivalent to
1989*0Sstevel@tonic-gate
1990*0Sstevel@tonic-gate    --foo --bar arg1 arg2 arg3
1991*0Sstevel@tonic-gate
1992*0Sstevel@tonic-gateIf an argument callback routine is specified, C<@ARGV> will always be
1993*0Sstevel@tonic-gateempty upon succesful return of GetOptions() since all options have been
1994*0Sstevel@tonic-gateprocessed. The only exception is when C<--> is used:
1995*0Sstevel@tonic-gate
1996*0Sstevel@tonic-gate    --foo arg1 --bar arg2 -- arg3
1997*0Sstevel@tonic-gate
1998*0Sstevel@tonic-gateThis will call the callback routine for arg1 and arg2, and then
1999*0Sstevel@tonic-gateterminate GetOptions() leaving C<"arg2"> in C<@ARGV>.
2000*0Sstevel@tonic-gate
2001*0Sstevel@tonic-gateIf C<require_order> is enabled, options processing
2002*0Sstevel@tonic-gateterminates when the first non-option is encountered.
2003*0Sstevel@tonic-gate
2004*0Sstevel@tonic-gate    --foo arg1 --bar arg2 arg3
2005*0Sstevel@tonic-gate
2006*0Sstevel@tonic-gateis equivalent to
2007*0Sstevel@tonic-gate
2008*0Sstevel@tonic-gate    --foo -- arg1 --bar arg2 arg3
2009*0Sstevel@tonic-gate
2010*0Sstevel@tonic-gateIf C<pass_through> is also enabled, options processing will terminate
2011*0Sstevel@tonic-gateat the first unrecognized option, or non-option, whichever comes
2012*0Sstevel@tonic-gatefirst.
2013*0Sstevel@tonic-gate
2014*0Sstevel@tonic-gate=item bundling (default: disabled)
2015*0Sstevel@tonic-gate
2016*0Sstevel@tonic-gateEnabling this option will allow single-character options to be
2017*0Sstevel@tonic-gatebundled. To distinguish bundles from long option names, long options
2018*0Sstevel@tonic-gateI<must> be introduced with C<--> and bundles with C<->.
2019*0Sstevel@tonic-gate
2020*0Sstevel@tonic-gateNote that, if you have options C<a>, C<l> and C<all>, and
2021*0Sstevel@tonic-gateauto_abbrev enabled, possible arguments and option settings are:
2022*0Sstevel@tonic-gate
2023*0Sstevel@tonic-gate    using argument               sets option(s)
2024*0Sstevel@tonic-gate    ------------------------------------------
2025*0Sstevel@tonic-gate    -a, --a                      a
2026*0Sstevel@tonic-gate    -l, --l                      l
2027*0Sstevel@tonic-gate    -al, -la, -ala, -all,...     a, l
2028*0Sstevel@tonic-gate    --al, --all                  all
2029*0Sstevel@tonic-gate
2030*0Sstevel@tonic-gateThe suprising part is that C<--a> sets option C<a> (due to auto
2031*0Sstevel@tonic-gatecompletion), not C<all>.
2032*0Sstevel@tonic-gate
2033*0Sstevel@tonic-gateNote: disabling C<bundling> also disables C<bundling_override>.
2034*0Sstevel@tonic-gate
2035*0Sstevel@tonic-gate=item bundling_override (default: disabled)
2036*0Sstevel@tonic-gate
2037*0Sstevel@tonic-gateIf C<bundling_override> is enabled, bundling is enabled as with
2038*0Sstevel@tonic-gateC<bundling> but now long option names override option bundles.
2039*0Sstevel@tonic-gate
2040*0Sstevel@tonic-gateNote: disabling C<bundling_override> also disables C<bundling>.
2041*0Sstevel@tonic-gate
2042*0Sstevel@tonic-gateB<Note:> Using option bundling can easily lead to unexpected results,
2043*0Sstevel@tonic-gateespecially when mixing long options and bundles. Caveat emptor.
2044*0Sstevel@tonic-gate
2045*0Sstevel@tonic-gate=item ignore_case  (default: enabled)
2046*0Sstevel@tonic-gate
2047*0Sstevel@tonic-gateIf enabled, case is ignored when matching long option names. If,
2048*0Sstevel@tonic-gatehowever, bundling is enabled as well, single character options will be
2049*0Sstevel@tonic-gatetreated case-sensitive.
2050*0Sstevel@tonic-gate
2051*0Sstevel@tonic-gateWith C<ignore_case>, option specifications for options that only
2052*0Sstevel@tonic-gatediffer in case, e.g., C<"foo"> and C<"Foo">, will be flagged as
2053*0Sstevel@tonic-gateduplicates.
2054*0Sstevel@tonic-gate
2055*0Sstevel@tonic-gateNote: disabling C<ignore_case> also disables C<ignore_case_always>.
2056*0Sstevel@tonic-gate
2057*0Sstevel@tonic-gate=item ignore_case_always (default: disabled)
2058*0Sstevel@tonic-gate
2059*0Sstevel@tonic-gateWhen bundling is in effect, case is ignored on single-character
2060*0Sstevel@tonic-gateoptions also.
2061*0Sstevel@tonic-gate
2062*0Sstevel@tonic-gateNote: disabling C<ignore_case_always> also disables C<ignore_case>.
2063*0Sstevel@tonic-gate
2064*0Sstevel@tonic-gate=item auto_version (default:disabled)
2065*0Sstevel@tonic-gate
2066*0Sstevel@tonic-gateAutomatically provide support for the B<--version> option if
2067*0Sstevel@tonic-gatethe application did not specify a handler for this option itself.
2068*0Sstevel@tonic-gate
2069*0Sstevel@tonic-gateGetopt::Long will provide a standard version message that includes the
2070*0Sstevel@tonic-gateprogram name, its version (if $main::VERSION is defined), and the
2071*0Sstevel@tonic-gateversions of Getopt::Long and Perl. The message will be written to
2072*0Sstevel@tonic-gatestandard output and processing will terminate.
2073*0Sstevel@tonic-gate
2074*0Sstevel@tonic-gateC<auto_version> will be enabled if the calling program explicitly
2075*0Sstevel@tonic-gatespecified a version number higher than 2.32 in the C<use> or
2076*0Sstevel@tonic-gateC<require> statement.
2077*0Sstevel@tonic-gate
2078*0Sstevel@tonic-gate=item auto_help (default:disabled)
2079*0Sstevel@tonic-gate
2080*0Sstevel@tonic-gateAutomatically provide support for the B<--help> and B<-?> options if
2081*0Sstevel@tonic-gatethe application did not specify a handler for this option itself.
2082*0Sstevel@tonic-gate
2083*0Sstevel@tonic-gateGetopt::Long will provide a help message using module L<Pod::Usage>. The
2084*0Sstevel@tonic-gatemessage, derived from the SYNOPSIS POD section, will be written to
2085*0Sstevel@tonic-gatestandard output and processing will terminate.
2086*0Sstevel@tonic-gate
2087*0Sstevel@tonic-gateC<auto_help> will be enabled if the calling program explicitly
2088*0Sstevel@tonic-gatespecified a version number higher than 2.32 in the C<use> or
2089*0Sstevel@tonic-gateC<require> statement.
2090*0Sstevel@tonic-gate
2091*0Sstevel@tonic-gate=item pass_through (default: disabled)
2092*0Sstevel@tonic-gate
2093*0Sstevel@tonic-gateOptions that are unknown, ambiguous or supplied with an invalid option
2094*0Sstevel@tonic-gatevalue are passed through in C<@ARGV> instead of being flagged as
2095*0Sstevel@tonic-gateerrors. This makes it possible to write wrapper scripts that process
2096*0Sstevel@tonic-gateonly part of the user supplied command line arguments, and pass the
2097*0Sstevel@tonic-gateremaining options to some other program.
2098*0Sstevel@tonic-gate
2099*0Sstevel@tonic-gateIf C<require_order> is enabled, options processing will terminate at
2100*0Sstevel@tonic-gatethe first unrecognized option, or non-option, whichever comes first.
2101*0Sstevel@tonic-gateHowever, if C<permute> is enabled instead, results can become confusing.
2102*0Sstevel@tonic-gate
2103*0Sstevel@tonic-gateNote that the options terminator (default C<-->), if present, will
2104*0Sstevel@tonic-gatealso be passed through in C<@ARGV>.
2105*0Sstevel@tonic-gate
2106*0Sstevel@tonic-gate=item prefix
2107*0Sstevel@tonic-gate
2108*0Sstevel@tonic-gateThe string that starts options. If a constant string is not
2109*0Sstevel@tonic-gatesufficient, see C<prefix_pattern>.
2110*0Sstevel@tonic-gate
2111*0Sstevel@tonic-gate=item prefix_pattern
2112*0Sstevel@tonic-gate
2113*0Sstevel@tonic-gateA Perl pattern that identifies the strings that introduce options.
2114*0Sstevel@tonic-gateDefault is C<(--|-|\+)> unless environment variable
2115*0Sstevel@tonic-gatePOSIXLY_CORRECT has been set, in which case it is C<(--|-)>.
2116*0Sstevel@tonic-gate
2117*0Sstevel@tonic-gate=item debug (default: disabled)
2118*0Sstevel@tonic-gate
2119*0Sstevel@tonic-gateEnable debugging output.
2120*0Sstevel@tonic-gate
2121*0Sstevel@tonic-gate=back
2122*0Sstevel@tonic-gate
2123*0Sstevel@tonic-gate=head1 Exportable Methods
2124*0Sstevel@tonic-gate
2125*0Sstevel@tonic-gate=over
2126*0Sstevel@tonic-gate
2127*0Sstevel@tonic-gate=item VersionMessage
2128*0Sstevel@tonic-gate
2129*0Sstevel@tonic-gateThis subroutine provides a standard version message. Its argument can be:
2130*0Sstevel@tonic-gate
2131*0Sstevel@tonic-gate=over 4
2132*0Sstevel@tonic-gate
2133*0Sstevel@tonic-gate=item *
2134*0Sstevel@tonic-gate
2135*0Sstevel@tonic-gateA string containing the text of a message to print I<before> printing
2136*0Sstevel@tonic-gatethe standard message.
2137*0Sstevel@tonic-gate
2138*0Sstevel@tonic-gate=item *
2139*0Sstevel@tonic-gate
2140*0Sstevel@tonic-gateA numeric value corresponding to the desired exit status.
2141*0Sstevel@tonic-gate
2142*0Sstevel@tonic-gate=item *
2143*0Sstevel@tonic-gate
2144*0Sstevel@tonic-gateA reference to a hash.
2145*0Sstevel@tonic-gate
2146*0Sstevel@tonic-gate=back
2147*0Sstevel@tonic-gate
2148*0Sstevel@tonic-gateIf more than one argument is given then the entire argument list is
2149*0Sstevel@tonic-gateassumed to be a hash.  If a hash is supplied (either as a reference or
2150*0Sstevel@tonic-gateas a list) it should contain one or more elements with the following
2151*0Sstevel@tonic-gatekeys:
2152*0Sstevel@tonic-gate
2153*0Sstevel@tonic-gate=over 4
2154*0Sstevel@tonic-gate
2155*0Sstevel@tonic-gate=item C<-message>
2156*0Sstevel@tonic-gate
2157*0Sstevel@tonic-gate=item C<-msg>
2158*0Sstevel@tonic-gate
2159*0Sstevel@tonic-gateThe text of a message to print immediately prior to printing the
2160*0Sstevel@tonic-gateprogram's usage message.
2161*0Sstevel@tonic-gate
2162*0Sstevel@tonic-gate=item C<-exitval>
2163*0Sstevel@tonic-gate
2164*0Sstevel@tonic-gateThe desired exit status to pass to the B<exit()> function.
2165*0Sstevel@tonic-gateThis should be an integer, or else the string "NOEXIT" to
2166*0Sstevel@tonic-gateindicate that control should simply be returned without
2167*0Sstevel@tonic-gateterminating the invoking process.
2168*0Sstevel@tonic-gate
2169*0Sstevel@tonic-gate=item C<-output>
2170*0Sstevel@tonic-gate
2171*0Sstevel@tonic-gateA reference to a filehandle, or the pathname of a file to which the
2172*0Sstevel@tonic-gateusage message should be written. The default is C<\*STDERR> unless the
2173*0Sstevel@tonic-gateexit value is less than 2 (in which case the default is C<\*STDOUT>).
2174*0Sstevel@tonic-gate
2175*0Sstevel@tonic-gate=back
2176*0Sstevel@tonic-gate
2177*0Sstevel@tonic-gateYou cannot tie this routine directly to an option, e.g.:
2178*0Sstevel@tonic-gate
2179*0Sstevel@tonic-gate    GetOptions("version" => \&VersionMessage);
2180*0Sstevel@tonic-gate
2181*0Sstevel@tonic-gateUse this instead:
2182*0Sstevel@tonic-gate
2183*0Sstevel@tonic-gate    GetOptions("version" => sub { VersionMessage() });
2184*0Sstevel@tonic-gate
2185*0Sstevel@tonic-gate=item HelpMessage
2186*0Sstevel@tonic-gate
2187*0Sstevel@tonic-gateThis subroutine produces a standard help message, derived from the
2188*0Sstevel@tonic-gateprogram's POD section SYNOPSIS using L<Pod::Usage>. It takes the same
2189*0Sstevel@tonic-gatearguments as VersionMessage(). In particular, you cannot tie it
2190*0Sstevel@tonic-gatedirectly to an option, e.g.:
2191*0Sstevel@tonic-gate
2192*0Sstevel@tonic-gate    GetOptions("help" => \&HelpMessage);
2193*0Sstevel@tonic-gate
2194*0Sstevel@tonic-gateUse this instead:
2195*0Sstevel@tonic-gate
2196*0Sstevel@tonic-gate    GetOptions("help" => sub { HelpMessage() });
2197*0Sstevel@tonic-gate
2198*0Sstevel@tonic-gate=back
2199*0Sstevel@tonic-gate
2200*0Sstevel@tonic-gate=head1 Return values and Errors
2201*0Sstevel@tonic-gate
2202*0Sstevel@tonic-gateConfiguration errors and errors in the option definitions are
2203*0Sstevel@tonic-gatesignalled using die() and will terminate the calling program unless
2204*0Sstevel@tonic-gatethe call to Getopt::Long::GetOptions() was embedded in C<eval { ...
2205*0Sstevel@tonic-gate}>, or die() was trapped using C<$SIG{__DIE__}>.
2206*0Sstevel@tonic-gate
2207*0Sstevel@tonic-gateGetOptions returns true to indicate success.
2208*0Sstevel@tonic-gateIt returns false when the function detected one or more errors during
2209*0Sstevel@tonic-gateoption parsing. These errors are signalled using warn() and can be
2210*0Sstevel@tonic-gatetrapped with C<$SIG{__WARN__}>.
2211*0Sstevel@tonic-gate
2212*0Sstevel@tonic-gate=head1 Legacy
2213*0Sstevel@tonic-gate
2214*0Sstevel@tonic-gateThe earliest development of C<newgetopt.pl> started in 1990, with Perl
2215*0Sstevel@tonic-gateversion 4. As a result, its development, and the development of
2216*0Sstevel@tonic-gateGetopt::Long, has gone through several stages. Since backward
2217*0Sstevel@tonic-gatecompatibility has always been extremely important, the current version
2218*0Sstevel@tonic-gateof Getopt::Long still supports a lot of constructs that nowadays are
2219*0Sstevel@tonic-gateno longer necessary or otherwise unwanted. This section describes
2220*0Sstevel@tonic-gatebriefly some of these 'features'.
2221*0Sstevel@tonic-gate
2222*0Sstevel@tonic-gate=head2 Default destinations
2223*0Sstevel@tonic-gate
2224*0Sstevel@tonic-gateWhen no destination is specified for an option, GetOptions will store
2225*0Sstevel@tonic-gatethe resultant value in a global variable named C<opt_>I<XXX>, where
2226*0Sstevel@tonic-gateI<XXX> is the primary name of this option. When a progam executes
2227*0Sstevel@tonic-gateunder C<use strict> (recommended), these variables must be
2228*0Sstevel@tonic-gatepre-declared with our() or C<use vars>.
2229*0Sstevel@tonic-gate
2230*0Sstevel@tonic-gate    our $opt_length = 0;
2231*0Sstevel@tonic-gate    GetOptions ('length=i');	# will store in $opt_length
2232*0Sstevel@tonic-gate
2233*0Sstevel@tonic-gateTo yield a usable Perl variable, characters that are not part of the
2234*0Sstevel@tonic-gatesyntax for variables are translated to underscores. For example,
2235*0Sstevel@tonic-gateC<--fpp-struct-return> will set the variable
2236*0Sstevel@tonic-gateC<$opt_fpp_struct_return>. Note that this variable resides in the
2237*0Sstevel@tonic-gatenamespace of the calling program, not necessarily C<main>. For
2238*0Sstevel@tonic-gateexample:
2239*0Sstevel@tonic-gate
2240*0Sstevel@tonic-gate    GetOptions ("size=i", "sizes=i@");
2241*0Sstevel@tonic-gate
2242*0Sstevel@tonic-gatewith command line "-size 10 -sizes 24 -sizes 48" will perform the
2243*0Sstevel@tonic-gateequivalent of the assignments
2244*0Sstevel@tonic-gate
2245*0Sstevel@tonic-gate    $opt_size = 10;
2246*0Sstevel@tonic-gate    @opt_sizes = (24, 48);
2247*0Sstevel@tonic-gate
2248*0Sstevel@tonic-gate=head2 Alternative option starters
2249*0Sstevel@tonic-gate
2250*0Sstevel@tonic-gateA string of alternative option starter characters may be passed as the
2251*0Sstevel@tonic-gatefirst argument (or the first argument after a leading hash reference
2252*0Sstevel@tonic-gateargument).
2253*0Sstevel@tonic-gate
2254*0Sstevel@tonic-gate    my $len = 0;
2255*0Sstevel@tonic-gate    GetOptions ('/', 'length=i' => $len);
2256*0Sstevel@tonic-gate
2257*0Sstevel@tonic-gateNow the command line may look like:
2258*0Sstevel@tonic-gate
2259*0Sstevel@tonic-gate    /length 24 -- arg
2260*0Sstevel@tonic-gate
2261*0Sstevel@tonic-gateNote that to terminate options processing still requires a double dash
2262*0Sstevel@tonic-gateC<-->.
2263*0Sstevel@tonic-gate
2264*0Sstevel@tonic-gateGetOptions() will not interpret a leading C<< "<>" >> as option starters
2265*0Sstevel@tonic-gateif the next argument is a reference. To force C<< "<" >> and C<< ">" >> as
2266*0Sstevel@tonic-gateoption starters, use C<< "><" >>. Confusing? Well, B<using a starter
2267*0Sstevel@tonic-gateargument is strongly deprecated> anyway.
2268*0Sstevel@tonic-gate
2269*0Sstevel@tonic-gate=head2 Configuration variables
2270*0Sstevel@tonic-gate
2271*0Sstevel@tonic-gatePrevious versions of Getopt::Long used variables for the purpose of
2272*0Sstevel@tonic-gateconfiguring. Although manipulating these variables still work, it is
2273*0Sstevel@tonic-gatestrongly encouraged to use the C<Configure> routine that was introduced
2274*0Sstevel@tonic-gatein version 2.17. Besides, it is much easier.
2275*0Sstevel@tonic-gate
2276*0Sstevel@tonic-gate=head1 Trouble Shooting
2277*0Sstevel@tonic-gate
2278*0Sstevel@tonic-gate=head2 GetOptions does not return a false result when an option is not supplied
2279*0Sstevel@tonic-gate
2280*0Sstevel@tonic-gateThat's why they're called 'options'.
2281*0Sstevel@tonic-gate
2282*0Sstevel@tonic-gate=head2 GetOptions does not split the command line correctly
2283*0Sstevel@tonic-gate
2284*0Sstevel@tonic-gateThe command line is not split by GetOptions, but by the command line
2285*0Sstevel@tonic-gateinterpreter (CLI). On Unix, this is the shell. On Windows, it is
2286*0Sstevel@tonic-gateCOMMAND.COM or CMD.EXE. Other operating systems have other CLIs.
2287*0Sstevel@tonic-gate
2288*0Sstevel@tonic-gateIt is important to know that these CLIs may behave different when the
2289*0Sstevel@tonic-gatecommand line contains special characters, in particular quotes or
2290*0Sstevel@tonic-gatebackslashes. For example, with Unix shells you can use single quotes
2291*0Sstevel@tonic-gate(C<'>) and double quotes (C<">) to group words together. The following
2292*0Sstevel@tonic-gatealternatives are equivalent on Unix:
2293*0Sstevel@tonic-gate
2294*0Sstevel@tonic-gate    "two words"
2295*0Sstevel@tonic-gate    'two words'
2296*0Sstevel@tonic-gate    two\ words
2297*0Sstevel@tonic-gate
2298*0Sstevel@tonic-gateIn case of doubt, insert the following statement in front of your Perl
2299*0Sstevel@tonic-gateprogram:
2300*0Sstevel@tonic-gate
2301*0Sstevel@tonic-gate    print STDERR (join("|",@ARGV),"\n");
2302*0Sstevel@tonic-gate
2303*0Sstevel@tonic-gateto verify how your CLI passes the arguments to the program.
2304*0Sstevel@tonic-gate
2305*0Sstevel@tonic-gate=head2 Undefined subroutine &main::GetOptions called
2306*0Sstevel@tonic-gate
2307*0Sstevel@tonic-gateAre you running Windows, and did you write
2308*0Sstevel@tonic-gate
2309*0Sstevel@tonic-gate    use GetOpt::Long;
2310*0Sstevel@tonic-gate
2311*0Sstevel@tonic-gate(note the capital 'O')?
2312*0Sstevel@tonic-gate
2313*0Sstevel@tonic-gate=head2 How do I put a "-?" option into a Getopt::Long?
2314*0Sstevel@tonic-gate
2315*0Sstevel@tonic-gateYou can only obtain this using an alias, and Getopt::Long of at least
2316*0Sstevel@tonic-gateversion 2.13.
2317*0Sstevel@tonic-gate
2318*0Sstevel@tonic-gate    use Getopt::Long;
2319*0Sstevel@tonic-gate    GetOptions ("help|?");    # -help and -? will both set $opt_help
2320*0Sstevel@tonic-gate
2321*0Sstevel@tonic-gate=head1 AUTHOR
2322*0Sstevel@tonic-gate
2323*0Sstevel@tonic-gateJohan Vromans <jvromans@squirrel.nl>
2324*0Sstevel@tonic-gate
2325*0Sstevel@tonic-gate=head1 COPYRIGHT AND DISCLAIMER
2326*0Sstevel@tonic-gate
2327*0Sstevel@tonic-gateThis program is Copyright 2003,1990 by Johan Vromans.
2328*0Sstevel@tonic-gateThis program is free software; you can redistribute it and/or
2329*0Sstevel@tonic-gatemodify it under the terms of the Perl Artistic License or the
2330*0Sstevel@tonic-gateGNU General Public License as published by the Free Software
2331*0Sstevel@tonic-gateFoundation; either version 2 of the License, or (at your option) any
2332*0Sstevel@tonic-gatelater version.
2333*0Sstevel@tonic-gate
2334*0Sstevel@tonic-gateThis program is distributed in the hope that it will be useful,
2335*0Sstevel@tonic-gatebut WITHOUT ANY WARRANTY; without even the implied warranty of
2336*0Sstevel@tonic-gateMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2337*0Sstevel@tonic-gateGNU General Public License for more details.
2338*0Sstevel@tonic-gate
2339*0Sstevel@tonic-gateIf you do not have a copy of the GNU General Public License write to
2340*0Sstevel@tonic-gatethe Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
2341*0Sstevel@tonic-gateMA 02139, USA.
2342*0Sstevel@tonic-gate
2343*0Sstevel@tonic-gate=cut
2344*0Sstevel@tonic-gate
2345