1e9ce3842Safresh1#! perl 2e9ce3842Safresh1 3b39c5158Smillert# Getopt::Long.pm -- Universal options parsing 4b39c5158Smillert# Author : Johan Vromans 5b39c5158Smillert# Created On : Tue Sep 11 15:00:12 1990 6*3d61058aSafresh1# Last Modified On: Sat Nov 11 17:48:41 2023 7*3d61058aSafresh1# Update Count : 1808 8b39c5158Smillert# Status : Released 9b39c5158Smillert 10b39c5158Smillert################ Module Preamble ################ 11b39c5158Smillert 12*3d61058aSafresh1# Getopt::Long is reported to run under 5.6.1. Thanks Tux! 13*3d61058aSafresh1use 5.006001; 14b39c5158Smillert 15b39c5158Smillertuse strict; 169f11ffb7Safresh1use warnings; 179f11ffb7Safresh1 189f11ffb7Safresh1package Getopt::Long; 19b39c5158Smillert 20*3d61058aSafresh1our $VERSION = 2.57; 21b39c5158Smillert 22b39c5158Smillertuse Exporter; 23*3d61058aSafresh1use base qw(Exporter); 24b39c5158Smillert 25b39c5158Smillert# Exported subroutines. 26b39c5158Smillertsub GetOptions(@); # always 27b39c5158Smillertsub GetOptionsFromArray(@); # on demand 28b39c5158Smillertsub GetOptionsFromString(@); # on demand 29b39c5158Smillertsub Configure(@); # on demand 30b39c5158Smillertsub HelpMessage(@); # on demand 31b39c5158Smillertsub VersionMessage(@); # in demand 32b39c5158Smillert 33*3d61058aSafresh1our @EXPORT; 34*3d61058aSafresh1our @EXPORT_OK; 35*3d61058aSafresh1# Values for $order. See GNU getopt.c for details. 36*3d61058aSafresh1our ($REQUIRE_ORDER, $PERMUTE, $RETURN_IN_ORDER); 37b39c5158SmillertBEGIN { 38*3d61058aSafresh1 ($REQUIRE_ORDER, $PERMUTE, $RETURN_IN_ORDER) = (0..2); 39b39c5158Smillert @EXPORT = qw(&GetOptions $REQUIRE_ORDER $PERMUTE $RETURN_IN_ORDER); 40b39c5158Smillert @EXPORT_OK = qw(&HelpMessage &VersionMessage &Configure 41b39c5158Smillert &GetOptionsFromArray &GetOptionsFromString); 42b39c5158Smillert} 43b39c5158Smillert 44b39c5158Smillert# User visible variables. 45*3d61058aSafresh1our ($error, $debug, $major_version, $minor_version); 46b39c5158Smillert# Deprecated visible variables. 47*3d61058aSafresh1our ($autoabbrev, $getopt_compat, $ignorecase, $bundling, $order, 48b39c5158Smillert $passthrough); 49b39c5158Smillert# Official invisible variables. 50*3d61058aSafresh1our ($genprefix, $caller, $gnu_compat, $auto_help, $auto_version, $longprefix); 51b39c5158Smillert 52b8851fccSafresh1# Really invisible variables. 53b8851fccSafresh1my $bundling_values; 54b8851fccSafresh1 55b39c5158Smillert# Public subroutines. 56b39c5158Smillertsub config(@); # deprecated name 57b39c5158Smillert 58b39c5158Smillert# Private subroutines. 59b39c5158Smillertsub ConfigDefaults(); 60b39c5158Smillertsub ParseOptionSpec($$); 61b39c5158Smillertsub OptCtl($); 62b39c5158Smillertsub FindOption($$$$$); 63b39c5158Smillertsub ValidValue ($$$$$); 64b39c5158Smillert 65b39c5158Smillert################ Local Variables ################ 66b39c5158Smillert 67b39c5158Smillert# $requested_version holds the version that was mentioned in the 'use' 68b39c5158Smillert# or 'require', if any. It can be used to enable or disable specific 69b39c5158Smillert# features. 70b39c5158Smillertmy $requested_version = 0; 71b39c5158Smillert 72b39c5158Smillert################ Resident subroutines ################ 73b39c5158Smillert 74b39c5158Smillertsub ConfigDefaults() { 75b39c5158Smillert # Handle POSIX compliancy. 76b39c5158Smillert if ( defined $ENV{"POSIXLY_CORRECT"} ) { 77b39c5158Smillert $genprefix = "(--|-)"; 78b39c5158Smillert $autoabbrev = 0; # no automatic abbrev of options 79b39c5158Smillert $bundling = 0; # no bundling of single letter switches 80b39c5158Smillert $getopt_compat = 0; # disallow '+' to start options 81b39c5158Smillert $order = $REQUIRE_ORDER; 82b39c5158Smillert } 83b39c5158Smillert else { 84b39c5158Smillert $genprefix = "(--|-|\\+)"; 85b39c5158Smillert $autoabbrev = 1; # automatic abbrev of options 86b39c5158Smillert $bundling = 0; # bundling off by default 87b39c5158Smillert $getopt_compat = 1; # allow '+' to start options 88b39c5158Smillert $order = $PERMUTE; 89b39c5158Smillert } 90b39c5158Smillert # Other configurable settings. 91b39c5158Smillert $debug = 0; # for debugging 92b39c5158Smillert $error = 0; # error tally 93b39c5158Smillert $ignorecase = 1; # ignore case when matching options 94b39c5158Smillert $passthrough = 0; # leave unrecognized options alone 95b39c5158Smillert $gnu_compat = 0; # require --opt=val if value is optional 96b39c5158Smillert $longprefix = "(--)"; # what does a long prefix look like 97b8851fccSafresh1 $bundling_values = 0; # no bundling of values 98b39c5158Smillert} 99b39c5158Smillert 100b39c5158Smillert# Override import. 101b39c5158Smillertsub import { 102b39c5158Smillert my $pkg = shift; # package 103b39c5158Smillert my @syms = (); # symbols to import 104b39c5158Smillert my @config = (); # configuration 105b39c5158Smillert my $dest = \@syms; # symbols first 106b39c5158Smillert for ( @_ ) { 107b39c5158Smillert if ( $_ eq ':config' ) { 108b39c5158Smillert $dest = \@config; # config next 109b39c5158Smillert next; 110b39c5158Smillert } 111b39c5158Smillert push(@$dest, $_); # push 112b39c5158Smillert } 113b39c5158Smillert # Hide one level and call super. 114b39c5158Smillert local $Exporter::ExportLevel = 1; 115b39c5158Smillert push(@syms, qw(&GetOptions)) if @syms; # always export GetOptions 116e9ce3842Safresh1 $requested_version = 0; 117b39c5158Smillert $pkg->SUPER::import(@syms); 118b39c5158Smillert # And configure. 119b39c5158Smillert Configure(@config) if @config; 120b39c5158Smillert} 121b39c5158Smillert 122b39c5158Smillert################ Initialization ################ 123b39c5158Smillert 124b39c5158Smillert# Version major/minor numbers. 125b39c5158Smillert($major_version, $minor_version) = $VERSION =~ /^(\d+)\.(\d+)/; 126b39c5158Smillert 127b39c5158SmillertConfigDefaults(); 128b39c5158Smillert 129b39c5158Smillert# Store a copy of the default configuration. Since ConfigDefaults has 130b39c5158Smillert# just been called, what we get from Configure is the default. 131b39c5158Smillertmy $default_config = do { 132b39c5158Smillert Getopt::Long::Configure () 133b39c5158Smillert}; 134b39c5158Smillert 135*3d61058aSafresh1# For the parser only. 136*3d61058aSafresh1sub _default_config { $default_config } 137b39c5158Smillert 138b39c5158Smillert################ Back to Normal ################ 139b39c5158Smillert 140*3d61058aSafresh1# The ooparser was traditionally part of the main package. 141*3d61058aSafresh1no warnings 'redefine'; 142*3d61058aSafresh1sub Getopt::Long::Parser::new { 143*3d61058aSafresh1 require Getopt::Long::Parser; 144*3d61058aSafresh1 goto &Getopt::Long::Parser::new; 145*3d61058aSafresh1} 146*3d61058aSafresh1use warnings 'redefine'; 147*3d61058aSafresh1 148b39c5158Smillert# Indices in option control info. 149b39c5158Smillert# Note that ParseOptions uses the fields directly. Search for 'hard-wired'. 150b39c5158Smillertuse constant CTL_TYPE => 0; 151b39c5158Smillert#use constant CTL_TYPE_FLAG => ''; 152b39c5158Smillert#use constant CTL_TYPE_NEG => '!'; 153b39c5158Smillert#use constant CTL_TYPE_INCR => '+'; 154b39c5158Smillert#use constant CTL_TYPE_INT => 'i'; 155b39c5158Smillert#use constant CTL_TYPE_INTINC => 'I'; 156b39c5158Smillert#use constant CTL_TYPE_XINT => 'o'; 157b39c5158Smillert#use constant CTL_TYPE_FLOAT => 'f'; 158b39c5158Smillert#use constant CTL_TYPE_STRING => 's'; 159b39c5158Smillert 160b39c5158Smillertuse constant CTL_CNAME => 1; 161b39c5158Smillert 162b39c5158Smillertuse constant CTL_DEFAULT => 2; 163b39c5158Smillert 164b39c5158Smillertuse constant CTL_DEST => 3; 165b39c5158Smillert use constant CTL_DEST_SCALAR => 0; 166b39c5158Smillert use constant CTL_DEST_ARRAY => 1; 167b39c5158Smillert use constant CTL_DEST_HASH => 2; 168b39c5158Smillert use constant CTL_DEST_CODE => 3; 169b39c5158Smillert 170b39c5158Smillertuse constant CTL_AMIN => 4; 171b39c5158Smillertuse constant CTL_AMAX => 5; 172b39c5158Smillert 173b39c5158Smillert# FFU. 174b39c5158Smillert#use constant CTL_RANGE => ; 175b39c5158Smillert#use constant CTL_REPEAT => ; 176b39c5158Smillert 177b39c5158Smillert# Rather liberal patterns to match numbers. 178b39c5158Smillertuse constant PAT_INT => "[-+]?_*[0-9][0-9_]*"; 179b39c5158Smillertuse constant PAT_XINT => 180b39c5158Smillert "(?:". 181b39c5158Smillert "[-+]?_*[1-9][0-9_]*". 182b39c5158Smillert "|". 183b39c5158Smillert "0x_*[0-9a-f][0-9a-f_]*". 184b39c5158Smillert "|". 185b39c5158Smillert "0b_*[01][01_]*". 186b39c5158Smillert "|". 187b39c5158Smillert "0[0-7_]*". 188b39c5158Smillert ")"; 189b8851fccSafresh1use constant PAT_FLOAT => 190b8851fccSafresh1 "[-+]?". # optional sign 191e0680481Safresh1 "(?=\\.?[0-9])". # must start with digit or dec.point 192b8851fccSafresh1 "[0-9_]*". # digits before the dec.point 193e0680481Safresh1 "(\\.[0-9_]*)?". # optional fraction 194b8851fccSafresh1 "([eE][-+]?[0-9_]+)?"; # optional exponent 195b39c5158Smillert 196b39c5158Smillertsub GetOptions(@) { 197b39c5158Smillert # Shift in default array. 198b39c5158Smillert unshift(@_, \@ARGV); 199e9ce3842Safresh1 # Try to keep caller() and Carp consistent. 200b39c5158Smillert goto &GetOptionsFromArray; 201b39c5158Smillert} 202b39c5158Smillert 203b39c5158Smillertsub GetOptionsFromString(@) { 204b39c5158Smillert my ($string) = shift; 205b39c5158Smillert require Text::ParseWords; 206b39c5158Smillert my $args = [ Text::ParseWords::shellwords($string) ]; 207b39c5158Smillert $caller ||= (caller)[0]; # current context 208b39c5158Smillert my $ret = GetOptionsFromArray($args, @_); 209b39c5158Smillert return ( $ret, $args ) if wantarray; 210b39c5158Smillert if ( @$args ) { 211b39c5158Smillert $ret = 0; 212b39c5158Smillert warn("GetOptionsFromString: Excess data \"@$args\" in string \"$string\"\n"); 213b39c5158Smillert } 214b39c5158Smillert $ret; 215b39c5158Smillert} 216b39c5158Smillert 217b39c5158Smillertsub GetOptionsFromArray(@) { 218b39c5158Smillert 219b39c5158Smillert my ($argv, @optionlist) = @_; # local copy of the option descriptions 220b39c5158Smillert my $argend = '--'; # option list terminator 221b39c5158Smillert my %opctl = (); # table of option specs 222b39c5158Smillert my $pkg = $caller || (caller)[0]; # current context 223b39c5158Smillert # Needed if linkage is omitted. 224b39c5158Smillert my @ret = (); # accum for non-options 225b39c5158Smillert my %linkage; # linkage 226b39c5158Smillert my $userlinkage; # user supplied HASH 227b39c5158Smillert my $opt; # current option 228b39c5158Smillert my $prefix = $genprefix; # current prefix 229b39c5158Smillert 230b39c5158Smillert $error = ''; 231b39c5158Smillert 232b39c5158Smillert if ( $debug ) { 233b39c5158Smillert # Avoid some warnings if debugging. 234b39c5158Smillert local ($^W) = 0; 235b39c5158Smillert print STDERR 236*3d61058aSafresh1 ("Getopt::Long $VERSION ", 237b39c5158Smillert "called from package \"$pkg\".", 238b39c5158Smillert "\n ", 239b8851fccSafresh1 "argv: ", 240b8851fccSafresh1 defined($argv) 241b8851fccSafresh1 ? UNIVERSAL::isa( $argv, 'ARRAY' ) ? "(@$argv)" : $argv 242b8851fccSafresh1 : "<undef>", 243b39c5158Smillert "\n ", 244b39c5158Smillert "autoabbrev=$autoabbrev,". 245b39c5158Smillert "bundling=$bundling,", 246b8851fccSafresh1 "bundling_values=$bundling_values,", 247b39c5158Smillert "getopt_compat=$getopt_compat,", 248b39c5158Smillert "gnu_compat=$gnu_compat,", 249b39c5158Smillert "order=$order,", 250b39c5158Smillert "\n ", 251b39c5158Smillert "ignorecase=$ignorecase,", 252b39c5158Smillert "requested_version=$requested_version,", 253b39c5158Smillert "passthrough=$passthrough,", 254b39c5158Smillert "genprefix=\"$genprefix\",", 255b39c5158Smillert "longprefix=\"$longprefix\".", 256b39c5158Smillert "\n"); 257b39c5158Smillert } 258b39c5158Smillert 259b39c5158Smillert # Check for ref HASH as first argument. 260b39c5158Smillert # First argument may be an object. It's OK to use this as long 261b39c5158Smillert # as it is really a hash underneath. 262b39c5158Smillert $userlinkage = undef; 263b39c5158Smillert if ( @optionlist && ref($optionlist[0]) and 264b39c5158Smillert UNIVERSAL::isa($optionlist[0],'HASH') ) { 265b39c5158Smillert $userlinkage = shift (@optionlist); 266b39c5158Smillert print STDERR ("=> user linkage: $userlinkage\n") if $debug; 267b39c5158Smillert } 268b39c5158Smillert 269b39c5158Smillert # See if the first element of the optionlist contains option 270b39c5158Smillert # starter characters. 271b39c5158Smillert # Be careful not to interpret '<>' as option starters. 272b39c5158Smillert if ( @optionlist && $optionlist[0] =~ /^\W+$/ 273b39c5158Smillert && !($optionlist[0] eq '<>' 274b39c5158Smillert && @optionlist > 0 275b39c5158Smillert && ref($optionlist[1])) ) { 276b39c5158Smillert $prefix = shift (@optionlist); 277b39c5158Smillert # Turn into regexp. Needs to be parenthesized! 278b39c5158Smillert $prefix =~ s/(\W)/\\$1/g; 279b39c5158Smillert $prefix = "([" . $prefix . "])"; 280b39c5158Smillert print STDERR ("=> prefix=\"$prefix\"\n") if $debug; 281b39c5158Smillert } 282b39c5158Smillert 283b39c5158Smillert # Verify correctness of optionlist. 284b39c5158Smillert %opctl = (); 285b39c5158Smillert while ( @optionlist ) { 286b39c5158Smillert my $opt = shift (@optionlist); 287b39c5158Smillert 288b39c5158Smillert unless ( defined($opt) ) { 289b39c5158Smillert $error .= "Undefined argument in option spec\n"; 290b39c5158Smillert next; 291b39c5158Smillert } 292b39c5158Smillert 293b39c5158Smillert # Strip leading prefix so people can specify "--foo=i" if they like. 294b39c5158Smillert $opt = $+ if $opt =~ /^$prefix+(.*)$/s; 295b39c5158Smillert 296b39c5158Smillert if ( $opt eq '<>' ) { 297b39c5158Smillert if ( (defined $userlinkage) 298b39c5158Smillert && !(@optionlist > 0 && ref($optionlist[0])) 299b39c5158Smillert && (exists $userlinkage->{$opt}) 300b39c5158Smillert && ref($userlinkage->{$opt}) ) { 301b39c5158Smillert unshift (@optionlist, $userlinkage->{$opt}); 302b39c5158Smillert } 303b39c5158Smillert unless ( @optionlist > 0 304b39c5158Smillert && ref($optionlist[0]) && ref($optionlist[0]) eq 'CODE' ) { 305b39c5158Smillert $error .= "Option spec <> requires a reference to a subroutine\n"; 306b39c5158Smillert # Kill the linkage (to avoid another error). 307b39c5158Smillert shift (@optionlist) 308b39c5158Smillert if @optionlist && ref($optionlist[0]); 309b39c5158Smillert next; 310b39c5158Smillert } 311b39c5158Smillert $linkage{'<>'} = shift (@optionlist); 312b39c5158Smillert next; 313b39c5158Smillert } 314b39c5158Smillert 315b39c5158Smillert # Parse option spec. 316b39c5158Smillert my ($name, $orig) = ParseOptionSpec ($opt, \%opctl); 317b39c5158Smillert unless ( defined $name ) { 318b39c5158Smillert # Failed. $orig contains the error message. Sorry for the abuse. 319b39c5158Smillert $error .= $orig; 320b39c5158Smillert # Kill the linkage (to avoid another error). 321b39c5158Smillert shift (@optionlist) 322b39c5158Smillert if @optionlist && ref($optionlist[0]); 323b39c5158Smillert next; 324b39c5158Smillert } 325b39c5158Smillert 326b39c5158Smillert # If no linkage is supplied in the @optionlist, copy it from 327b39c5158Smillert # the userlinkage if available. 328b39c5158Smillert if ( defined $userlinkage ) { 329b39c5158Smillert unless ( @optionlist > 0 && ref($optionlist[0]) ) { 330b39c5158Smillert if ( exists $userlinkage->{$orig} && 331b39c5158Smillert ref($userlinkage->{$orig}) ) { 332b39c5158Smillert print STDERR ("=> found userlinkage for \"$orig\": ", 333b39c5158Smillert "$userlinkage->{$orig}\n") 334b39c5158Smillert if $debug; 335b39c5158Smillert unshift (@optionlist, $userlinkage->{$orig}); 336b39c5158Smillert } 337b39c5158Smillert else { 338b39c5158Smillert # Do nothing. Being undefined will be handled later. 339b39c5158Smillert next; 340b39c5158Smillert } 341b39c5158Smillert } 342b39c5158Smillert } 343b39c5158Smillert 344b39c5158Smillert # Copy the linkage. If omitted, link to global variable. 345b39c5158Smillert if ( @optionlist > 0 && ref($optionlist[0]) ) { 346b39c5158Smillert print STDERR ("=> link \"$orig\" to $optionlist[0]\n") 347b39c5158Smillert if $debug; 348b39c5158Smillert my $rl = ref($linkage{$orig} = shift (@optionlist)); 349b39c5158Smillert 350b39c5158Smillert if ( $rl eq "ARRAY" ) { 351b39c5158Smillert $opctl{$name}[CTL_DEST] = CTL_DEST_ARRAY; 352b39c5158Smillert } 353b39c5158Smillert elsif ( $rl eq "HASH" ) { 354b39c5158Smillert $opctl{$name}[CTL_DEST] = CTL_DEST_HASH; 355b39c5158Smillert } 356b39c5158Smillert elsif ( $rl eq "SCALAR" || $rl eq "REF" ) { 357b39c5158Smillert# if ( $opctl{$name}[CTL_DEST] == CTL_DEST_ARRAY ) { 358b39c5158Smillert# my $t = $linkage{$orig}; 359b39c5158Smillert# $$t = $linkage{$orig} = []; 360b39c5158Smillert# } 361b39c5158Smillert# elsif ( $opctl{$name}[CTL_DEST] == CTL_DEST_HASH ) { 362b39c5158Smillert# } 363b39c5158Smillert# else { 364b39c5158Smillert # Ok. 365b39c5158Smillert# } 366b39c5158Smillert } 367b39c5158Smillert elsif ( $rl eq "CODE" ) { 368b39c5158Smillert # Ok. 369b39c5158Smillert } 370b39c5158Smillert else { 371b39c5158Smillert $error .= "Invalid option linkage for \"$opt\"\n"; 372b39c5158Smillert } 373b39c5158Smillert } 374b39c5158Smillert else { 375b39c5158Smillert # Link to global $opt_XXX variable. 376b39c5158Smillert # Make sure a valid perl identifier results. 377b39c5158Smillert my $ov = $orig; 378b39c5158Smillert $ov =~ s/\W/_/g; 379b39c5158Smillert if ( $opctl{$name}[CTL_DEST] == CTL_DEST_ARRAY ) { 380b39c5158Smillert print STDERR ("=> link \"$orig\" to \@$pkg","::opt_$ov\n") 381b39c5158Smillert if $debug; 382b39c5158Smillert eval ("\$linkage{\$orig} = \\\@".$pkg."::opt_$ov;"); 383b39c5158Smillert } 384b39c5158Smillert elsif ( $opctl{$name}[CTL_DEST] == CTL_DEST_HASH ) { 385b39c5158Smillert print STDERR ("=> link \"$orig\" to \%$pkg","::opt_$ov\n") 386b39c5158Smillert if $debug; 387b39c5158Smillert eval ("\$linkage{\$orig} = \\\%".$pkg."::opt_$ov;"); 388b39c5158Smillert } 389b39c5158Smillert else { 390b39c5158Smillert print STDERR ("=> link \"$orig\" to \$$pkg","::opt_$ov\n") 391b39c5158Smillert if $debug; 392b39c5158Smillert eval ("\$linkage{\$orig} = \\\$".$pkg."::opt_$ov;"); 393b39c5158Smillert } 394b39c5158Smillert } 395b39c5158Smillert 396b39c5158Smillert if ( $opctl{$name}[CTL_TYPE] eq 'I' 397b39c5158Smillert && ( $opctl{$name}[CTL_DEST] == CTL_DEST_ARRAY 398b39c5158Smillert || $opctl{$name}[CTL_DEST] == CTL_DEST_HASH ) 399b39c5158Smillert ) { 400b39c5158Smillert $error .= "Invalid option linkage for \"$opt\"\n"; 401b39c5158Smillert } 402b39c5158Smillert 403b39c5158Smillert } 404b39c5158Smillert 405b8851fccSafresh1 $error .= "GetOptionsFromArray: 1st parameter is not an array reference\n" 406b8851fccSafresh1 unless $argv && UNIVERSAL::isa( $argv, 'ARRAY' ); 407b8851fccSafresh1 408b39c5158Smillert # Bail out if errors found. 409b39c5158Smillert die ($error) if $error; 410b39c5158Smillert $error = 0; 411b39c5158Smillert 412b39c5158Smillert # Supply --version and --help support, if needed and allowed. 413b39c5158Smillert if ( defined($auto_version) ? $auto_version : ($requested_version >= 2.3203) ) { 414b39c5158Smillert if ( !defined($opctl{version}) ) { 415b39c5158Smillert $opctl{version} = ['','version',0,CTL_DEST_CODE,undef]; 416b39c5158Smillert $linkage{version} = \&VersionMessage; 417b39c5158Smillert } 418b39c5158Smillert $auto_version = 1; 419b39c5158Smillert } 420b39c5158Smillert if ( defined($auto_help) ? $auto_help : ($requested_version >= 2.3203) ) { 421b39c5158Smillert if ( !defined($opctl{help}) && !defined($opctl{'?'}) ) { 422b39c5158Smillert $opctl{help} = $opctl{'?'} = ['','help',0,CTL_DEST_CODE,undef]; 423b39c5158Smillert $linkage{help} = \&HelpMessage; 424b39c5158Smillert } 425b39c5158Smillert $auto_help = 1; 426b39c5158Smillert } 427b39c5158Smillert 428b39c5158Smillert # Show the options tables if debugging. 429b39c5158Smillert if ( $debug ) { 430b39c5158Smillert my ($arrow, $k, $v); 431b39c5158Smillert $arrow = "=> "; 432b39c5158Smillert while ( ($k,$v) = each(%opctl) ) { 433b39c5158Smillert print STDERR ($arrow, "\$opctl{$k} = $v ", OptCtl($v), "\n"); 434b39c5158Smillert $arrow = " "; 435b39c5158Smillert } 436b39c5158Smillert } 437b39c5158Smillert 438b39c5158Smillert # Process argument list 439b39c5158Smillert my $goon = 1; 440b39c5158Smillert while ( $goon && @$argv > 0 ) { 441b39c5158Smillert 442b39c5158Smillert # Get next argument. 443b39c5158Smillert $opt = shift (@$argv); 444b39c5158Smillert print STDERR ("=> arg \"", $opt, "\"\n") if $debug; 445b39c5158Smillert 446b39c5158Smillert # Double dash is option list terminator. 447e9ce3842Safresh1 if ( defined($opt) && $opt eq $argend ) { 448b39c5158Smillert push (@ret, $argend) if $passthrough; 449b39c5158Smillert last; 450b39c5158Smillert } 451b39c5158Smillert 452b39c5158Smillert # Look it up. 453b39c5158Smillert my $tryopt = $opt; 454b39c5158Smillert my $found; # success status 455b39c5158Smillert my $key; # key (if hash type) 456b39c5158Smillert my $arg; # option argument 457b39c5158Smillert my $ctl; # the opctl entry 458e0680481Safresh1 my $starter; # the actual starter character(s) 459b39c5158Smillert 460e0680481Safresh1 ($found, $opt, $ctl, $starter, $arg, $key) = 461b39c5158Smillert FindOption ($argv, $prefix, $argend, $opt, \%opctl); 462b39c5158Smillert 463b39c5158Smillert if ( $found ) { 464b39c5158Smillert 465b39c5158Smillert # FindOption undefines $opt in case of errors. 466b39c5158Smillert next unless defined $opt; 467b39c5158Smillert 468b39c5158Smillert my $argcnt = 0; 469b39c5158Smillert while ( defined $arg ) { 470b39c5158Smillert 471b39c5158Smillert # Get the canonical name. 472eac174f2Safresh1 my $given = $opt; 473b39c5158Smillert print STDERR ("=> cname for \"$opt\" is ") if $debug; 474b39c5158Smillert $opt = $ctl->[CTL_CNAME]; 475b39c5158Smillert print STDERR ("\"$ctl->[CTL_CNAME]\"\n") if $debug; 476b39c5158Smillert 477b39c5158Smillert if ( defined $linkage{$opt} ) { 478b39c5158Smillert print STDERR ("=> ref(\$L{$opt}) -> ", 479b39c5158Smillert ref($linkage{$opt}), "\n") if $debug; 480b39c5158Smillert 481b39c5158Smillert if ( ref($linkage{$opt}) eq 'SCALAR' 482b39c5158Smillert || ref($linkage{$opt}) eq 'REF' ) { 483b39c5158Smillert if ( $ctl->[CTL_TYPE] eq '+' ) { 484b39c5158Smillert print STDERR ("=> \$\$L{$opt} += \"$arg\"\n") 485b39c5158Smillert if $debug; 486b39c5158Smillert if ( defined ${$linkage{$opt}} ) { 487b39c5158Smillert ${$linkage{$opt}} += $arg; 488b39c5158Smillert } 489b39c5158Smillert else { 490b39c5158Smillert ${$linkage{$opt}} = $arg; 491b39c5158Smillert } 492b39c5158Smillert } 493b39c5158Smillert elsif ( $ctl->[CTL_DEST] == CTL_DEST_ARRAY ) { 494b39c5158Smillert print STDERR ("=> ref(\$L{$opt}) auto-vivified", 495b39c5158Smillert " to ARRAY\n") 496b39c5158Smillert if $debug; 497b39c5158Smillert my $t = $linkage{$opt}; 498b39c5158Smillert $$t = $linkage{$opt} = []; 499b39c5158Smillert print STDERR ("=> push(\@{\$L{$opt}, \"$arg\")\n") 500b39c5158Smillert if $debug; 501b39c5158Smillert push (@{$linkage{$opt}}, $arg); 502b39c5158Smillert } 503b39c5158Smillert elsif ( $ctl->[CTL_DEST] == CTL_DEST_HASH ) { 504b39c5158Smillert print STDERR ("=> ref(\$L{$opt}) auto-vivified", 505b39c5158Smillert " to HASH\n") 506b39c5158Smillert if $debug; 507b39c5158Smillert my $t = $linkage{$opt}; 508b39c5158Smillert $$t = $linkage{$opt} = {}; 509b39c5158Smillert print STDERR ("=> \$\$L{$opt}->{$key} = \"$arg\"\n") 510b39c5158Smillert if $debug; 511b39c5158Smillert $linkage{$opt}->{$key} = $arg; 512b39c5158Smillert } 513b39c5158Smillert else { 514b39c5158Smillert print STDERR ("=> \$\$L{$opt} = \"$arg\"\n") 515b39c5158Smillert if $debug; 516b39c5158Smillert ${$linkage{$opt}} = $arg; 517b39c5158Smillert } 518b39c5158Smillert } 519b39c5158Smillert elsif ( ref($linkage{$opt}) eq 'ARRAY' ) { 520b39c5158Smillert print STDERR ("=> push(\@{\$L{$opt}, \"$arg\")\n") 521b39c5158Smillert if $debug; 522b39c5158Smillert push (@{$linkage{$opt}}, $arg); 523b39c5158Smillert } 524b39c5158Smillert elsif ( ref($linkage{$opt}) eq 'HASH' ) { 525b39c5158Smillert print STDERR ("=> \$\$L{$opt}->{$key} = \"$arg\"\n") 526b39c5158Smillert if $debug; 527b39c5158Smillert $linkage{$opt}->{$key} = $arg; 528b39c5158Smillert } 529b39c5158Smillert elsif ( ref($linkage{$opt}) eq 'CODE' ) { 530b39c5158Smillert print STDERR ("=> &L{$opt}(\"$opt\"", 531b39c5158Smillert $ctl->[CTL_DEST] == CTL_DEST_HASH ? ", \"$key\"" : "", 532b39c5158Smillert ", \"$arg\")\n") 533b39c5158Smillert if $debug; 534b39c5158Smillert my $eval_error = do { 535b39c5158Smillert local $@; 536b39c5158Smillert local $SIG{__DIE__} = 'DEFAULT'; 537b39c5158Smillert eval { 538b39c5158Smillert &{$linkage{$opt}} 539b39c5158Smillert (Getopt::Long::CallBack->new 540b39c5158Smillert (name => $opt, 541eac174f2Safresh1 given => $given, 542b39c5158Smillert ctl => $ctl, 543b39c5158Smillert opctl => \%opctl, 544b39c5158Smillert linkage => \%linkage, 545b39c5158Smillert prefix => $prefix, 546e0680481Safresh1 starter => $starter, 547b39c5158Smillert ), 548b39c5158Smillert $ctl->[CTL_DEST] == CTL_DEST_HASH ? ($key) : (), 549b39c5158Smillert $arg); 550b39c5158Smillert }; 551b39c5158Smillert $@; 552b39c5158Smillert }; 553b39c5158Smillert print STDERR ("=> die($eval_error)\n") 554b39c5158Smillert if $debug && $eval_error ne ''; 555b39c5158Smillert if ( $eval_error =~ /^!/ ) { 556b39c5158Smillert if ( $eval_error =~ /^!FINISH\b/ ) { 557b39c5158Smillert $goon = 0; 558b39c5158Smillert } 559b39c5158Smillert } 560b39c5158Smillert elsif ( $eval_error ne '' ) { 561b39c5158Smillert warn ($eval_error); 562b39c5158Smillert $error++; 563b39c5158Smillert } 564b39c5158Smillert } 565b39c5158Smillert else { 566b39c5158Smillert print STDERR ("Invalid REF type \"", ref($linkage{$opt}), 567b39c5158Smillert "\" in linkage\n"); 568b39c5158Smillert die("Getopt::Long -- internal error!\n"); 569b39c5158Smillert } 570b39c5158Smillert } 571b39c5158Smillert # No entry in linkage means entry in userlinkage. 572b39c5158Smillert elsif ( $ctl->[CTL_DEST] == CTL_DEST_ARRAY ) { 573b39c5158Smillert if ( defined $userlinkage->{$opt} ) { 574b39c5158Smillert print STDERR ("=> push(\@{\$L{$opt}}, \"$arg\")\n") 575b39c5158Smillert if $debug; 576b39c5158Smillert push (@{$userlinkage->{$opt}}, $arg); 577b39c5158Smillert } 578b39c5158Smillert else { 579b39c5158Smillert print STDERR ("=>\$L{$opt} = [\"$arg\"]\n") 580b39c5158Smillert if $debug; 581b39c5158Smillert $userlinkage->{$opt} = [$arg]; 582b39c5158Smillert } 583b39c5158Smillert } 584b39c5158Smillert elsif ( $ctl->[CTL_DEST] == CTL_DEST_HASH ) { 585b39c5158Smillert if ( defined $userlinkage->{$opt} ) { 586b39c5158Smillert print STDERR ("=> \$L{$opt}->{$key} = \"$arg\"\n") 587b39c5158Smillert if $debug; 588b39c5158Smillert $userlinkage->{$opt}->{$key} = $arg; 589b39c5158Smillert } 590b39c5158Smillert else { 591b39c5158Smillert print STDERR ("=>\$L{$opt} = {$key => \"$arg\"}\n") 592b39c5158Smillert if $debug; 593b39c5158Smillert $userlinkage->{$opt} = {$key => $arg}; 594b39c5158Smillert } 595b39c5158Smillert } 596b39c5158Smillert else { 597b39c5158Smillert if ( $ctl->[CTL_TYPE] eq '+' ) { 598b39c5158Smillert print STDERR ("=> \$L{$opt} += \"$arg\"\n") 599b39c5158Smillert if $debug; 600b39c5158Smillert if ( defined $userlinkage->{$opt} ) { 601b39c5158Smillert $userlinkage->{$opt} += $arg; 602b39c5158Smillert } 603b39c5158Smillert else { 604b39c5158Smillert $userlinkage->{$opt} = $arg; 605b39c5158Smillert } 606b39c5158Smillert } 607b39c5158Smillert else { 608b39c5158Smillert print STDERR ("=>\$L{$opt} = \"$arg\"\n") if $debug; 609b39c5158Smillert $userlinkage->{$opt} = $arg; 610b39c5158Smillert } 611b39c5158Smillert } 612b39c5158Smillert 613b39c5158Smillert $argcnt++; 614b39c5158Smillert last if $argcnt >= $ctl->[CTL_AMAX] && $ctl->[CTL_AMAX] != -1; 615b39c5158Smillert undef($arg); 616b39c5158Smillert 617b39c5158Smillert # Need more args? 618b39c5158Smillert if ( $argcnt < $ctl->[CTL_AMIN] ) { 619b39c5158Smillert if ( @$argv ) { 620b39c5158Smillert if ( ValidValue($ctl, $argv->[0], 1, $argend, $prefix) ) { 621b39c5158Smillert $arg = shift(@$argv); 622e9ce3842Safresh1 if ( $ctl->[CTL_TYPE] =~ /^[iIo]$/ ) { 623e9ce3842Safresh1 $arg =~ tr/_//d; 624e9ce3842Safresh1 $arg = $ctl->[CTL_TYPE] eq 'o' && $arg =~ /^0/ 625e9ce3842Safresh1 ? oct($arg) 626e9ce3842Safresh1 : 0+$arg 627e9ce3842Safresh1 } 628b39c5158Smillert ($key,$arg) = $arg =~ /^([^=]+)=(.*)/ 629b39c5158Smillert if $ctl->[CTL_DEST] == CTL_DEST_HASH; 630b39c5158Smillert next; 631b39c5158Smillert } 632b39c5158Smillert warn("Value \"$$argv[0]\" invalid for option $opt\n"); 633b39c5158Smillert $error++; 634b39c5158Smillert } 635b39c5158Smillert else { 636b39c5158Smillert warn("Insufficient arguments for option $opt\n"); 637b39c5158Smillert $error++; 638b39c5158Smillert } 639b39c5158Smillert } 640b39c5158Smillert 641b39c5158Smillert # Any more args? 642b39c5158Smillert if ( @$argv && ValidValue($ctl, $argv->[0], 0, $argend, $prefix) ) { 643b39c5158Smillert $arg = shift(@$argv); 644e9ce3842Safresh1 if ( $ctl->[CTL_TYPE] =~ /^[iIo]$/ ) { 645e9ce3842Safresh1 $arg =~ tr/_//d; 646e9ce3842Safresh1 $arg = $ctl->[CTL_TYPE] eq 'o' && $arg =~ /^0/ 647e9ce3842Safresh1 ? oct($arg) 648e9ce3842Safresh1 : 0+$arg 649e9ce3842Safresh1 } 650b39c5158Smillert ($key,$arg) = $arg =~ /^([^=]+)=(.*)/ 651b39c5158Smillert if $ctl->[CTL_DEST] == CTL_DEST_HASH; 652b39c5158Smillert next; 653b39c5158Smillert } 654b39c5158Smillert } 655b39c5158Smillert } 656b39c5158Smillert 657b39c5158Smillert # Not an option. Save it if we $PERMUTE and don't have a <>. 658b39c5158Smillert elsif ( $order == $PERMUTE ) { 659b39c5158Smillert # Try non-options call-back. 660b39c5158Smillert my $cb; 661b8851fccSafresh1 if ( defined ($cb = $linkage{'<>'}) ) { 662b39c5158Smillert print STDERR ("=> &L{$tryopt}(\"$tryopt\")\n") 663b39c5158Smillert if $debug; 664b39c5158Smillert my $eval_error = do { 665b39c5158Smillert local $@; 666b39c5158Smillert local $SIG{__DIE__} = 'DEFAULT'; 667b39c5158Smillert eval { 668e9ce3842Safresh1 # The arg to <> cannot be the CallBack object 669e9ce3842Safresh1 # since it may be passed to other modules that 670e9ce3842Safresh1 # get confused (e.g., Archive::Tar). Well, 671e9ce3842Safresh1 # it's not relevant for this callback anyway. 672e9ce3842Safresh1 &$cb($tryopt); 673b39c5158Smillert }; 674b39c5158Smillert $@; 675b39c5158Smillert }; 676b39c5158Smillert print STDERR ("=> die($eval_error)\n") 677b39c5158Smillert if $debug && $eval_error ne ''; 678b39c5158Smillert if ( $eval_error =~ /^!/ ) { 679b39c5158Smillert if ( $eval_error =~ /^!FINISH\b/ ) { 680b39c5158Smillert $goon = 0; 681b39c5158Smillert } 682b39c5158Smillert } 683b39c5158Smillert elsif ( $eval_error ne '' ) { 684b39c5158Smillert warn ($eval_error); 685b39c5158Smillert $error++; 686b39c5158Smillert } 687b39c5158Smillert } 688b39c5158Smillert else { 689b39c5158Smillert print STDERR ("=> saving \"$tryopt\" ", 690b39c5158Smillert "(not an option, may permute)\n") if $debug; 691b39c5158Smillert push (@ret, $tryopt); 692b39c5158Smillert } 693b39c5158Smillert next; 694b39c5158Smillert } 695b39c5158Smillert 696b39c5158Smillert # ...otherwise, terminate. 697b39c5158Smillert else { 698b39c5158Smillert # Push this one back and exit. 699b39c5158Smillert unshift (@$argv, $tryopt); 700b39c5158Smillert return ($error == 0); 701b39c5158Smillert } 702b39c5158Smillert 703b39c5158Smillert } 704b39c5158Smillert 705b39c5158Smillert # Finish. 70656d68f1eSafresh1 if ( @ret && ( $order == $PERMUTE || $passthrough ) ) { 707b39c5158Smillert # Push back accumulated arguments 708b39c5158Smillert print STDERR ("=> restoring \"", join('" "', @ret), "\"\n") 709b39c5158Smillert if $debug; 710b39c5158Smillert unshift (@$argv, @ret); 711b39c5158Smillert } 712b39c5158Smillert 713b39c5158Smillert return ($error == 0); 714b39c5158Smillert} 715b39c5158Smillert 716b39c5158Smillert# A readable representation of what's in an optbl. 717b39c5158Smillertsub OptCtl ($) { 718b39c5158Smillert my ($v) = @_; 719b39c5158Smillert my @v = map { defined($_) ? ($_) : ("<undef>") } @$v; 720b39c5158Smillert "[". 721b39c5158Smillert join(",", 722b39c5158Smillert "\"$v[CTL_TYPE]\"", 723b39c5158Smillert "\"$v[CTL_CNAME]\"", 724b39c5158Smillert "\"$v[CTL_DEFAULT]\"", 725b39c5158Smillert ("\$","\@","\%","\&")[$v[CTL_DEST] || 0], 726b39c5158Smillert $v[CTL_AMIN] || '', 727b39c5158Smillert $v[CTL_AMAX] || '', 728b39c5158Smillert# $v[CTL_RANGE] || '', 729b39c5158Smillert# $v[CTL_REPEAT] || '', 730b39c5158Smillert ). "]"; 731b39c5158Smillert} 732b39c5158Smillert 733b39c5158Smillert# Parse an option specification and fill the tables. 734b39c5158Smillertsub ParseOptionSpec ($$) { 735b39c5158Smillert my ($opt, $opctl) = @_; 736b39c5158Smillert 737*3d61058aSafresh1 # Allow period in option name unless passing through, 738*3d61058aSafresh1 my $op = $passthrough 739*3d61058aSafresh1 ? qr/(?: \w+[-\w]* )/x : qr/(?: \w+[-.\w]* )/x; 740*3d61058aSafresh1 741b39c5158Smillert # Match option spec. 742b39c5158Smillert if ( $opt !~ m;^ 743b39c5158Smillert ( 744b39c5158Smillert # Option name 745*3d61058aSafresh1 $op 746e9ce3842Safresh1 # Aliases 74756d68f1eSafresh1 (?: \| (?: . [^|!+=:]* )? )* 748b39c5158Smillert )? 749b39c5158Smillert ( 750b39c5158Smillert # Either modifiers ... 751b39c5158Smillert [!+] 752b39c5158Smillert | 753b39c5158Smillert # ... or a value/dest/repeat specification 754b39c5158Smillert [=:] [ionfs] [@%]? (?: \{\d*,?\d*\} )? 755b39c5158Smillert | 756b39c5158Smillert # ... or an optional-with-default spec 757e0680481Safresh1 : (?: 0[0-7]+ | 0[xX][0-9a-fA-F]+ | 0[bB][01]+ | -?\d+ | \+ ) [@%]? 758b39c5158Smillert )? 759b39c5158Smillert $;x ) { 760b39c5158Smillert return (undef, "Error in option spec: \"$opt\"\n"); 761b39c5158Smillert } 762b39c5158Smillert 763b39c5158Smillert my ($names, $spec) = ($1, $2); 764b39c5158Smillert $spec = '' unless defined $spec; 765b39c5158Smillert 766b39c5158Smillert # $orig keeps track of the primary name the user specified. 767b39c5158Smillert # This name will be used for the internal or external linkage. 768b39c5158Smillert # In other words, if the user specifies "FoO|BaR", it will 769b39c5158Smillert # match any case combinations of 'foo' and 'bar', but if a global 770b39c5158Smillert # variable needs to be set, it will be $opt_FoO in the exact case 771b39c5158Smillert # as specified. 772b39c5158Smillert my $orig; 773b39c5158Smillert 774b39c5158Smillert my @names; 775b39c5158Smillert if ( defined $names ) { 776b39c5158Smillert @names = split (/\|/, $names); 777b39c5158Smillert $orig = $names[0]; 778b39c5158Smillert } 779b39c5158Smillert else { 780b39c5158Smillert @names = (''); 781b39c5158Smillert $orig = ''; 782b39c5158Smillert } 783b39c5158Smillert 784b39c5158Smillert # Construct the opctl entries. 785b39c5158Smillert my $entry; 786b39c5158Smillert if ( $spec eq '' || $spec eq '+' || $spec eq '!' ) { 787b39c5158Smillert # Fields are hard-wired here. 788b39c5158Smillert $entry = [$spec,$orig,undef,CTL_DEST_SCALAR,0,0]; 789b39c5158Smillert } 790e0680481Safresh1 elsif ( $spec =~ /^:(0[0-7]+|0x[0-9a-f]+|0b[01]+|-?\d+|\+)([@%])?$/i ) { 791b39c5158Smillert my $def = $1; 792b39c5158Smillert my $dest = $2; 793e0680481Safresh1 my $type = 'i'; # assume integer 794e0680481Safresh1 if ( $def eq '+' ) { 795e0680481Safresh1 # Increment. 796e0680481Safresh1 $type = 'I'; 797e0680481Safresh1 } 798e0680481Safresh1 elsif ( $def =~ /^(0[0-7]+|0[xX][0-9a-fA-F]+|0[bB][01]+)$/ ) { 799e0680481Safresh1 # Octal, binary or hex. 800e0680481Safresh1 $type = 'o'; 801e0680481Safresh1 $def = oct($def); 802e0680481Safresh1 } 803e0680481Safresh1 elsif ( $def =~ /^-?\d+$/ ) { 804e0680481Safresh1 # Integer. 805e0680481Safresh1 $def = 0 + $def; 806e0680481Safresh1 } 807b39c5158Smillert $dest ||= '$'; 808b39c5158Smillert $dest = $dest eq '@' ? CTL_DEST_ARRAY 809b39c5158Smillert : $dest eq '%' ? CTL_DEST_HASH : CTL_DEST_SCALAR; 810b39c5158Smillert # Fields are hard-wired here. 811b39c5158Smillert $entry = [$type,$orig,$def eq '+' ? undef : $def, 812b39c5158Smillert $dest,0,1]; 813b39c5158Smillert } 814b39c5158Smillert else { 815b39c5158Smillert my ($mand, $type, $dest) = 816b39c5158Smillert $spec =~ /^([=:])([ionfs])([@%])?(\{(\d+)?(,)?(\d+)?\})?$/; 817b39c5158Smillert return (undef, "Cannot repeat while bundling: \"$opt\"\n") 818b39c5158Smillert if $bundling && defined($4); 819b39c5158Smillert my ($mi, $cm, $ma) = ($5, $6, $7); 820b39c5158Smillert return (undef, "{0} is useless in option spec: \"$opt\"\n") 821b39c5158Smillert if defined($mi) && !$mi && !defined($ma) && !defined($cm); 822b39c5158Smillert 823b39c5158Smillert $type = 'i' if $type eq 'n'; 824b39c5158Smillert $dest ||= '$'; 825b39c5158Smillert $dest = $dest eq '@' ? CTL_DEST_ARRAY 826b39c5158Smillert : $dest eq '%' ? CTL_DEST_HASH : CTL_DEST_SCALAR; 827b39c5158Smillert # Default minargs to 1/0 depending on mand status. 828b39c5158Smillert $mi = $mand eq '=' ? 1 : 0 unless defined $mi; 829b39c5158Smillert # Adjust mand status according to minargs. 830b39c5158Smillert $mand = $mi ? '=' : ':'; 831b39c5158Smillert # Adjust maxargs. 832b39c5158Smillert $ma = $mi ? $mi : 1 unless defined $ma || defined $cm; 833b39c5158Smillert return (undef, "Max must be greater than zero in option spec: \"$opt\"\n") 834b39c5158Smillert if defined($ma) && !$ma; 835b39c5158Smillert return (undef, "Max less than min in option spec: \"$opt\"\n") 836b39c5158Smillert if defined($ma) && $ma < $mi; 837b39c5158Smillert 838b39c5158Smillert # Fields are hard-wired here. 839b39c5158Smillert $entry = [$type,$orig,undef,$dest,$mi,$ma||-1]; 840b39c5158Smillert } 841b39c5158Smillert 842b39c5158Smillert # Process all names. First is canonical, the rest are aliases. 843b39c5158Smillert my $dups = ''; 844b39c5158Smillert foreach ( @names ) { 845b39c5158Smillert 846b39c5158Smillert $_ = lc ($_) 847b39c5158Smillert if $ignorecase > (($bundling && length($_) == 1) ? 1 : 0); 848b39c5158Smillert 849b39c5158Smillert if ( exists $opctl->{$_} ) { 850b39c5158Smillert $dups .= "Duplicate specification \"$opt\" for option \"$_\"\n"; 851b39c5158Smillert } 852b39c5158Smillert 853b39c5158Smillert if ( $spec eq '!' ) { 854b39c5158Smillert $opctl->{"no$_"} = $entry; 855b39c5158Smillert $opctl->{"no-$_"} = $entry; 856b39c5158Smillert $opctl->{$_} = [@$entry]; 857b39c5158Smillert $opctl->{$_}->[CTL_TYPE] = ''; 858b39c5158Smillert } 859b39c5158Smillert else { 860b39c5158Smillert $opctl->{$_} = $entry; 861b39c5158Smillert } 862b39c5158Smillert } 863b39c5158Smillert 864*3d61058aSafresh1 if ( $dups ) { 865*3d61058aSafresh1 # Warn now. Will become fatal in a future release. 866b39c5158Smillert foreach ( split(/\n+/, $dups) ) { 867b39c5158Smillert warn($_."\n"); 868b39c5158Smillert } 869b39c5158Smillert } 870b39c5158Smillert ($names[0], $orig); 871b39c5158Smillert} 872b39c5158Smillert 873b39c5158Smillert# Option lookup. 874b39c5158Smillertsub FindOption ($$$$$) { 875b39c5158Smillert 876e0680481Safresh1 # returns (1, $opt, $ctl, $starter, $arg, $key) if okay, 877b39c5158Smillert # returns (1, undef) if option in error, 878b39c5158Smillert # returns (0) otherwise. 879b39c5158Smillert 880b39c5158Smillert my ($argv, $prefix, $argend, $opt, $opctl) = @_; 881b39c5158Smillert 882b39c5158Smillert print STDERR ("=> find \"$opt\"\n") if $debug; 883b39c5158Smillert 884e9ce3842Safresh1 return (0) unless defined($opt); 885e9ce3842Safresh1 return (0) unless $opt =~ /^($prefix)(.*)$/s; 886b39c5158Smillert return (0) if $opt eq "-" && !defined $opctl->{''}; 887b39c5158Smillert 888e9ce3842Safresh1 $opt = substr( $opt, length($1) ); # retain taintedness 889b39c5158Smillert my $starter = $1; 890b39c5158Smillert 891b39c5158Smillert print STDERR ("=> split \"$starter\"+\"$opt\"\n") if $debug; 892b39c5158Smillert 893b39c5158Smillert my $optarg; # value supplied with --opt=value 894b39c5158Smillert my $rest; # remainder from unbundling 895b39c5158Smillert 896b39c5158Smillert # If it is a long option, it may include the value. 897b39c5158Smillert # With getopt_compat, only if not bundling. 898b39c5158Smillert if ( ($starter=~/^$longprefix$/ 899b39c5158Smillert || ($getopt_compat && ($bundling == 0 || $bundling == 2))) 900e9ce3842Safresh1 && (my $oppos = index($opt, '=', 1)) > 0) { 901e9ce3842Safresh1 my $optorg = $opt; 902e9ce3842Safresh1 $opt = substr($optorg, 0, $oppos); 903e9ce3842Safresh1 $optarg = substr($optorg, $oppos + 1); # retain tainedness 904b39c5158Smillert print STDERR ("=> option \"", $opt, 905b39c5158Smillert "\", optarg = \"$optarg\"\n") if $debug; 906b39c5158Smillert } 907b39c5158Smillert 908b39c5158Smillert #### Look it up ### 909b39c5158Smillert 910b39c5158Smillert my $tryopt = $opt; # option to try 911b39c5158Smillert 912b8851fccSafresh1 if ( ( $bundling || $bundling_values ) && $starter eq '-' ) { 913b39c5158Smillert 914b39c5158Smillert # To try overrides, obey case ignore. 915b39c5158Smillert $tryopt = $ignorecase ? lc($opt) : $opt; 916b39c5158Smillert 917b39c5158Smillert # If bundling == 2, long options can override bundles. 918b39c5158Smillert if ( $bundling == 2 && length($tryopt) > 1 919b39c5158Smillert && defined ($opctl->{$tryopt}) ) { 920b39c5158Smillert print STDERR ("=> $starter$tryopt overrides unbundling\n") 921b39c5158Smillert if $debug; 922b39c5158Smillert } 923b8851fccSafresh1 924b8851fccSafresh1 # If bundling_values, option may be followed by the value. 925b8851fccSafresh1 elsif ( $bundling_values ) { 926b8851fccSafresh1 $tryopt = $opt; 927b8851fccSafresh1 # Unbundle single letter option. 928b8851fccSafresh1 $rest = length ($tryopt) > 0 ? substr ($tryopt, 1) : ''; 929b8851fccSafresh1 $tryopt = substr ($tryopt, 0, 1); 930b8851fccSafresh1 $tryopt = lc ($tryopt) if $ignorecase > 1; 931b8851fccSafresh1 print STDERR ("=> $starter$tryopt unbundled from ", 932b8851fccSafresh1 "$starter$tryopt$rest\n") if $debug; 933b8851fccSafresh1 # Whatever remains may not be considered an option. 934b8851fccSafresh1 $optarg = $rest eq '' ? undef : $rest; 935b8851fccSafresh1 $rest = undef; 936b8851fccSafresh1 } 937b8851fccSafresh1 938b8851fccSafresh1 # Split off a single letter and leave the rest for 939b8851fccSafresh1 # further processing. 940b39c5158Smillert else { 941b39c5158Smillert $tryopt = $opt; 942b39c5158Smillert # Unbundle single letter option. 943b39c5158Smillert $rest = length ($tryopt) > 0 ? substr ($tryopt, 1) : ''; 944b39c5158Smillert $tryopt = substr ($tryopt, 0, 1); 945b39c5158Smillert $tryopt = lc ($tryopt) if $ignorecase > 1; 946b39c5158Smillert print STDERR ("=> $starter$tryopt unbundled from ", 947b39c5158Smillert "$starter$tryopt$rest\n") if $debug; 948b39c5158Smillert $rest = undef unless $rest ne ''; 949b39c5158Smillert } 950b39c5158Smillert } 951b39c5158Smillert 952b39c5158Smillert # Try auto-abbreviation. 953b39c5158Smillert elsif ( $autoabbrev && $opt ne "" ) { 954b39c5158Smillert # Sort the possible long option names. 955b39c5158Smillert my @names = sort(keys (%$opctl)); 956b39c5158Smillert # Downcase if allowed. 957b39c5158Smillert $opt = lc ($opt) if $ignorecase; 958b39c5158Smillert $tryopt = $opt; 959b39c5158Smillert # Turn option name into pattern. 960b39c5158Smillert my $pat = quotemeta ($opt); 961b39c5158Smillert # Look up in option names. 962b39c5158Smillert my @hits = grep (/^$pat/, @names); 963b39c5158Smillert print STDERR ("=> ", scalar(@hits), " hits (@hits) with \"$pat\" ", 964b39c5158Smillert "out of ", scalar(@names), "\n") if $debug; 965b39c5158Smillert 966b39c5158Smillert # Check for ambiguous results. 967b39c5158Smillert unless ( (@hits <= 1) || (grep ($_ eq $opt, @hits) == 1) ) { 968b39c5158Smillert # See if all matches are for the same option. 969b39c5158Smillert my %hit; 970b39c5158Smillert foreach ( @hits ) { 971e5157e49Safresh1 my $hit = $opctl->{$_}->[CTL_CNAME] 972e5157e49Safresh1 if defined $opctl->{$_}->[CTL_CNAME]; 973e5157e49Safresh1 $hit = "no" . $hit if $opctl->{$_}->[CTL_TYPE] eq '!'; 974b39c5158Smillert $hit{$hit} = 1; 975b39c5158Smillert } 976b39c5158Smillert # Remove auto-supplied options (version, help). 977b39c5158Smillert if ( keys(%hit) == 2 ) { 978b39c5158Smillert if ( $auto_version && exists($hit{version}) ) { 979b39c5158Smillert delete $hit{version}; 980b39c5158Smillert } 981b39c5158Smillert elsif ( $auto_help && exists($hit{help}) ) { 982b39c5158Smillert delete $hit{help}; 983b39c5158Smillert } 984b39c5158Smillert } 985b39c5158Smillert # Now see if it really is ambiguous. 986b39c5158Smillert unless ( keys(%hit) == 1 ) { 987b39c5158Smillert return (0) if $passthrough; 988b39c5158Smillert warn ("Option ", $opt, " is ambiguous (", 989b39c5158Smillert join(", ", @hits), ")\n"); 990b39c5158Smillert $error++; 991b39c5158Smillert return (1, undef); 992b39c5158Smillert } 993b39c5158Smillert @hits = keys(%hit); 994b39c5158Smillert } 995b39c5158Smillert 996b39c5158Smillert # Complete the option name, if appropriate. 997b39c5158Smillert if ( @hits == 1 && $hits[0] ne $opt ) { 998b39c5158Smillert $tryopt = $hits[0]; 9999f11ffb7Safresh1 $tryopt = lc ($tryopt) 10009f11ffb7Safresh1 if $ignorecase > (($bundling && length($tryopt) == 1) ? 1 : 0); 1001b39c5158Smillert print STDERR ("=> option \"$opt\" -> \"$tryopt\"\n") 1002b39c5158Smillert if $debug; 1003b39c5158Smillert } 1004b39c5158Smillert } 1005b39c5158Smillert 1006b39c5158Smillert # Map to all lowercase if ignoring case. 1007b39c5158Smillert elsif ( $ignorecase ) { 1008b39c5158Smillert $tryopt = lc ($opt); 1009b39c5158Smillert } 1010b39c5158Smillert 1011b39c5158Smillert # Check validity by fetching the info. 1012b39c5158Smillert my $ctl = $opctl->{$tryopt}; 1013b39c5158Smillert unless ( defined $ctl ) { 1014b39c5158Smillert return (0) if $passthrough; 1015b39c5158Smillert # Pretend one char when bundling. 1016b39c5158Smillert if ( $bundling == 1 && length($starter) == 1 ) { 1017b39c5158Smillert $opt = substr($opt,0,1); 1018b39c5158Smillert unshift (@$argv, $starter.$rest) if defined $rest; 1019b39c5158Smillert } 1020b39c5158Smillert if ( $opt eq "" ) { 1021b39c5158Smillert warn ("Missing option after ", $starter, "\n"); 1022b39c5158Smillert } 1023b39c5158Smillert else { 1024b39c5158Smillert warn ("Unknown option: ", $opt, "\n"); 1025b39c5158Smillert } 1026b39c5158Smillert $error++; 1027b39c5158Smillert return (1, undef); 1028b39c5158Smillert } 1029b39c5158Smillert # Apparently valid. 1030b39c5158Smillert $opt = $tryopt; 1031b39c5158Smillert print STDERR ("=> found ", OptCtl($ctl), 1032b39c5158Smillert " for \"", $opt, "\"\n") if $debug; 1033b39c5158Smillert 1034b39c5158Smillert #### Determine argument status #### 1035b39c5158Smillert 1036b39c5158Smillert # If it is an option w/o argument, we're almost finished with it. 1037b39c5158Smillert my $type = $ctl->[CTL_TYPE]; 1038b39c5158Smillert my $arg; 1039b39c5158Smillert 1040b39c5158Smillert if ( $type eq '' || $type eq '!' || $type eq '+' ) { 1041b39c5158Smillert if ( defined $optarg ) { 1042b39c5158Smillert return (0) if $passthrough; 1043b39c5158Smillert warn ("Option ", $opt, " does not take an argument\n"); 1044b39c5158Smillert $error++; 1045b39c5158Smillert undef $opt; 1046b8851fccSafresh1 undef $optarg if $bundling_values; 1047b39c5158Smillert } 1048b39c5158Smillert elsif ( $type eq '' || $type eq '+' ) { 1049b39c5158Smillert # Supply explicit value. 1050b39c5158Smillert $arg = 1; 1051b39c5158Smillert } 1052b39c5158Smillert else { 1053b39c5158Smillert $opt =~ s/^no-?//i; # strip NO prefix 1054b39c5158Smillert $arg = 0; # supply explicit value 1055b39c5158Smillert } 1056b39c5158Smillert unshift (@$argv, $starter.$rest) if defined $rest; 1057e0680481Safresh1 return (1, $opt, $ctl, $starter, $arg); 1058b39c5158Smillert } 1059b39c5158Smillert 1060b39c5158Smillert # Get mandatory status and type info. 1061b39c5158Smillert my $mand = $ctl->[CTL_AMIN]; 1062b39c5158Smillert 1063b39c5158Smillert # Check if there is an option argument available. 1064b8851fccSafresh1 if ( $gnu_compat ) { 10659f11ffb7Safresh1 my $optargtype = 0; # none, 1 = empty, 2 = nonempty, 3 = aux 10669f11ffb7Safresh1 if ( defined($optarg) ) { 10679f11ffb7Safresh1 $optargtype = (length($optarg) == 0) ? 1 : 2; 10689f11ffb7Safresh1 } 10699f11ffb7Safresh1 elsif ( defined $rest || @$argv > 0 ) { 10709f11ffb7Safresh1 # GNU getopt_long() does not accept the (optional) 10719f11ffb7Safresh1 # argument to be passed to the option without = sign. 10729f11ffb7Safresh1 # We do, since not doing so breaks existing scripts. 10739f11ffb7Safresh1 $optargtype = 3; 10749f11ffb7Safresh1 } 10759f11ffb7Safresh1 if(($optargtype == 0) && !$mand) { 107656d68f1eSafresh1 if ( $type eq 'I' ) { 107756d68f1eSafresh1 # Fake incremental type. 107856d68f1eSafresh1 my @c = @$ctl; 107956d68f1eSafresh1 $c[CTL_TYPE] = '+'; 1080e0680481Safresh1 return (1, $opt, \@c, $starter, 1); 108156d68f1eSafresh1 } 10829f11ffb7Safresh1 my $val 10839f11ffb7Safresh1 = defined($ctl->[CTL_DEFAULT]) ? $ctl->[CTL_DEFAULT] 10849f11ffb7Safresh1 : $type eq 's' ? '' 10859f11ffb7Safresh1 : 0; 1086e0680481Safresh1 return (1, $opt, $ctl, $starter, $val); 10879f11ffb7Safresh1 } 1088e0680481Safresh1 return (1, $opt, $ctl, $starter, $type eq 's' ? '' : 0) 1089b8851fccSafresh1 if $optargtype == 1; # --foo= -> return nothing 1090b39c5158Smillert } 1091b39c5158Smillert 1092b39c5158Smillert # Check if there is an option argument available. 1093b39c5158Smillert if ( defined $optarg 1094b39c5158Smillert ? ($optarg eq '') 1095b39c5158Smillert : !(defined $rest || @$argv > 0) ) { 1096b39c5158Smillert # Complain if this option needs an argument. 1097b39c5158Smillert# if ( $mand && !($type eq 's' ? defined($optarg) : 0) ) { 1098eac174f2Safresh1 if ( $mand || $ctl->[CTL_DEST] == CTL_DEST_HASH ) { 1099b39c5158Smillert return (0) if $passthrough; 1100b39c5158Smillert warn ("Option ", $opt, " requires an argument\n"); 1101b39c5158Smillert $error++; 1102b39c5158Smillert return (1, undef); 1103b39c5158Smillert } 1104b39c5158Smillert if ( $type eq 'I' ) { 1105b39c5158Smillert # Fake incremental type. 1106b39c5158Smillert my @c = @$ctl; 1107b39c5158Smillert $c[CTL_TYPE] = '+'; 1108e0680481Safresh1 return (1, $opt, \@c, $starter, 1); 1109b39c5158Smillert } 1110e0680481Safresh1 return (1, $opt, $ctl, $starter, 1111b39c5158Smillert defined($ctl->[CTL_DEFAULT]) ? $ctl->[CTL_DEFAULT] : 1112b39c5158Smillert $type eq 's' ? '' : 0); 1113b39c5158Smillert } 1114b39c5158Smillert 1115b39c5158Smillert # Get (possibly optional) argument. 1116b39c5158Smillert $arg = (defined $rest ? $rest 1117b39c5158Smillert : (defined $optarg ? $optarg : shift (@$argv))); 1118b39c5158Smillert 1119b39c5158Smillert # Get key if this is a "name=value" pair for a hash option. 1120b39c5158Smillert my $key; 1121b39c5158Smillert if ($ctl->[CTL_DEST] == CTL_DEST_HASH && defined $arg) { 1122b39c5158Smillert ($key, $arg) = ($arg =~ /^([^=]*)=(.*)$/s) ? ($1, $2) 1123b39c5158Smillert : ($arg, defined($ctl->[CTL_DEFAULT]) ? $ctl->[CTL_DEFAULT] : 1124b39c5158Smillert ($mand ? undef : ($type eq 's' ? "" : 1))); 1125b39c5158Smillert if (! defined $arg) { 1126b39c5158Smillert warn ("Option $opt, key \"$key\", requires a value\n"); 1127b39c5158Smillert $error++; 1128b39c5158Smillert # Push back. 1129b39c5158Smillert unshift (@$argv, $starter.$rest) if defined $rest; 1130b39c5158Smillert return (1, undef); 1131b39c5158Smillert } 1132b39c5158Smillert } 1133b39c5158Smillert 1134b39c5158Smillert #### Check if the argument is valid for this option #### 1135b39c5158Smillert 1136b39c5158Smillert my $key_valid = $ctl->[CTL_DEST] == CTL_DEST_HASH ? "[^=]+=" : ""; 1137b39c5158Smillert 1138b39c5158Smillert if ( $type eq 's' ) { # string 1139b39c5158Smillert # A mandatory string takes anything. 1140e0680481Safresh1 return (1, $opt, $ctl, $starter, $arg, $key) if $mand; 1141b39c5158Smillert 1142b39c5158Smillert # Same for optional string as a hash value 1143e0680481Safresh1 return (1, $opt, $ctl, $starter, $arg, $key) 1144b39c5158Smillert if $ctl->[CTL_DEST] == CTL_DEST_HASH; 1145b39c5158Smillert 1146b39c5158Smillert # An optional string takes almost anything. 1147e0680481Safresh1 return (1, $opt, $ctl, $starter, $arg, $key) 1148b39c5158Smillert if defined $optarg || defined $rest; 1149e0680481Safresh1 return (1, $opt, $ctl, $starter, $arg, $key) if $arg eq "-"; # ?? 1150b39c5158Smillert 1151b39c5158Smillert # Check for option or option list terminator. 1152b39c5158Smillert if ($arg eq $argend || 1153b39c5158Smillert $arg =~ /^$prefix.+/) { 1154b39c5158Smillert # Push back. 1155b39c5158Smillert unshift (@$argv, $arg); 1156b39c5158Smillert # Supply empty value. 1157b39c5158Smillert $arg = ''; 1158b39c5158Smillert } 1159b39c5158Smillert } 1160b39c5158Smillert 1161b39c5158Smillert elsif ( $type eq 'i' # numeric/integer 1162b39c5158Smillert || $type eq 'I' # numeric/integer w/ incr default 1163b39c5158Smillert || $type eq 'o' ) { # dec/oct/hex/bin value 1164b39c5158Smillert 1165b39c5158Smillert my $o_valid = $type eq 'o' ? PAT_XINT : PAT_INT; 1166b39c5158Smillert 1167b39c5158Smillert if ( $bundling && defined $rest 1168b39c5158Smillert && $rest =~ /^($key_valid)($o_valid)(.*)$/si ) { 1169b39c5158Smillert ($key, $arg, $rest) = ($1, $2, $+); 1170b39c5158Smillert chop($key) if $key; 1171b39c5158Smillert $arg = ($type eq 'o' && $arg =~ /^0/) ? oct($arg) : 0+$arg; 1172b39c5158Smillert unshift (@$argv, $starter.$rest) if defined $rest && $rest ne ''; 1173b39c5158Smillert } 1174b39c5158Smillert elsif ( $arg =~ /^$o_valid$/si ) { 1175b39c5158Smillert $arg =~ tr/_//d; 1176b39c5158Smillert $arg = ($type eq 'o' && $arg =~ /^0/) ? oct($arg) : 0+$arg; 1177b39c5158Smillert } 1178b39c5158Smillert else { 1179b39c5158Smillert if ( defined $optarg || $mand ) { 1180b39c5158Smillert if ( $passthrough ) { 1181b39c5158Smillert unshift (@$argv, defined $rest ? $starter.$rest : $arg) 1182b39c5158Smillert unless defined $optarg; 1183b39c5158Smillert return (0); 1184b39c5158Smillert } 1185b39c5158Smillert warn ("Value \"", $arg, "\" invalid for option ", 1186b39c5158Smillert $opt, " (", 1187b39c5158Smillert $type eq 'o' ? "extended " : '', 1188b39c5158Smillert "number expected)\n"); 1189b39c5158Smillert $error++; 1190b39c5158Smillert # Push back. 1191b39c5158Smillert unshift (@$argv, $starter.$rest) if defined $rest; 1192b39c5158Smillert return (1, undef); 1193b39c5158Smillert } 1194b39c5158Smillert else { 1195b39c5158Smillert # Push back. 1196b39c5158Smillert unshift (@$argv, defined $rest ? $starter.$rest : $arg); 1197b39c5158Smillert if ( $type eq 'I' ) { 1198b39c5158Smillert # Fake incremental type. 1199b39c5158Smillert my @c = @$ctl; 1200b39c5158Smillert $c[CTL_TYPE] = '+'; 1201e0680481Safresh1 return (1, $opt, \@c, $starter, 1); 1202b39c5158Smillert } 1203b39c5158Smillert # Supply default value. 1204b39c5158Smillert $arg = defined($ctl->[CTL_DEFAULT]) ? $ctl->[CTL_DEFAULT] : 0; 1205b39c5158Smillert } 1206b39c5158Smillert } 1207b39c5158Smillert } 1208b39c5158Smillert 1209b39c5158Smillert elsif ( $type eq 'f' ) { # real number, int is also ok 1210b39c5158Smillert my $o_valid = PAT_FLOAT; 1211b39c5158Smillert if ( $bundling && defined $rest && 1212b39c5158Smillert $rest =~ /^($key_valid)($o_valid)(.*)$/s ) { 1213b39c5158Smillert $arg =~ tr/_//d; 1214b39c5158Smillert ($key, $arg, $rest) = ($1, $2, $+); 1215b39c5158Smillert chop($key) if $key; 1216b39c5158Smillert unshift (@$argv, $starter.$rest) if defined $rest && $rest ne ''; 1217b39c5158Smillert } 1218b39c5158Smillert elsif ( $arg =~ /^$o_valid$/ ) { 1219b39c5158Smillert $arg =~ tr/_//d; 1220b39c5158Smillert } 1221b39c5158Smillert else { 1222b39c5158Smillert if ( defined $optarg || $mand ) { 1223b39c5158Smillert if ( $passthrough ) { 1224b39c5158Smillert unshift (@$argv, defined $rest ? $starter.$rest : $arg) 1225b39c5158Smillert unless defined $optarg; 1226b39c5158Smillert return (0); 1227b39c5158Smillert } 1228b39c5158Smillert warn ("Value \"", $arg, "\" invalid for option ", 1229b39c5158Smillert $opt, " (real number expected)\n"); 1230b39c5158Smillert $error++; 1231b39c5158Smillert # Push back. 1232b39c5158Smillert unshift (@$argv, $starter.$rest) if defined $rest; 1233b39c5158Smillert return (1, undef); 1234b39c5158Smillert } 1235b39c5158Smillert else { 1236b39c5158Smillert # Push back. 1237b39c5158Smillert unshift (@$argv, defined $rest ? $starter.$rest : $arg); 1238b39c5158Smillert # Supply default value. 1239b39c5158Smillert $arg = 0.0; 1240b39c5158Smillert } 1241b39c5158Smillert } 1242b39c5158Smillert } 1243b39c5158Smillert else { 1244b39c5158Smillert die("Getopt::Long internal error (Can't happen)\n"); 1245b39c5158Smillert } 1246e0680481Safresh1 return (1, $opt, $ctl, $starter, $arg, $key); 1247b39c5158Smillert} 1248b39c5158Smillert 1249b39c5158Smillertsub ValidValue ($$$$$) { 1250b39c5158Smillert my ($ctl, $arg, $mand, $argend, $prefix) = @_; 1251b39c5158Smillert 1252b39c5158Smillert if ( $ctl->[CTL_DEST] == CTL_DEST_HASH ) { 1253b39c5158Smillert return 0 unless $arg =~ /[^=]+=(.*)/; 1254b39c5158Smillert $arg = $1; 1255b39c5158Smillert } 1256b39c5158Smillert 1257b39c5158Smillert my $type = $ctl->[CTL_TYPE]; 1258b39c5158Smillert 1259b39c5158Smillert if ( $type eq 's' ) { # string 1260b39c5158Smillert # A mandatory string takes anything. 1261b39c5158Smillert return (1) if $mand; 1262b39c5158Smillert 1263b39c5158Smillert return (1) if $arg eq "-"; 1264b39c5158Smillert 1265b39c5158Smillert # Check for option or option list terminator. 1266b39c5158Smillert return 0 if $arg eq $argend || $arg =~ /^$prefix.+/; 1267b39c5158Smillert return 1; 1268b39c5158Smillert } 1269b39c5158Smillert 1270b39c5158Smillert elsif ( $type eq 'i' # numeric/integer 1271b39c5158Smillert || $type eq 'I' # numeric/integer w/ incr default 1272b39c5158Smillert || $type eq 'o' ) { # dec/oct/hex/bin value 1273b39c5158Smillert 1274b39c5158Smillert my $o_valid = $type eq 'o' ? PAT_XINT : PAT_INT; 1275b39c5158Smillert return $arg =~ /^$o_valid$/si; 1276b39c5158Smillert } 1277b39c5158Smillert 1278b39c5158Smillert elsif ( $type eq 'f' ) { # real number, int is also ok 1279b39c5158Smillert my $o_valid = PAT_FLOAT; 1280b39c5158Smillert return $arg =~ /^$o_valid$/; 1281b39c5158Smillert } 1282b39c5158Smillert die("ValidValue: Cannot happen\n"); 1283b39c5158Smillert} 1284b39c5158Smillert 1285b39c5158Smillert# Getopt::Long Configuration. 1286b39c5158Smillertsub Configure (@) { 1287b39c5158Smillert my (@options) = @_; 1288b39c5158Smillert 1289b39c5158Smillert my $prevconfig = 1290b8851fccSafresh1 [ $error, $debug, $major_version, $minor_version, $caller, 1291b39c5158Smillert $autoabbrev, $getopt_compat, $ignorecase, $bundling, $order, 1292b39c5158Smillert $gnu_compat, $passthrough, $genprefix, $auto_version, $auto_help, 1293b8851fccSafresh1 $longprefix, $bundling_values ]; 1294b39c5158Smillert 1295b39c5158Smillert if ( ref($options[0]) eq 'ARRAY' ) { 1296b8851fccSafresh1 ( $error, $debug, $major_version, $minor_version, $caller, 1297b39c5158Smillert $autoabbrev, $getopt_compat, $ignorecase, $bundling, $order, 1298b39c5158Smillert $gnu_compat, $passthrough, $genprefix, $auto_version, $auto_help, 1299b8851fccSafresh1 $longprefix, $bundling_values ) = @{shift(@options)}; 1300b39c5158Smillert } 1301b39c5158Smillert 1302b39c5158Smillert my $opt; 1303b39c5158Smillert foreach $opt ( @options ) { 1304b39c5158Smillert my $try = lc ($opt); 1305b39c5158Smillert my $action = 1; 1306b39c5158Smillert if ( $try =~ /^no_?(.*)$/s ) { 1307b39c5158Smillert $action = 0; 1308b39c5158Smillert $try = $+; 1309b39c5158Smillert } 1310b39c5158Smillert if ( ($try eq 'default' or $try eq 'defaults') && $action ) { 1311b39c5158Smillert ConfigDefaults (); 1312b39c5158Smillert } 1313b39c5158Smillert elsif ( ($try eq 'posix_default' or $try eq 'posix_defaults') ) { 1314b39c5158Smillert local $ENV{POSIXLY_CORRECT}; 1315b39c5158Smillert $ENV{POSIXLY_CORRECT} = 1 if $action; 1316b39c5158Smillert ConfigDefaults (); 1317b39c5158Smillert } 1318b39c5158Smillert elsif ( $try eq 'auto_abbrev' or $try eq 'autoabbrev' ) { 1319b39c5158Smillert $autoabbrev = $action; 1320b39c5158Smillert } 1321b39c5158Smillert elsif ( $try eq 'getopt_compat' ) { 1322b39c5158Smillert $getopt_compat = $action; 1323b39c5158Smillert $genprefix = $action ? "(--|-|\\+)" : "(--|-)"; 1324b39c5158Smillert } 1325b39c5158Smillert elsif ( $try eq 'gnu_getopt' ) { 1326b39c5158Smillert if ( $action ) { 1327b39c5158Smillert $gnu_compat = 1; 1328b39c5158Smillert $bundling = 1; 1329b39c5158Smillert $getopt_compat = 0; 1330b39c5158Smillert $genprefix = "(--|-)"; 1331b39c5158Smillert $order = $PERMUTE; 1332b8851fccSafresh1 $bundling_values = 0; 1333b39c5158Smillert } 1334b39c5158Smillert } 1335b39c5158Smillert elsif ( $try eq 'gnu_compat' ) { 1336b39c5158Smillert $gnu_compat = $action; 1337b8851fccSafresh1 $bundling = 0; 1338b8851fccSafresh1 $bundling_values = 1; 1339b39c5158Smillert } 1340b39c5158Smillert elsif ( $try =~ /^(auto_?)?version$/ ) { 1341b39c5158Smillert $auto_version = $action; 1342b39c5158Smillert } 1343b39c5158Smillert elsif ( $try =~ /^(auto_?)?help$/ ) { 1344b39c5158Smillert $auto_help = $action; 1345b39c5158Smillert } 1346b39c5158Smillert elsif ( $try eq 'ignorecase' or $try eq 'ignore_case' ) { 1347b39c5158Smillert $ignorecase = $action; 1348b39c5158Smillert } 1349b39c5158Smillert elsif ( $try eq 'ignorecase_always' or $try eq 'ignore_case_always' ) { 1350b39c5158Smillert $ignorecase = $action ? 2 : 0; 1351b39c5158Smillert } 1352b39c5158Smillert elsif ( $try eq 'bundling' ) { 1353b39c5158Smillert $bundling = $action; 1354b8851fccSafresh1 $bundling_values = 0 if $action; 1355b39c5158Smillert } 1356b39c5158Smillert elsif ( $try eq 'bundling_override' ) { 1357b39c5158Smillert $bundling = $action ? 2 : 0; 1358b8851fccSafresh1 $bundling_values = 0 if $action; 1359b8851fccSafresh1 } 1360b8851fccSafresh1 elsif ( $try eq 'bundling_values' ) { 1361b8851fccSafresh1 $bundling_values = $action; 1362b8851fccSafresh1 $bundling = 0 if $action; 1363b39c5158Smillert } 1364b39c5158Smillert elsif ( $try eq 'require_order' ) { 1365b39c5158Smillert $order = $action ? $REQUIRE_ORDER : $PERMUTE; 1366b39c5158Smillert } 1367b39c5158Smillert elsif ( $try eq 'permute' ) { 1368b39c5158Smillert $order = $action ? $PERMUTE : $REQUIRE_ORDER; 1369b39c5158Smillert } 1370b39c5158Smillert elsif ( $try eq 'pass_through' or $try eq 'passthrough' ) { 1371b39c5158Smillert $passthrough = $action; 1372b39c5158Smillert } 1373b39c5158Smillert elsif ( $try =~ /^prefix=(.+)$/ && $action ) { 1374b39c5158Smillert $genprefix = $1; 1375b39c5158Smillert # Turn into regexp. Needs to be parenthesized! 1376b39c5158Smillert $genprefix = "(" . quotemeta($genprefix) . ")"; 1377b39c5158Smillert eval { '' =~ /$genprefix/; }; 1378e9ce3842Safresh1 die("Getopt::Long: invalid pattern \"$genprefix\"\n") if $@; 1379b39c5158Smillert } 1380b39c5158Smillert elsif ( $try =~ /^prefix_pattern=(.+)$/ && $action ) { 1381b39c5158Smillert $genprefix = $1; 1382b39c5158Smillert # Parenthesize if needed. 1383b39c5158Smillert $genprefix = "(" . $genprefix . ")" 1384b39c5158Smillert unless $genprefix =~ /^\(.*\)$/; 1385b39c5158Smillert eval { '' =~ m"$genprefix"; }; 1386e9ce3842Safresh1 die("Getopt::Long: invalid pattern \"$genprefix\"\n") if $@; 1387b39c5158Smillert } 1388b39c5158Smillert elsif ( $try =~ /^long_prefix_pattern=(.+)$/ && $action ) { 1389b39c5158Smillert $longprefix = $1; 1390b39c5158Smillert # Parenthesize if needed. 1391b39c5158Smillert $longprefix = "(" . $longprefix . ")" 1392b39c5158Smillert unless $longprefix =~ /^\(.*\)$/; 1393b39c5158Smillert eval { '' =~ m"$longprefix"; }; 1394e9ce3842Safresh1 die("Getopt::Long: invalid long prefix pattern \"$longprefix\"\n") if $@; 1395b39c5158Smillert } 1396b39c5158Smillert elsif ( $try eq 'debug' ) { 1397b39c5158Smillert $debug = $action; 1398b39c5158Smillert } 1399b39c5158Smillert else { 1400e9ce3842Safresh1 die("Getopt::Long: unknown or erroneous config parameter \"$opt\"\n") 1401b39c5158Smillert } 1402b39c5158Smillert } 1403b39c5158Smillert $prevconfig; 1404b39c5158Smillert} 1405b39c5158Smillert 1406b39c5158Smillert# Deprecated name. 1407b39c5158Smillertsub config (@) { 1408b39c5158Smillert Configure (@_); 1409b39c5158Smillert} 1410b39c5158Smillert 1411b39c5158Smillert# Issue a standard message for --version. 1412b39c5158Smillert# 1413b39c5158Smillert# The arguments are mostly the same as for Pod::Usage::pod2usage: 1414b39c5158Smillert# 1415b39c5158Smillert# - a number (exit value) 1416b39c5158Smillert# - a string (lead in message) 1417b39c5158Smillert# - a hash with options. See Pod::Usage for details. 1418b39c5158Smillert# 1419b39c5158Smillertsub VersionMessage(@) { 1420b39c5158Smillert # Massage args. 1421b39c5158Smillert my $pa = setup_pa_args("version", @_); 1422b39c5158Smillert 1423b39c5158Smillert my $v = $main::VERSION; 1424b39c5158Smillert my $fh = $pa->{-output} || 14259f11ffb7Safresh1 ( ($pa->{-exitval} eq "NOEXIT" || $pa->{-exitval} < 2) ? \*STDOUT : \*STDERR ); 1426b39c5158Smillert 1427b39c5158Smillert print $fh (defined($pa->{-message}) ? $pa->{-message} : (), 1428b39c5158Smillert $0, defined $v ? " version $v" : (), 1429b39c5158Smillert "\n", 1430b39c5158Smillert "(", __PACKAGE__, "::", "GetOptions", 1431*3d61058aSafresh1 " version $VERSION,", 1432b39c5158Smillert " Perl version ", 1433b39c5158Smillert $] >= 5.006 ? sprintf("%vd", $^V) : $], 1434b39c5158Smillert ")\n"); 1435b39c5158Smillert exit($pa->{-exitval}) unless $pa->{-exitval} eq "NOEXIT"; 1436b39c5158Smillert} 1437b39c5158Smillert 1438b39c5158Smillert# Issue a standard message for --help. 1439b39c5158Smillert# 1440b39c5158Smillert# The arguments are the same as for Pod::Usage::pod2usage: 1441b39c5158Smillert# 1442b39c5158Smillert# - a number (exit value) 1443b39c5158Smillert# - a string (lead in message) 1444b39c5158Smillert# - a hash with options. See Pod::Usage for details. 1445b39c5158Smillert# 1446b39c5158Smillertsub HelpMessage(@) { 1447b39c5158Smillert eval { 1448b39c5158Smillert require Pod::Usage; 1449*3d61058aSafresh1 Pod::Usage->import; 1450b39c5158Smillert 1; 1451b39c5158Smillert } || die("Cannot provide help: cannot load Pod::Usage\n"); 1452b39c5158Smillert 1453b39c5158Smillert # Note that pod2usage will issue a warning if -exitval => NOEXIT. 1454b39c5158Smillert pod2usage(setup_pa_args("help", @_)); 1455b39c5158Smillert 1456b39c5158Smillert} 1457b39c5158Smillert 1458b39c5158Smillert# Helper routine to set up a normalized hash ref to be used as 1459b39c5158Smillert# argument to pod2usage. 1460b39c5158Smillertsub setup_pa_args($@) { 1461b39c5158Smillert my $tag = shift; # who's calling 1462b39c5158Smillert 1463b39c5158Smillert # If called by direct binding to an option, it will get the option 1464b39c5158Smillert # name and value as arguments. Remove these, if so. 1465b39c5158Smillert @_ = () if @_ == 2 && $_[0] eq $tag; 1466b39c5158Smillert 1467b39c5158Smillert my $pa; 1468b39c5158Smillert if ( @_ > 1 ) { 1469b39c5158Smillert $pa = { @_ }; 1470b39c5158Smillert } 1471b39c5158Smillert else { 1472b39c5158Smillert $pa = shift || {}; 1473b39c5158Smillert } 1474b39c5158Smillert 1475b39c5158Smillert # At this point, $pa can be a number (exit value), string 1476b39c5158Smillert # (message) or hash with options. 1477b39c5158Smillert 1478b39c5158Smillert if ( UNIVERSAL::isa($pa, 'HASH') ) { 1479b39c5158Smillert # Get rid of -msg vs. -message ambiguity. 1480e0680481Safresh1 if (!defined $pa->{-message}) { 1481e0680481Safresh1 $pa->{-message} = delete($pa->{-msg}); 1482e0680481Safresh1 } 1483b39c5158Smillert } 1484b39c5158Smillert elsif ( $pa =~ /^-?\d+$/ ) { 1485b39c5158Smillert $pa = { -exitval => $pa }; 1486b39c5158Smillert } 1487b39c5158Smillert else { 1488b39c5158Smillert $pa = { -message => $pa }; 1489b39c5158Smillert } 1490b39c5158Smillert 1491b39c5158Smillert # These are _our_ defaults. 1492b39c5158Smillert $pa->{-verbose} = 0 unless exists($pa->{-verbose}); 1493b39c5158Smillert $pa->{-exitval} = 0 unless exists($pa->{-exitval}); 1494b39c5158Smillert $pa; 1495b39c5158Smillert} 1496b39c5158Smillert 1497b39c5158Smillert# Sneak way to know what version the user requested. 1498b39c5158Smillertsub VERSION { 149956d68f1eSafresh1 $requested_version = $_[1] if @_ > 1; 1500b39c5158Smillert shift->SUPER::VERSION(@_); 1501b39c5158Smillert} 1502b39c5158Smillert 1503b39c5158Smillertpackage Getopt::Long::CallBack; 1504b39c5158Smillert 1505b39c5158Smillertsub new { 1506b39c5158Smillert my ($pkg, %atts) = @_; 1507b39c5158Smillert bless { %atts }, $pkg; 1508b39c5158Smillert} 1509b39c5158Smillert 1510b39c5158Smillertsub name { 1511b39c5158Smillert my $self = shift; 1512b39c5158Smillert ''.$self->{name}; 1513b39c5158Smillert} 1514b39c5158Smillert 1515eac174f2Safresh1sub given { 1516eac174f2Safresh1 my $self = shift; 1517eac174f2Safresh1 $self->{given}; 1518eac174f2Safresh1} 1519eac174f2Safresh1 1520b39c5158Smillertuse overload 1521b39c5158Smillert # Treat this object as an ordinary string for legacy API. 1522b39c5158Smillert '""' => \&name, 1523b39c5158Smillert fallback => 1; 1524b39c5158Smillert 1525b39c5158Smillert1; 1526b39c5158Smillert 1527b39c5158Smillert################ Documentation ################ 1528b39c5158Smillert 1529b39c5158Smillert=head1 NAME 1530b39c5158Smillert 1531b39c5158SmillertGetopt::Long - Extended processing of command line options 1532b39c5158Smillert 1533b39c5158Smillert=head1 SYNOPSIS 1534b39c5158Smillert 1535b39c5158Smillert use Getopt::Long; 1536b39c5158Smillert my $data = "file.dat"; 1537b39c5158Smillert my $length = 24; 1538b39c5158Smillert my $verbose; 1539e9ce3842Safresh1 GetOptions ("length=i" => \$length, # numeric 1540b39c5158Smillert "file=s" => \$data, # string 1541e9ce3842Safresh1 "verbose" => \$verbose) # flag 1542e9ce3842Safresh1 or die("Error in command line arguments\n"); 1543b39c5158Smillert 1544b39c5158Smillert=head1 DESCRIPTION 1545b39c5158Smillert 1546b39c5158SmillertThe Getopt::Long module implements an extended getopt function called 1547e9ce3842Safresh1GetOptions(). It parses the command line from C<@ARGV>, recognizing 1548e9ce3842Safresh1and removing specified options and their possible values. 1549e9ce3842Safresh1 1550e9ce3842Safresh1This function adheres to the POSIX syntax for command 1551b39c5158Smillertline options, with GNU extensions. In general, this means that options 1552b39c5158Smillerthave long names instead of single letters, and are introduced with a 1553b39c5158Smillertdouble dash "--". Support for bundling of command line options, as was 1554b39c5158Smillertthe case with the more traditional single-letter approach, is provided 1555b39c5158Smillertbut not enabled by default. 1556b39c5158Smillert 1557b39c5158Smillert=head1 Command Line Options, an Introduction 1558b39c5158Smillert 1559b39c5158SmillertCommand line operated programs traditionally take their arguments from 1560b39c5158Smillertthe command line, for example filenames or other information that the 1561b39c5158Smillertprogram needs to know. Besides arguments, these programs often take 1562b39c5158Smillertcommand line I<options> as well. Options are not necessary for the 1563b39c5158Smillertprogram to work, hence the name 'option', but are used to modify its 1564b39c5158Smillertdefault behaviour. For example, a program could do its job quietly, 1565b39c5158Smillertbut with a suitable option it could provide verbose information about 1566b39c5158Smillertwhat it did. 1567b39c5158Smillert 1568b39c5158SmillertCommand line options come in several flavours. Historically, they are 1569b39c5158Smillertpreceded by a single dash C<->, and consist of a single letter. 1570b39c5158Smillert 1571b39c5158Smillert -l -a -c 1572b39c5158Smillert 1573b39c5158SmillertUsually, these single-character options can be bundled: 1574b39c5158Smillert 1575b39c5158Smillert -lac 1576b39c5158Smillert 1577b39c5158SmillertOptions can have values, the value is placed after the option 1578b39c5158Smillertcharacter. Sometimes with whitespace in between, sometimes not: 1579b39c5158Smillert 1580b39c5158Smillert -s 24 -s24 1581b39c5158Smillert 1582b39c5158SmillertDue to the very cryptic nature of these options, another style was 1583b39c5158Smillertdeveloped that used long names. So instead of a cryptic C<-l> one 1584b39c5158Smillertcould use the more descriptive C<--long>. To distinguish between a 1585b39c5158Smillertbundle of single-character options and a long one, two dashes are used 1586b39c5158Smillertto precede the option name. Early implementations of long options used 1587b39c5158Smillerta plus C<+> instead. Also, option values could be specified either 1588b39c5158Smillertlike 1589b39c5158Smillert 1590b39c5158Smillert --size=24 1591b39c5158Smillert 1592b39c5158Smillertor 1593b39c5158Smillert 1594b39c5158Smillert --size 24 1595b39c5158Smillert 1596b39c5158SmillertThe C<+> form is now obsolete and strongly deprecated. 1597b39c5158Smillert 1598b39c5158Smillert=head1 Getting Started with Getopt::Long 1599b39c5158Smillert 1600b39c5158SmillertGetopt::Long is the Perl5 successor of C<newgetopt.pl>. This was the 1601b39c5158Smillertfirst Perl module that provided support for handling the new style of 1602e9ce3842Safresh1command line options, in particular long option names, hence the Perl5 1603e9ce3842Safresh1name Getopt::Long. This module also supports single-character options 1604e9ce3842Safresh1and bundling. 1605b39c5158Smillert 1606b39c5158SmillertTo use Getopt::Long from a Perl program, you must include the 1607b39c5158Smillertfollowing line in your Perl program: 1608b39c5158Smillert 1609b39c5158Smillert use Getopt::Long; 1610b39c5158Smillert 1611b39c5158SmillertThis will load the core of the Getopt::Long module and prepare your 1612b39c5158Smillertprogram for using it. Most of the actual Getopt::Long code is not 1613b39c5158Smillertloaded until you really call one of its functions. 1614b39c5158Smillert 1615b39c5158SmillertIn the default configuration, options names may be abbreviated to 1616b39c5158Smillertuniqueness, case does not matter, and a single dash is sufficient, 1617b39c5158Smillerteven for long option names. Also, options may be placed between 1618b39c5158Smillertnon-option arguments. See L<Configuring Getopt::Long> for more 1619b39c5158Smillertdetails on how to configure Getopt::Long. 1620b39c5158Smillert 1621b39c5158Smillert=head2 Simple options 1622b39c5158Smillert 1623b39c5158SmillertThe most simple options are the ones that take no values. Their mere 1624b39c5158Smillertpresence on the command line enables the option. Popular examples are: 1625b39c5158Smillert 1626b39c5158Smillert --all --verbose --quiet --debug 1627b39c5158Smillert 1628b39c5158SmillertHandling simple options is straightforward: 1629b39c5158Smillert 1630b39c5158Smillert my $verbose = ''; # option variable with default value (false) 1631b39c5158Smillert my $all = ''; # option variable with default value (false) 1632b39c5158Smillert GetOptions ('verbose' => \$verbose, 'all' => \$all); 1633b39c5158Smillert 1634b39c5158SmillertThe call to GetOptions() parses the command line arguments that are 1635b39c5158Smillertpresent in C<@ARGV> and sets the option variable to the value C<1> if 1636b39c5158Smillertthe option did occur on the command line. Otherwise, the option 1637b39c5158Smillertvariable is not touched. Setting the option value to true is often 1638b39c5158Smillertcalled I<enabling> the option. 1639b39c5158Smillert 1640b39c5158SmillertThe option name as specified to the GetOptions() function is called 1641b39c5158Smillertthe option I<specification>. Later we'll see that this specification 1642b39c5158Smillertcan contain more than just the option name. The reference to the 1643b39c5158Smillertvariable is called the option I<destination>. 1644b39c5158Smillert 1645b39c5158SmillertGetOptions() will return a true value if the command line could be 1646e9ce3842Safresh1processed successfully. Otherwise, it will write error messages using 1647e9ce3842Safresh1die() and warn(), and return a false result. 1648b39c5158Smillert 1649b39c5158Smillert=head2 A little bit less simple options 1650b39c5158Smillert 1651b39c5158SmillertGetopt::Long supports two useful variants of simple options: 1652b39c5158SmillertI<negatable> options and I<incremental> options. 1653b39c5158Smillert 1654b39c5158SmillertA negatable option is specified with an exclamation mark C<!> after the 1655b39c5158Smillertoption name: 1656b39c5158Smillert 1657b39c5158Smillert my $verbose = ''; # option variable with default value (false) 1658b39c5158Smillert GetOptions ('verbose!' => \$verbose); 1659b39c5158Smillert 1660b39c5158SmillertNow, using C<--verbose> on the command line will enable C<$verbose>, 1661b39c5158Smillertas expected. But it is also allowed to use C<--noverbose>, which will 1662b39c5158Smillertdisable C<$verbose> by setting its value to C<0>. Using a suitable 1663b39c5158Smillertdefault value, the program can find out whether C<$verbose> is false 1664b39c5158Smillertby default, or disabled by using C<--noverbose>. 1665b39c5158Smillert 1666e0680481Safresh1(If both C<--verbose> and C<--noverbose> are given, whichever is given 1667e0680481Safresh1last takes precedence.) 1668e0680481Safresh1 1669b39c5158SmillertAn incremental option is specified with a plus C<+> after the 1670b39c5158Smillertoption name: 1671b39c5158Smillert 1672b39c5158Smillert my $verbose = ''; # option variable with default value (false) 1673b39c5158Smillert GetOptions ('verbose+' => \$verbose); 1674b39c5158Smillert 1675b39c5158SmillertUsing C<--verbose> on the command line will increment the value of 1676b39c5158SmillertC<$verbose>. This way the program can keep track of how many times the 1677b39c5158Smillertoption occurred on the command line. For example, each occurrence of 1678b39c5158SmillertC<--verbose> could increase the verbosity level of the program. 1679b39c5158Smillert 1680b39c5158Smillert=head2 Mixing command line option with other arguments 1681b39c5158Smillert 1682b39c5158SmillertUsually programs take command line options as well as other arguments, 1683b39c5158Smillertfor example, file names. It is good practice to always specify the 1684b39c5158Smillertoptions first, and the other arguments last. Getopt::Long will, 1685b39c5158Smillerthowever, allow the options and arguments to be mixed and 'filter out' 1686b39c5158Smillertall the options before passing the rest of the arguments to the 1687b39c5158Smillertprogram. To stop Getopt::Long from processing further arguments, 1688b39c5158Smillertinsert a double dash C<--> on the command line: 1689b39c5158Smillert 1690b39c5158Smillert --size 24 -- --all 1691b39c5158Smillert 1692b39c5158SmillertIn this example, C<--all> will I<not> be treated as an option, but 1693b39c5158Smillertpassed to the program unharmed, in C<@ARGV>. 1694b39c5158Smillert 1695b39c5158Smillert=head2 Options with values 1696b39c5158Smillert 1697b39c5158SmillertFor options that take values it must be specified whether the option 1698b39c5158Smillertvalue is required or not, and what kind of value the option expects. 1699b39c5158Smillert 1700b39c5158SmillertThree kinds of values are supported: integer numbers, floating point 1701b39c5158Smillertnumbers, and strings. 1702b39c5158Smillert 1703b39c5158SmillertIf the option value is required, Getopt::Long will take the 1704b39c5158Smillertcommand line argument that follows the option and assign this to the 1705b39c5158Smillertoption variable. If, however, the option value is specified as 1706b39c5158Smillertoptional, this will only be done if that value does not look like a 1707b39c5158Smillertvalid command line option itself. 1708b39c5158Smillert 1709b39c5158Smillert my $tag = ''; # option variable with default value 1710b39c5158Smillert GetOptions ('tag=s' => \$tag); 1711b39c5158Smillert 1712b39c5158SmillertIn the option specification, the option name is followed by an equals 1713b39c5158Smillertsign C<=> and the letter C<s>. The equals sign indicates that this 1714b39c5158Smillertoption requires a value. The letter C<s> indicates that this value is 1715b39c5158Smillertan arbitrary string. Other possible value types are C<i> for integer 1716b39c5158Smillertvalues, and C<f> for floating point values. Using a colon C<:> instead 1717b39c5158Smillertof the equals sign indicates that the option value is optional. In 1718b39c5158Smillertthis case, if no suitable value is supplied, string valued options get 1719b39c5158Smillertan empty string C<''> assigned, while numeric options are set to C<0>. 1720b39c5158Smillert 1721e0680481Safresh1(If the same option appears more than once on the command line, the 1722e0680481Safresh1last given value is used. If you want to take all the values, see 1723e0680481Safresh1below.) 1724e0680481Safresh1 1725b39c5158Smillert=head2 Options with multiple values 1726b39c5158Smillert 1727b39c5158SmillertOptions sometimes take several values. For example, a program could 1728b39c5158Smillertuse multiple directories to search for library files: 1729b39c5158Smillert 1730b39c5158Smillert --library lib/stdlib --library lib/extlib 1731b39c5158Smillert 1732b39c5158SmillertTo accomplish this behaviour, simply specify an array reference as the 1733b39c5158Smillertdestination for the option: 1734b39c5158Smillert 1735b39c5158Smillert GetOptions ("library=s" => \@libfiles); 1736b39c5158Smillert 1737b39c5158SmillertAlternatively, you can specify that the option can have multiple 17389f11ffb7Safresh1values by adding a "@", and pass a reference to a scalar as the 1739b39c5158Smillertdestination: 1740b39c5158Smillert 1741b39c5158Smillert GetOptions ("library=s@" => \$libfiles); 1742b39c5158Smillert 17439f11ffb7Safresh1Used with the example above, C<@libfiles> c.q. C<@$libfiles> would 1744e9ce3842Safresh1contain two strings upon completion: C<"lib/stdlib"> and 1745b39c5158SmillertC<"lib/extlib">, in that order. It is also possible to specify that 1746b39c5158Smillertonly integer or floating point numbers are acceptable values. 1747b39c5158Smillert 1748b39c5158SmillertOften it is useful to allow comma-separated lists of values as well as 1749b39c5158Smillertmultiple occurrences of the options. This is easy using Perl's split() 1750b39c5158Smillertand join() operators: 1751b39c5158Smillert 1752b39c5158Smillert GetOptions ("library=s" => \@libfiles); 1753b39c5158Smillert @libfiles = split(/,/,join(',',@libfiles)); 1754b39c5158Smillert 1755b39c5158SmillertOf course, it is important to choose the right separator string for 1756b39c5158Smillerteach purpose. 1757b39c5158Smillert 1758b39c5158SmillertWarning: What follows is an experimental feature. 1759b39c5158Smillert 1760b39c5158SmillertOptions can take multiple values at once, for example 1761b39c5158Smillert 1762b39c5158Smillert --coordinates 52.2 16.4 --rgbcolor 255 255 149 1763b39c5158Smillert 1764b39c5158SmillertThis can be accomplished by adding a repeat specifier to the option 1765b39c5158Smillertspecification. Repeat specifiers are very similar to the C<{...}> 1766b39c5158Smillertrepeat specifiers that can be used with regular expression patterns. 1767b39c5158SmillertFor example, the above command line would be handled as follows: 1768b39c5158Smillert 1769b39c5158Smillert GetOptions('coordinates=f{2}' => \@coor, 'rgbcolor=i{3}' => \@color); 1770b39c5158Smillert 1771b39c5158SmillertThe destination for the option must be an array or array reference. 1772b39c5158Smillert 1773b39c5158SmillertIt is also possible to specify the minimal and maximal number of 1774b39c5158Smillertarguments an option takes. C<foo=s{2,4}> indicates an option that 1775e9ce3842Safresh1takes at least two and at most 4 arguments. C<foo=s{1,}> indicates one 1776b39c5158Smillertor more values; C<foo:s{,}> indicates zero or more option values. 1777b39c5158Smillert 1778b39c5158Smillert=head2 Options with hash values 1779b39c5158Smillert 1780b39c5158SmillertIf the option destination is a reference to a hash, the option will 1781b39c5158Smillerttake, as value, strings of the form I<key>C<=>I<value>. The value will 1782b39c5158Smillertbe stored with the specified key in the hash. 1783b39c5158Smillert 1784b39c5158Smillert GetOptions ("define=s" => \%defines); 1785b39c5158Smillert 1786b39c5158SmillertAlternatively you can use: 1787b39c5158Smillert 1788b39c5158Smillert GetOptions ("define=s%" => \$defines); 1789b39c5158Smillert 1790b39c5158SmillertWhen used with command line options: 1791b39c5158Smillert 1792b39c5158Smillert --define os=linux --define vendor=redhat 1793b39c5158Smillert 1794b39c5158Smillertthe hash C<%defines> (or C<%$defines>) will contain two keys, C<"os"> 1795b39c5158Smillertwith value C<"linux"> and C<"vendor"> with value C<"redhat">. It is 1796b39c5158Smillertalso possible to specify that only integer or floating point numbers 1797b39c5158Smillertare acceptable values. The keys are always taken to be strings. 1798b39c5158Smillert 1799b39c5158Smillert=head2 User-defined subroutines to handle options 1800b39c5158Smillert 1801b39c5158SmillertUltimate control over what should be done when (actually: each time) 1802b39c5158Smillertan option is encountered on the command line can be achieved by 1803b39c5158Smillertdesignating a reference to a subroutine (or an anonymous subroutine) 1804b39c5158Smillertas the option destination. When GetOptions() encounters the option, it 1805b39c5158Smillertwill call the subroutine with two or three arguments. The first 1806b39c5158Smillertargument is the name of the option. (Actually, it is an object that 1807b39c5158Smillertstringifies to the name of the option.) For a scalar or array destination, 1808b39c5158Smillertthe second argument is the value to be stored. For a hash destination, 1809e9ce3842Safresh1the second argument is the key to the hash, and the third argument 1810b39c5158Smillertthe value to be stored. It is up to the subroutine to store the value, 1811b39c5158Smillertor do whatever it thinks is appropriate. 1812b39c5158Smillert 1813b39c5158SmillertA trivial application of this mechanism is to implement options that 1814b39c5158Smillertare related to each other. For example: 1815b39c5158Smillert 1816b39c5158Smillert my $verbose = ''; # option variable with default value (false) 1817b39c5158Smillert GetOptions ('verbose' => \$verbose, 1818b39c5158Smillert 'quiet' => sub { $verbose = 0 }); 1819b39c5158Smillert 1820b39c5158SmillertHere C<--verbose> and C<--quiet> control the same variable 1821b39c5158SmillertC<$verbose>, but with opposite values. 1822b39c5158Smillert 1823b39c5158SmillertIf the subroutine needs to signal an error, it should call die() with 1824b39c5158Smillertthe desired error message as its argument. GetOptions() will catch the 1825b39c5158Smillertdie(), issue the error message, and record that an error result must 1826b39c5158Smillertbe returned upon completion. 1827b39c5158Smillert 1828b39c5158SmillertIf the text of the error message starts with an exclamation mark C<!> 1829b39c5158Smillertit is interpreted specially by GetOptions(). There is currently one 1830b39c5158Smillertspecial command implemented: C<die("!FINISH")> will cause GetOptions() 1831b39c5158Smillertto stop processing options, as if it encountered a double dash C<-->. 1832b39c5158Smillert 1833e9ce3842Safresh1Here is an example of how to access the option name and value from within 1834e9ce3842Safresh1a subroutine: 1835e9ce3842Safresh1 1836e9ce3842Safresh1 GetOptions ('opt=i' => \&handler); 1837e9ce3842Safresh1 sub handler { 1838e9ce3842Safresh1 my ($opt_name, $opt_value) = @_; 1839e9ce3842Safresh1 print("Option name is $opt_name and value is $opt_value\n"); 1840e9ce3842Safresh1 } 1841e9ce3842Safresh1 1842b39c5158Smillert=head2 Options with multiple names 1843b39c5158Smillert 1844b39c5158SmillertOften it is user friendly to supply alternate mnemonic names for 1845b39c5158Smillertoptions. For example C<--height> could be an alternate name for 1846b39c5158SmillertC<--length>. Alternate names can be included in the option 1847b39c5158Smillertspecification, separated by vertical bar C<|> characters. To implement 1848b39c5158Smillertthe above example: 1849b39c5158Smillert 1850b39c5158Smillert GetOptions ('length|height=f' => \$length); 1851b39c5158Smillert 1852b39c5158SmillertThe first name is called the I<primary> name, the other names are 1853b39c5158Smillertcalled I<aliases>. When using a hash to store options, the key will 1854b39c5158Smillertalways be the primary name. 1855b39c5158Smillert 1856b39c5158SmillertMultiple alternate names are possible. 1857b39c5158Smillert 1858b39c5158Smillert=head2 Case and abbreviations 1859b39c5158Smillert 1860b39c5158SmillertWithout additional configuration, GetOptions() will ignore the case of 1861b39c5158Smillertoption names, and allow the options to be abbreviated to uniqueness. 1862b39c5158Smillert 1863b39c5158Smillert GetOptions ('length|height=f' => \$length, "head" => \$head); 1864b39c5158Smillert 1865b39c5158SmillertThis call will allow C<--l> and C<--L> for the length option, but 1866b39c5158Smillertrequires a least C<--hea> and C<--hei> for the head and height options. 1867b39c5158Smillert 1868b39c5158Smillert=head2 Summary of Option Specifications 1869b39c5158Smillert 1870b39c5158SmillertEach option specifier consists of two parts: the name specification 1871b39c5158Smillertand the argument specification. 1872b39c5158Smillert 1873b39c5158SmillertThe name specification contains the name of the option, optionally 1874b39c5158Smillertfollowed by a list of alternative names separated by vertical bar 1875*3d61058aSafresh1characters. The name is made up of alphanumeric characters, hyphens, 1876*3d61058aSafresh1underscores. If C<pass_through> is disabled, a period is also allowed in 1877*3d61058aSafresh1option names. 1878b39c5158Smillert 1879b39c5158Smillert length option name is "length" 1880b39c5158Smillert length|size|l name is "length", aliases are "size" and "l" 1881b39c5158Smillert 1882b39c5158SmillertThe argument specification is optional. If omitted, the option is 1883b39c5158Smillertconsidered boolean, a value of 1 will be assigned when the option is 1884b39c5158Smillertused on the command line. 1885b39c5158Smillert 1886b39c5158SmillertThe argument specification can be 1887b39c5158Smillert 1888b39c5158Smillert=over 4 1889b39c5158Smillert 1890b39c5158Smillert=item ! 1891b39c5158Smillert 1892b39c5158SmillertThe option does not take an argument and may be negated by prefixing 1893b39c5158Smillertit with "no" or "no-". E.g. C<"foo!"> will allow C<--foo> (a value of 1894b39c5158Smillert1 will be assigned) as well as C<--nofoo> and C<--no-foo> (a value of 1895b39c5158Smillert0 will be assigned). If the option has aliases, this applies to the 1896b39c5158Smillertaliases as well. 1897b39c5158Smillert 1898b39c5158SmillertUsing negation on a single letter option when bundling is in effect is 1899b39c5158Smillertpointless and will result in a warning. 1900b39c5158Smillert 1901b39c5158Smillert=item + 1902b39c5158Smillert 1903b39c5158SmillertThe option does not take an argument and will be incremented by 1 1904b39c5158Smillertevery time it appears on the command line. E.g. C<"more+">, when used 1905b39c5158Smillertwith C<--more --more --more>, will increment the value three times, 1906b39c5158Smillertresulting in a value of 3 (provided it was 0 or undefined at first). 1907b39c5158Smillert 1908b39c5158SmillertThe C<+> specifier is ignored if the option destination is not a scalar. 1909b39c5158Smillert 1910b39c5158Smillert=item = I<type> [ I<desttype> ] [ I<repeat> ] 1911b39c5158Smillert 1912b39c5158SmillertThe option requires an argument of the given type. Supported types 1913b39c5158Smillertare: 1914b39c5158Smillert 1915b39c5158Smillert=over 4 1916b39c5158Smillert 1917b39c5158Smillert=item s 1918b39c5158Smillert 1919b39c5158SmillertString. An arbitrary sequence of characters. It is valid for the 1920b39c5158Smillertargument to start with C<-> or C<-->. 1921b39c5158Smillert 1922b39c5158Smillert=item i 1923b39c5158Smillert 1924b39c5158SmillertInteger. An optional leading plus or minus sign, followed by a 1925b39c5158Smillertsequence of digits. 1926b39c5158Smillert 1927b39c5158Smillert=item o 1928b39c5158Smillert 1929b39c5158SmillertExtended integer, Perl style. This can be either an optional leading 1930b39c5158Smillertplus or minus sign, followed by a sequence of digits, or an octal 1931b39c5158Smillertstring (a zero, optionally followed by '0', '1', .. '7'), or a 1932b39c5158Smillerthexadecimal string (C<0x> followed by '0' .. '9', 'a' .. 'f', case 1933b39c5158Smillertinsensitive), or a binary string (C<0b> followed by a series of '0' 1934b39c5158Smillertand '1'). 1935b39c5158Smillert 1936b39c5158Smillert=item f 1937b39c5158Smillert 1938b39c5158SmillertReal number. For example C<3.14>, C<-6.23E24> and so on. 1939b39c5158Smillert 1940b39c5158Smillert=back 1941b39c5158Smillert 1942b39c5158SmillertThe I<desttype> can be C<@> or C<%> to specify that the option is 1943b39c5158Smillertlist or a hash valued. This is only needed when the destination for 1944b39c5158Smillertthe option value is not otherwise specified. It should be omitted when 1945b39c5158Smillertnot needed. 1946b39c5158Smillert 1947b39c5158SmillertThe I<repeat> specifies the number of values this option takes per 1948b39c5158Smillertoccurrence on the command line. It has the format C<{> [ I<min> ] [ C<,> [ I<max> ] ] C<}>. 1949b39c5158Smillert 1950b39c5158SmillertI<min> denotes the minimal number of arguments. It defaults to 1 for 1951b39c5158Smillertoptions with C<=> and to 0 for options with C<:>, see below. Note that 1952b39c5158SmillertI<min> overrules the C<=> / C<:> semantics. 1953b39c5158Smillert 1954b39c5158SmillertI<max> denotes the maximum number of arguments. It must be at least 1955b39c5158SmillertI<min>. If I<max> is omitted, I<but the comma is not>, there is no 1956b39c5158Smillertupper bound to the number of argument values taken. 1957b39c5158Smillert 1958b39c5158Smillert=item : I<type> [ I<desttype> ] 1959b39c5158Smillert 1960b39c5158SmillertLike C<=>, but designates the argument as optional. 1961b39c5158SmillertIf omitted, an empty string will be assigned to string values options, 1962b39c5158Smillertand the value zero to numeric options. 1963b39c5158Smillert 1964b39c5158SmillertNote that if a string argument starts with C<-> or C<-->, it will be 1965b39c5158Smillertconsidered an option on itself. 1966b39c5158Smillert 1967b39c5158Smillert=item : I<number> [ I<desttype> ] 1968b39c5158Smillert 1969b39c5158SmillertLike C<:i>, but if the value is omitted, the I<number> will be assigned. 1970b39c5158Smillert 1971e0680481Safresh1If the I<number> is octal, hexadecimal or binary, behaves like C<:o>. 1972e0680481Safresh1 1973b39c5158Smillert=item : + [ I<desttype> ] 1974b39c5158Smillert 1975b39c5158SmillertLike C<:i>, but if the value is omitted, the current value for the 1976b39c5158Smillertoption will be incremented. 1977b39c5158Smillert 1978b39c5158Smillert=back 1979b39c5158Smillert 1980b39c5158Smillert=head1 Advanced Possibilities 1981b39c5158Smillert 1982b39c5158Smillert=head2 Object oriented interface 1983b39c5158Smillert 1984*3d61058aSafresh1See L<Getopt::Long::Parser>. 1985b39c5158Smillert 1986eac174f2Safresh1=head2 Callback object 1987eac174f2Safresh1 1988eac174f2Safresh1In version 2.37 the first argument to the callback function was 1989eac174f2Safresh1changed from string to object. This was done to make room for 1990eac174f2Safresh1extensions and more detailed control. The object stringifies to the 1991eac174f2Safresh1option name so this change should not introduce compatibility 1992eac174f2Safresh1problems. 1993eac174f2Safresh1 1994eac174f2Safresh1The callback object has the following methods: 1995eac174f2Safresh1 1996eac174f2Safresh1=over 1997eac174f2Safresh1 1998eac174f2Safresh1=item name 1999eac174f2Safresh1 2000eac174f2Safresh1The name of the option, unabbreviated. For an option with multiple 2001eac174f2Safresh1names it return the first (canonical) name. 2002eac174f2Safresh1 2003eac174f2Safresh1=item given 2004eac174f2Safresh1 2005eac174f2Safresh1The name of the option as actually used, unabbreveated. 2006eac174f2Safresh1 2007eac174f2Safresh1=back 2008eac174f2Safresh1 2009b39c5158Smillert=head2 Thread Safety 2010b39c5158Smillert 2011b39c5158SmillertGetopt::Long is thread safe when using ithreads as of Perl 5.8. It is 2012b39c5158SmillertI<not> thread safe when using the older (experimental and now 2013b39c5158Smillertobsolete) threads implementation that was added to Perl 5.005. 2014b39c5158Smillert 2015b39c5158Smillert=head2 Documentation and help texts 2016b39c5158Smillert 2017b39c5158SmillertGetopt::Long encourages the use of Pod::Usage to produce help 2018b39c5158Smillertmessages. For example: 2019b39c5158Smillert 2020b39c5158Smillert use Getopt::Long; 2021b39c5158Smillert use Pod::Usage; 2022b39c5158Smillert 2023b39c5158Smillert my $man = 0; 2024b39c5158Smillert my $help = 0; 2025b39c5158Smillert 2026b39c5158Smillert GetOptions('help|?' => \$help, man => \$man) or pod2usage(2); 2027b39c5158Smillert pod2usage(1) if $help; 2028e9ce3842Safresh1 pod2usage(-exitval => 0, -verbose => 2) if $man; 2029b39c5158Smillert 2030b39c5158Smillert __END__ 2031b39c5158Smillert 2032b39c5158Smillert =head1 NAME 2033b39c5158Smillert 2034b39c5158Smillert sample - Using Getopt::Long and Pod::Usage 2035b39c5158Smillert 2036b39c5158Smillert =head1 SYNOPSIS 2037b39c5158Smillert 2038b39c5158Smillert sample [options] [file ...] 2039b39c5158Smillert 2040b39c5158Smillert Options: 2041b39c5158Smillert -help brief help message 2042b39c5158Smillert -man full documentation 2043b39c5158Smillert 2044b39c5158Smillert =head1 OPTIONS 2045b39c5158Smillert 2046b39c5158Smillert =over 8 2047b39c5158Smillert 2048b39c5158Smillert =item B<-help> 2049b39c5158Smillert 2050b39c5158Smillert Print a brief help message and exits. 2051b39c5158Smillert 2052b39c5158Smillert =item B<-man> 2053b39c5158Smillert 2054b39c5158Smillert Prints the manual page and exits. 2055b39c5158Smillert 2056b39c5158Smillert =back 2057b39c5158Smillert 2058b39c5158Smillert =head1 DESCRIPTION 2059b39c5158Smillert 2060b39c5158Smillert B<This program> will read the given input file(s) and do something 2061b39c5158Smillert useful with the contents thereof. 2062b39c5158Smillert 2063b39c5158Smillert =cut 2064b39c5158Smillert 2065b39c5158SmillertSee L<Pod::Usage> for details. 2066b39c5158Smillert 2067b39c5158Smillert=head2 Parsing options from an arbitrary array 2068b39c5158Smillert 2069b39c5158SmillertBy default, GetOptions parses the options that are present in the 2070b39c5158Smillertglobal array C<@ARGV>. A special entry C<GetOptionsFromArray> can be 2071b39c5158Smillertused to parse options from an arbitrary array. 2072b39c5158Smillert 2073b39c5158Smillert use Getopt::Long qw(GetOptionsFromArray); 2074b39c5158Smillert $ret = GetOptionsFromArray(\@myopts, ...); 2075b39c5158Smillert 2076e9ce3842Safresh1When used like this, options and their possible values are removed 2077e9ce3842Safresh1from C<@myopts>, the global C<@ARGV> is not touched at all. 2078b39c5158Smillert 2079b39c5158SmillertThe following two calls behave identically: 2080b39c5158Smillert 2081b39c5158Smillert $ret = GetOptions( ... ); 2082b39c5158Smillert $ret = GetOptionsFromArray(\@ARGV, ... ); 2083b39c5158Smillert 2084e9ce3842Safresh1This also means that a first argument hash reference now becomes the 2085e9ce3842Safresh1second argument: 2086e9ce3842Safresh1 2087e9ce3842Safresh1 $ret = GetOptions(\%opts, ... ); 2088e9ce3842Safresh1 $ret = GetOptionsFromArray(\@ARGV, \%opts, ... ); 2089e9ce3842Safresh1 2090b39c5158Smillert=head2 Parsing options from an arbitrary string 2091b39c5158Smillert 2092b39c5158SmillertA special entry C<GetOptionsFromString> can be used to parse options 2093b39c5158Smillertfrom an arbitrary string. 2094b39c5158Smillert 2095b39c5158Smillert use Getopt::Long qw(GetOptionsFromString); 2096b39c5158Smillert $ret = GetOptionsFromString($string, ...); 2097b39c5158Smillert 2098b39c5158SmillertThe contents of the string are split into arguments using a call to 2099b39c5158SmillertC<Text::ParseWords::shellwords>. As with C<GetOptionsFromArray>, the 2100b39c5158Smillertglobal C<@ARGV> is not touched. 2101b39c5158Smillert 2102b39c5158SmillertIt is possible that, upon completion, not all arguments in the string 2103b39c5158Smillerthave been processed. C<GetOptionsFromString> will, when called in list 2104b39c5158Smillertcontext, return both the return status and an array reference to any 2105b39c5158Smillertremaining arguments: 2106b39c5158Smillert 2107b39c5158Smillert ($ret, $args) = GetOptionsFromString($string, ... ); 2108b39c5158Smillert 2109b39c5158SmillertIf any arguments remain, and C<GetOptionsFromString> was not called in 2110b39c5158Smillertlist context, a message will be given and C<GetOptionsFromString> will 2111b39c5158Smillertreturn failure. 2112b39c5158Smillert 2113e9ce3842Safresh1As with GetOptionsFromArray, a first argument hash reference now 2114eac174f2Safresh1becomes the second argument. See the next section. 2115e9ce3842Safresh1 2116b39c5158Smillert=head2 Storing options values in a hash 2117b39c5158Smillert 2118b39c5158SmillertSometimes, for example when there are a lot of options, having a 2119b39c5158Smillertseparate variable for each of them can be cumbersome. GetOptions() 2120b39c5158Smillertsupports, as an alternative mechanism, storing options values in a 2121b39c5158Smillerthash. 2122b39c5158Smillert 2123b39c5158SmillertTo obtain this, a reference to a hash must be passed I<as the first 2124b39c5158Smillertargument> to GetOptions(). For each option that is specified on the 2125b39c5158Smillertcommand line, the option value will be stored in the hash with the 2126b39c5158Smillertoption name as key. Options that are not actually used on the command 2127b39c5158Smillertline will not be put in the hash, on other words, 2128b39c5158SmillertC<exists($h{option})> (or defined()) can be used to test if an option 2129b39c5158Smillertwas used. The drawback is that warnings will be issued if the program 2130b39c5158Smillertruns under C<use strict> and uses C<$h{option}> without testing with 2131b39c5158Smillertexists() or defined() first. 2132b39c5158Smillert 2133b39c5158Smillert my %h = (); 2134b39c5158Smillert GetOptions (\%h, 'length=i'); # will store in $h{length} 2135b39c5158Smillert 2136b39c5158SmillertFor options that take list or hash values, it is necessary to indicate 2137b39c5158Smillertthis by appending an C<@> or C<%> sign after the type: 2138b39c5158Smillert 2139b39c5158Smillert GetOptions (\%h, 'colours=s@'); # will push to @{$h{colours}} 2140b39c5158Smillert 2141b39c5158SmillertTo make things more complicated, the hash may contain references to 2142b39c5158Smillertthe actual destinations, for example: 2143b39c5158Smillert 2144b39c5158Smillert my $len = 0; 2145b39c5158Smillert my %h = ('length' => \$len); 2146b39c5158Smillert GetOptions (\%h, 'length=i'); # will store in $len 2147b39c5158Smillert 2148b39c5158SmillertThis example is fully equivalent with: 2149b39c5158Smillert 2150b39c5158Smillert my $len = 0; 2151b39c5158Smillert GetOptions ('length=i' => \$len); # will store in $len 2152b39c5158Smillert 2153b39c5158SmillertAny mixture is possible. For example, the most frequently used options 2154b39c5158Smillertcould be stored in variables while all other options get stored in the 2155b39c5158Smillerthash: 2156b39c5158Smillert 2157b39c5158Smillert my $verbose = 0; # frequently referred 2158b39c5158Smillert my $debug = 0; # frequently referred 2159b39c5158Smillert my %h = ('verbose' => \$verbose, 'debug' => \$debug); 2160b39c5158Smillert GetOptions (\%h, 'verbose', 'debug', 'filter', 'size=i'); 2161b39c5158Smillert if ( $verbose ) { ... } 2162b39c5158Smillert if ( exists $h{filter} ) { ... option 'filter' was specified ... } 2163b39c5158Smillert 2164b39c5158Smillert=head2 Bundling 2165b39c5158Smillert 2166b39c5158SmillertWith bundling it is possible to set several single-character options 2167b39c5158Smillertat once. For example if C<a>, C<v> and C<x> are all valid options, 2168b39c5158Smillert 2169b39c5158Smillert -vax 2170b39c5158Smillert 2171b8851fccSafresh1will set all three. 2172b39c5158Smillert 2173b8851fccSafresh1Getopt::Long supports three styles of bundling. To enable bundling, a 2174b39c5158Smillertcall to Getopt::Long::Configure is required. 2175b39c5158Smillert 2176b8851fccSafresh1The simplest style of bundling can be enabled with: 2177b39c5158Smillert 2178b39c5158Smillert Getopt::Long::Configure ("bundling"); 2179b39c5158Smillert 2180b39c5158SmillertConfigured this way, single-character options can be bundled but long 2181eac174f2Safresh1options (and any of their auto-abbreviated shortened forms) B<must> 2182eac174f2Safresh1always start with a double dash C<--> to avoid ambiguity. For example, 2183eac174f2Safresh1when C<vax>, C<a>, C<v> and C<x> are all valid options, 2184b39c5158Smillert 2185b39c5158Smillert -vax 2186b39c5158Smillert 2187b8851fccSafresh1will set C<a>, C<v> and C<x>, but 2188b39c5158Smillert 2189b39c5158Smillert --vax 2190b39c5158Smillert 2191b8851fccSafresh1will set C<vax>. 2192b39c5158Smillert 2193b8851fccSafresh1The second style of bundling lifts this restriction. It can be enabled 2194b39c5158Smillertwith: 2195b39c5158Smillert 2196b39c5158Smillert Getopt::Long::Configure ("bundling_override"); 2197b39c5158Smillert 2198b8851fccSafresh1Now, C<-vax> will set the option C<vax>. 2199b39c5158Smillert 2200b8851fccSafresh1In all of the above cases, option values may be inserted in the 2201b8851fccSafresh1bundle. For example: 2202b39c5158Smillert 2203b39c5158Smillert -h24w80 2204b39c5158Smillert 2205b39c5158Smillertis equivalent to 2206b39c5158Smillert 2207b39c5158Smillert -h 24 -w 80 2208b39c5158Smillert 2209b8851fccSafresh1A third style of bundling allows only values to be bundled with 2210b8851fccSafresh1options. It can be enabled with: 2211b8851fccSafresh1 2212b8851fccSafresh1 Getopt::Long::Configure ("bundling_values"); 2213b8851fccSafresh1 2214b8851fccSafresh1Now, C<-h24> will set the option C<h> to C<24>, but option bundles 2215b8851fccSafresh1like C<-vxa> and C<-h24w80> are flagged as errors. 2216b8851fccSafresh1 2217b8851fccSafresh1Enabling C<bundling_values> will disable the other two styles of 2218b8851fccSafresh1bundling. 2219b8851fccSafresh1 2220b39c5158SmillertWhen configured for bundling, single-character options are matched 2221b39c5158Smillertcase sensitive while long options are matched case insensitive. To 2222b39c5158Smillerthave the single-character options matched case insensitive as well, 2223b39c5158Smillertuse: 2224b39c5158Smillert 2225b39c5158Smillert Getopt::Long::Configure ("bundling", "ignorecase_always"); 2226b39c5158Smillert 2227b39c5158SmillertIt goes without saying that bundling can be quite confusing. 2228b39c5158Smillert 2229b39c5158Smillert=head2 The lonesome dash 2230b39c5158Smillert 2231b39c5158SmillertNormally, a lone dash C<-> on the command line will not be considered 2232b39c5158Smillertan option. Option processing will terminate (unless "permute" is 2233b39c5158Smillertconfigured) and the dash will be left in C<@ARGV>. 2234b39c5158Smillert 2235b39c5158SmillertIt is possible to get special treatment for a lone dash. This can be 2236b39c5158Smillertachieved by adding an option specification with an empty name, for 2237b39c5158Smillertexample: 2238b39c5158Smillert 2239b39c5158Smillert GetOptions ('' => \$stdio); 2240b39c5158Smillert 2241b39c5158SmillertA lone dash on the command line will now be a legal option, and using 2242b39c5158Smillertit will set variable C<$stdio>. 2243b39c5158Smillert 2244b39c5158Smillert=head2 Argument callback 2245b39c5158Smillert 2246b39c5158SmillertA special option 'name' C<< <> >> can be used to designate a subroutine 2247b39c5158Smillertto handle non-option arguments. When GetOptions() encounters an 2248b39c5158Smillertargument that does not look like an option, it will immediately call this 224956d68f1eSafresh1subroutine and passes it one parameter: the argument name. 2250b39c5158Smillert 2251b39c5158SmillertFor example: 2252b39c5158Smillert 2253b39c5158Smillert my $width = 80; 2254b39c5158Smillert sub process { ... } 2255b39c5158Smillert GetOptions ('width=i' => \$width, '<>' => \&process); 2256b39c5158Smillert 2257b39c5158SmillertWhen applied to the following command line: 2258b39c5158Smillert 2259b39c5158Smillert arg1 --width=72 arg2 --width=60 arg3 2260b39c5158Smillert 2261b39c5158SmillertThis will call 2262b39c5158SmillertC<process("arg1")> while C<$width> is C<80>, 2263b39c5158SmillertC<process("arg2")> while C<$width> is C<72>, and 2264b39c5158SmillertC<process("arg3")> while C<$width> is C<60>. 2265b39c5158Smillert 2266b39c5158SmillertThis feature requires configuration option B<permute>, see section 2267b39c5158SmillertL<Configuring Getopt::Long>. 2268b39c5158Smillert 2269b39c5158Smillert=head1 Configuring Getopt::Long 2270b39c5158Smillert 2271b39c5158SmillertGetopt::Long can be configured by calling subroutine 2272b39c5158SmillertGetopt::Long::Configure(). This subroutine takes a list of quoted 2273b39c5158Smillertstrings, each specifying a configuration option to be enabled, e.g. 2274eac174f2Safresh1C<ignore_case>. To disable, prefix with C<no> or C<no_>, e.g. 2275eac174f2Safresh1C<no_ignore_case>. Case does not matter. Multiple calls to Configure() 2276eac174f2Safresh1are possible. 2277b39c5158Smillert 2278b39c5158SmillertAlternatively, as of version 2.24, the configuration options may be 2279b39c5158Smillertpassed together with the C<use> statement: 2280b39c5158Smillert 2281b39c5158Smillert use Getopt::Long qw(:config no_ignore_case bundling); 2282b39c5158Smillert 2283b39c5158SmillertThe following options are available: 2284b39c5158Smillert 2285b39c5158Smillert=over 12 2286b39c5158Smillert 2287b39c5158Smillert=item default 2288b39c5158Smillert 2289b39c5158SmillertThis option causes all configuration options to be reset to their 2290b39c5158Smillertdefault values. 2291b39c5158Smillert 2292b39c5158Smillert=item posix_default 2293b39c5158Smillert 2294b39c5158SmillertThis option causes all configuration options to be reset to their 2295b39c5158Smillertdefault values as if the environment variable POSIXLY_CORRECT had 2296b39c5158Smillertbeen set. 2297b39c5158Smillert 2298b39c5158Smillert=item auto_abbrev 2299b39c5158Smillert 2300b39c5158SmillertAllow option names to be abbreviated to uniqueness. 2301b39c5158SmillertDefault is enabled unless environment variable 2302b39c5158SmillertPOSIXLY_CORRECT has been set, in which case C<auto_abbrev> is disabled. 2303b39c5158Smillert 2304b39c5158Smillert=item getopt_compat 2305b39c5158Smillert 2306b39c5158SmillertAllow C<+> to start options. 2307b39c5158SmillertDefault is enabled unless environment variable 2308b39c5158SmillertPOSIXLY_CORRECT has been set, in which case C<getopt_compat> is disabled. 2309b39c5158Smillert 2310b39c5158Smillert=item gnu_compat 2311b39c5158Smillert 2312b39c5158SmillertC<gnu_compat> controls whether C<--opt=> is allowed, and what it should 2313b39c5158Smillertdo. Without C<gnu_compat>, C<--opt=> gives an error. With C<gnu_compat>, 2314*3d61058aSafresh1C<--opt=> will give option C<opt> an empty value. 2315b39c5158SmillertThis is the way GNU getopt_long() does it. 2316b39c5158Smillert 2317*3d61058aSafresh1Note that for options with optional arguments, C<--opt value> is still 2318*3d61058aSafresh1accepted, even though GNU getopt_long() requires writing C<--opt=value> 2319*3d61058aSafresh1in this case. 23209f11ffb7Safresh1 2321b39c5158Smillert=item gnu_getopt 2322b39c5158Smillert 2323b39c5158SmillertThis is a short way of setting C<gnu_compat> C<bundling> C<permute> 2324b39c5158SmillertC<no_getopt_compat>. With C<gnu_getopt>, command line handling should be 23259f11ffb7Safresh1reasonably compatible with GNU getopt_long(). 2326b39c5158Smillert 2327b39c5158Smillert=item require_order 2328b39c5158Smillert 2329b39c5158SmillertWhether command line arguments are allowed to be mixed with options. 2330b39c5158SmillertDefault is disabled unless environment variable 2331b39c5158SmillertPOSIXLY_CORRECT has been set, in which case C<require_order> is enabled. 2332b39c5158Smillert 2333b39c5158SmillertSee also C<permute>, which is the opposite of C<require_order>. 2334b39c5158Smillert 2335b39c5158Smillert=item permute 2336b39c5158Smillert 2337b39c5158SmillertWhether command line arguments are allowed to be mixed with options. 2338b39c5158SmillertDefault is enabled unless environment variable 2339b39c5158SmillertPOSIXLY_CORRECT has been set, in which case C<permute> is disabled. 2340b39c5158SmillertNote that C<permute> is the opposite of C<require_order>. 2341b39c5158Smillert 2342b39c5158SmillertIf C<permute> is enabled, this means that 2343b39c5158Smillert 2344b39c5158Smillert --foo arg1 --bar arg2 arg3 2345b39c5158Smillert 2346b39c5158Smillertis equivalent to 2347b39c5158Smillert 2348b39c5158Smillert --foo --bar arg1 arg2 arg3 2349b39c5158Smillert 2350b39c5158SmillertIf an argument callback routine is specified, C<@ARGV> will always be 2351b39c5158Smillertempty upon successful return of GetOptions() since all options have been 2352b39c5158Smillertprocessed. The only exception is when C<--> is used: 2353b39c5158Smillert 2354b39c5158Smillert --foo arg1 --bar arg2 -- arg3 2355b39c5158Smillert 2356b39c5158SmillertThis will call the callback routine for arg1 and arg2, and then 2357b39c5158Smillertterminate GetOptions() leaving C<"arg3"> in C<@ARGV>. 2358b39c5158Smillert 2359b39c5158SmillertIf C<require_order> is enabled, options processing 2360b39c5158Smillertterminates when the first non-option is encountered. 2361b39c5158Smillert 2362b39c5158Smillert --foo arg1 --bar arg2 arg3 2363b39c5158Smillert 2364b39c5158Smillertis equivalent to 2365b39c5158Smillert 2366b39c5158Smillert --foo -- arg1 --bar arg2 arg3 2367b39c5158Smillert 2368b39c5158SmillertIf C<pass_through> is also enabled, options processing will terminate 2369b39c5158Smillertat the first unrecognized option, or non-option, whichever comes 2370b39c5158Smillertfirst. 2371b39c5158Smillert 2372b39c5158Smillert=item bundling (default: disabled) 2373b39c5158Smillert 2374b39c5158SmillertEnabling this option will allow single-character options to be 2375b39c5158Smillertbundled. To distinguish bundles from long option names, long options 2376eac174f2Safresh1(and any of their auto-abbreviated shortened forms) I<must> be 2377eac174f2Safresh1introduced with C<--> and bundles with C<->. 2378b39c5158Smillert 2379b39c5158SmillertNote that, if you have options C<a>, C<l> and C<all>, and 2380b39c5158Smillertauto_abbrev enabled, possible arguments and option settings are: 2381b39c5158Smillert 2382b39c5158Smillert using argument sets option(s) 2383b39c5158Smillert ------------------------------------------ 2384b39c5158Smillert -a, --a a 2385b39c5158Smillert -l, --l l 2386b39c5158Smillert -al, -la, -ala, -all,... a, l 2387b39c5158Smillert --al, --all all 2388b39c5158Smillert 2389b39c5158SmillertThe surprising part is that C<--a> sets option C<a> (due to auto 2390b39c5158Smillertcompletion), not C<all>. 2391b39c5158Smillert 2392b39c5158SmillertNote: disabling C<bundling> also disables C<bundling_override>. 2393b39c5158Smillert 2394b39c5158Smillert=item bundling_override (default: disabled) 2395b39c5158Smillert 2396b39c5158SmillertIf C<bundling_override> is enabled, bundling is enabled as with 2397b39c5158SmillertC<bundling> but now long option names override option bundles. 2398b39c5158Smillert 2399b39c5158SmillertNote: disabling C<bundling_override> also disables C<bundling>. 2400b39c5158Smillert 2401b39c5158SmillertB<Note:> Using option bundling can easily lead to unexpected results, 2402b39c5158Smillertespecially when mixing long options and bundles. Caveat emptor. 2403b39c5158Smillert 2404b39c5158Smillert=item ignore_case (default: enabled) 2405b39c5158Smillert 2406e9ce3842Safresh1If enabled, case is ignored when matching option names. If, however, 2407e9ce3842Safresh1bundling is enabled as well, single character options will be treated 2408e9ce3842Safresh1case-sensitive. 2409b39c5158Smillert 2410b39c5158SmillertWith C<ignore_case>, option specifications for options that only 2411b39c5158Smillertdiffer in case, e.g., C<"foo"> and C<"Foo">, will be flagged as 2412b39c5158Smillertduplicates. 2413b39c5158Smillert 2414b39c5158SmillertNote: disabling C<ignore_case> also disables C<ignore_case_always>. 2415b39c5158Smillert 2416b39c5158Smillert=item ignore_case_always (default: disabled) 2417b39c5158Smillert 2418b39c5158SmillertWhen bundling is in effect, case is ignored on single-character 2419b39c5158Smillertoptions also. 2420b39c5158Smillert 2421b39c5158SmillertNote: disabling C<ignore_case_always> also disables C<ignore_case>. 2422b39c5158Smillert 2423b39c5158Smillert=item auto_version (default:disabled) 2424b39c5158Smillert 2425b39c5158SmillertAutomatically provide support for the B<--version> option if 2426b39c5158Smillertthe application did not specify a handler for this option itself. 2427b39c5158Smillert 2428b39c5158SmillertGetopt::Long will provide a standard version message that includes the 2429b39c5158Smillertprogram name, its version (if $main::VERSION is defined), and the 2430b39c5158Smillertversions of Getopt::Long and Perl. The message will be written to 2431b39c5158Smillertstandard output and processing will terminate. 2432b39c5158Smillert 2433b39c5158SmillertC<auto_version> will be enabled if the calling program explicitly 2434b39c5158Smillertspecified a version number higher than 2.32 in the C<use> or 2435b39c5158SmillertC<require> statement. 2436b39c5158Smillert 2437b39c5158Smillert=item auto_help (default:disabled) 2438b39c5158Smillert 2439b39c5158SmillertAutomatically provide support for the B<--help> and B<-?> options if 2440b39c5158Smillertthe application did not specify a handler for this option itself. 2441b39c5158Smillert 2442b39c5158SmillertGetopt::Long will provide a help message using module L<Pod::Usage>. The 2443b39c5158Smillertmessage, derived from the SYNOPSIS POD section, will be written to 2444b39c5158Smillertstandard output and processing will terminate. 2445b39c5158Smillert 2446b39c5158SmillertC<auto_help> will be enabled if the calling program explicitly 2447b39c5158Smillertspecified a version number higher than 2.32 in the C<use> or 2448b39c5158SmillertC<require> statement. 2449b39c5158Smillert 2450b39c5158Smillert=item pass_through (default: disabled) 2451b39c5158Smillert 2452b8851fccSafresh1With C<pass_through> anything that is unknown, ambiguous or supplied with 2453b8851fccSafresh1an invalid option will not be flagged as an error. Instead the unknown 2454b8851fccSafresh1option(s) will be passed to the catchall C<< <> >> if present, otherwise 2455b8851fccSafresh1through to C<@ARGV>. This makes it possible to write wrapper scripts that 2456b8851fccSafresh1process only part of the user supplied command line arguments, and pass the 2457b39c5158Smillertremaining options to some other program. 2458b39c5158Smillert 2459b8851fccSafresh1If C<require_order> is enabled, options processing will terminate at the 2460b8851fccSafresh1first unrecognized option, or non-option, whichever comes first and all 2461b8851fccSafresh1remaining arguments are passed to C<@ARGV> instead of the catchall 2462b8851fccSafresh1C<< <> >> if present. However, if C<permute> is enabled instead, results 2463b8851fccSafresh1can become confusing. 2464b39c5158Smillert 2465b39c5158SmillertNote that the options terminator (default C<-->), if present, will 2466b39c5158Smillertalso be passed through in C<@ARGV>. 2467b39c5158Smillert 2468b39c5158Smillert=item prefix 2469b39c5158Smillert 2470b39c5158SmillertThe string that starts options. If a constant string is not 2471b39c5158Smillertsufficient, see C<prefix_pattern>. 2472b39c5158Smillert 2473b39c5158Smillert=item prefix_pattern 2474b39c5158Smillert 2475b39c5158SmillertA Perl pattern that identifies the strings that introduce options. 2476b39c5158SmillertDefault is C<--|-|\+> unless environment variable 2477b39c5158SmillertPOSIXLY_CORRECT has been set, in which case it is C<--|->. 2478b39c5158Smillert 2479b39c5158Smillert=item long_prefix_pattern 2480b39c5158Smillert 2481b39c5158SmillertA Perl pattern that allows the disambiguation of long and short 2482b39c5158Smillertprefixes. Default is C<-->. 2483b39c5158Smillert 2484b39c5158SmillertTypically you only need to set this if you are using nonstandard 2485b39c5158Smillertprefixes and want some or all of them to have the same semantics as 2486b39c5158Smillert'--' does under normal circumstances. 2487b39c5158Smillert 2488b39c5158SmillertFor example, setting prefix_pattern to C<--|-|\+|\/> and 2489b39c5158Smillertlong_prefix_pattern to C<--|\/> would add Win32 style argument 2490b39c5158Smillerthandling. 2491b39c5158Smillert 2492b39c5158Smillert=item debug (default: disabled) 2493b39c5158Smillert 2494b39c5158SmillertEnable debugging output. 2495b39c5158Smillert 2496b39c5158Smillert=back 2497b39c5158Smillert 2498b39c5158Smillert=head1 Exportable Methods 2499b39c5158Smillert 2500b39c5158Smillert=over 2501b39c5158Smillert 2502b39c5158Smillert=item VersionMessage 2503b39c5158Smillert 2504b39c5158SmillertThis subroutine provides a standard version message. Its argument can be: 2505b39c5158Smillert 2506b39c5158Smillert=over 4 2507b39c5158Smillert 2508b39c5158Smillert=item * 2509b39c5158Smillert 2510b39c5158SmillertA string containing the text of a message to print I<before> printing 2511b39c5158Smillertthe standard message. 2512b39c5158Smillert 2513b39c5158Smillert=item * 2514b39c5158Smillert 2515b39c5158SmillertA numeric value corresponding to the desired exit status. 2516b39c5158Smillert 2517b39c5158Smillert=item * 2518b39c5158Smillert 2519b39c5158SmillertA reference to a hash. 2520b39c5158Smillert 2521b39c5158Smillert=back 2522b39c5158Smillert 2523b39c5158SmillertIf more than one argument is given then the entire argument list is 2524b39c5158Smillertassumed to be a hash. If a hash is supplied (either as a reference or 2525b39c5158Smillertas a list) it should contain one or more elements with the following 2526b39c5158Smillertkeys: 2527b39c5158Smillert 2528b39c5158Smillert=over 4 2529b39c5158Smillert 2530b39c5158Smillert=item C<-message> 2531b39c5158Smillert 2532b39c5158Smillert=item C<-msg> 2533b39c5158Smillert 2534b39c5158SmillertThe text of a message to print immediately prior to printing the 2535b39c5158Smillertprogram's usage message. 2536b39c5158Smillert 2537b39c5158Smillert=item C<-exitval> 2538b39c5158Smillert 2539b39c5158SmillertThe desired exit status to pass to the B<exit()> function. 2540b39c5158SmillertThis should be an integer, or else the string "NOEXIT" to 2541b39c5158Smillertindicate that control should simply be returned without 2542b39c5158Smillertterminating the invoking process. 2543b39c5158Smillert 2544b39c5158Smillert=item C<-output> 2545b39c5158Smillert 2546b39c5158SmillertA reference to a filehandle, or the pathname of a file to which the 2547b39c5158Smillertusage message should be written. The default is C<\*STDERR> unless the 2548b39c5158Smillertexit value is less than 2 (in which case the default is C<\*STDOUT>). 2549b39c5158Smillert 2550b39c5158Smillert=back 2551b39c5158Smillert 2552b39c5158SmillertYou cannot tie this routine directly to an option, e.g.: 2553b39c5158Smillert 2554b39c5158Smillert GetOptions("version" => \&VersionMessage); 2555b39c5158Smillert 2556b39c5158SmillertUse this instead: 2557b39c5158Smillert 2558b39c5158Smillert GetOptions("version" => sub { VersionMessage() }); 2559b39c5158Smillert 2560b39c5158Smillert=item HelpMessage 2561b39c5158Smillert 2562b39c5158SmillertThis subroutine produces a standard help message, derived from the 2563b39c5158Smillertprogram's POD section SYNOPSIS using L<Pod::Usage>. It takes the same 2564b39c5158Smillertarguments as VersionMessage(). In particular, you cannot tie it 2565b39c5158Smillertdirectly to an option, e.g.: 2566b39c5158Smillert 2567b39c5158Smillert GetOptions("help" => \&HelpMessage); 2568b39c5158Smillert 2569b39c5158SmillertUse this instead: 2570b39c5158Smillert 2571b39c5158Smillert GetOptions("help" => sub { HelpMessage() }); 2572b39c5158Smillert 2573b39c5158Smillert=back 2574b39c5158Smillert 2575b39c5158Smillert=head1 Return values and Errors 2576b39c5158Smillert 2577b39c5158SmillertConfiguration errors and errors in the option definitions are 2578b39c5158Smillertsignalled using die() and will terminate the calling program unless 2579b39c5158Smillertthe call to Getopt::Long::GetOptions() was embedded in C<eval { ... 2580b39c5158Smillert}>, or die() was trapped using C<$SIG{__DIE__}>. 2581b39c5158Smillert 2582b39c5158SmillertGetOptions returns true to indicate success. 2583b39c5158SmillertIt returns false when the function detected one or more errors during 2584b39c5158Smillertoption parsing. These errors are signalled using warn() and can be 2585b39c5158Smillerttrapped with C<$SIG{__WARN__}>. 2586b39c5158Smillert 2587b39c5158Smillert=head1 Legacy 2588b39c5158Smillert 2589b39c5158SmillertThe earliest development of C<newgetopt.pl> started in 1990, with Perl 2590b39c5158Smillertversion 4. As a result, its development, and the development of 2591b39c5158SmillertGetopt::Long, has gone through several stages. Since backward 2592b39c5158Smillertcompatibility has always been extremely important, the current version 2593b39c5158Smillertof Getopt::Long still supports a lot of constructs that nowadays are 2594b39c5158Smillertno longer necessary or otherwise unwanted. This section describes 2595b39c5158Smillertbriefly some of these 'features'. 2596b39c5158Smillert 2597b39c5158Smillert=head2 Default destinations 2598b39c5158Smillert 2599b39c5158SmillertWhen no destination is specified for an option, GetOptions will store 2600b39c5158Smillertthe resultant value in a global variable named C<opt_>I<XXX>, where 2601e5157e49Safresh1I<XXX> is the primary name of this option. When a program executes 2602b39c5158Smillertunder C<use strict> (recommended), these variables must be 2603*3d61058aSafresh1pre-declared with our(). 2604b39c5158Smillert 2605b39c5158Smillert our $opt_length = 0; 2606b39c5158Smillert GetOptions ('length=i'); # will store in $opt_length 2607b39c5158Smillert 2608b39c5158SmillertTo yield a usable Perl variable, characters that are not part of the 2609b39c5158Smillertsyntax for variables are translated to underscores. For example, 2610b39c5158SmillertC<--fpp-struct-return> will set the variable 2611b39c5158SmillertC<$opt_fpp_struct_return>. Note that this variable resides in the 2612b39c5158Smillertnamespace of the calling program, not necessarily C<main>. For 2613b39c5158Smillertexample: 2614b39c5158Smillert 2615b39c5158Smillert GetOptions ("size=i", "sizes=i@"); 2616b39c5158Smillert 2617b39c5158Smillertwith command line "-size 10 -sizes 24 -sizes 48" will perform the 2618b39c5158Smillertequivalent of the assignments 2619b39c5158Smillert 2620b39c5158Smillert $opt_size = 10; 2621b39c5158Smillert @opt_sizes = (24, 48); 2622b39c5158Smillert 2623b39c5158Smillert=head2 Alternative option starters 2624b39c5158Smillert 2625b39c5158SmillertA string of alternative option starter characters may be passed as the 2626b39c5158Smillertfirst argument (or the first argument after a leading hash reference 2627b39c5158Smillertargument). 2628b39c5158Smillert 2629b39c5158Smillert my $len = 0; 2630b39c5158Smillert GetOptions ('/', 'length=i' => $len); 2631b39c5158Smillert 2632b39c5158SmillertNow the command line may look like: 2633b39c5158Smillert 2634b39c5158Smillert /length 24 -- arg 2635b39c5158Smillert 2636b39c5158SmillertNote that to terminate options processing still requires a double dash 2637b39c5158SmillertC<-->. 2638b39c5158Smillert 2639b39c5158SmillertGetOptions() will not interpret a leading C<< "<>" >> as option starters 2640b39c5158Smillertif the next argument is a reference. To force C<< "<" >> and C<< ">" >> as 2641b39c5158Smillertoption starters, use C<< "><" >>. Confusing? Well, B<using a starter 2642b39c5158Smillertargument is strongly deprecated> anyway. 2643b39c5158Smillert 2644b39c5158Smillert=head2 Configuration variables 2645b39c5158Smillert 2646b39c5158SmillertPrevious versions of Getopt::Long used variables for the purpose of 2647b39c5158Smillertconfiguring. Although manipulating these variables still work, it is 2648b39c5158Smillertstrongly encouraged to use the C<Configure> routine that was introduced 2649b39c5158Smillertin version 2.17. Besides, it is much easier. 2650b39c5158Smillert 2651b39c5158Smillert=head1 Tips and Techniques 2652b39c5158Smillert 2653b39c5158Smillert=head2 Pushing multiple values in a hash option 2654b39c5158Smillert 2655b39c5158SmillertSometimes you want to combine the best of hashes and arrays. For 2656b39c5158Smillertexample, the command line: 2657b39c5158Smillert 2658b39c5158Smillert --list add=first --list add=second --list add=third 2659b39c5158Smillert 2660b39c5158Smillertwhere each successive 'list add' option will push the value of add 2661b39c5158Smillertinto array ref $list->{'add'}. The result would be like 2662b39c5158Smillert 2663b39c5158Smillert $list->{add} = [qw(first second third)]; 2664b39c5158Smillert 2665b39c5158SmillertThis can be accomplished with a destination routine: 2666b39c5158Smillert 2667b39c5158Smillert GetOptions('list=s%' => 2668b39c5158Smillert sub { push(@{$list{$_[1]}}, $_[2]) }); 2669b39c5158Smillert 2670b39c5158Smillert=head1 Troubleshooting 2671b39c5158Smillert 2672b39c5158Smillert=head2 GetOptions does not return a false result when an option is not supplied 2673b39c5158Smillert 2674b39c5158SmillertThat's why they're called 'options'. 2675b39c5158Smillert 2676b39c5158Smillert=head2 GetOptions does not split the command line correctly 2677b39c5158Smillert 2678b39c5158SmillertThe command line is not split by GetOptions, but by the command line 2679b39c5158Smillertinterpreter (CLI). On Unix, this is the shell. On Windows, it is 2680b39c5158SmillertCOMMAND.COM or CMD.EXE. Other operating systems have other CLIs. 2681b39c5158Smillert 2682b39c5158SmillertIt is important to know that these CLIs may behave different when the 2683b39c5158Smillertcommand line contains special characters, in particular quotes or 2684b39c5158Smillertbackslashes. For example, with Unix shells you can use single quotes 2685b39c5158Smillert(C<'>) and double quotes (C<">) to group words together. The following 2686b39c5158Smillertalternatives are equivalent on Unix: 2687b39c5158Smillert 2688b39c5158Smillert "two words" 2689b39c5158Smillert 'two words' 2690b39c5158Smillert two\ words 2691b39c5158Smillert 2692b39c5158SmillertIn case of doubt, insert the following statement in front of your Perl 2693b39c5158Smillertprogram: 2694b39c5158Smillert 2695b39c5158Smillert print STDERR (join("|",@ARGV),"\n"); 2696b39c5158Smillert 2697b39c5158Smillertto verify how your CLI passes the arguments to the program. 2698b39c5158Smillert 2699b39c5158Smillert=head2 Undefined subroutine &main::GetOptions called 2700b39c5158Smillert 2701b39c5158SmillertAre you running Windows, and did you write 2702b39c5158Smillert 2703b39c5158Smillert use GetOpt::Long; 2704b39c5158Smillert 2705b39c5158Smillert(note the capital 'O')? 2706b39c5158Smillert 2707b39c5158Smillert=head2 How do I put a "-?" option into a Getopt::Long? 2708b39c5158Smillert 2709b39c5158SmillertYou can only obtain this using an alias, and Getopt::Long of at least 2710b39c5158Smillertversion 2.13. 2711b39c5158Smillert 2712b39c5158Smillert use Getopt::Long; 2713b39c5158Smillert GetOptions ("help|?"); # -help and -? will both set $opt_help 2714b39c5158Smillert 271556d68f1eSafresh1Other characters that can't appear in Perl identifiers are also 271656d68f1eSafresh1supported in aliases with Getopt::Long of at version 2.39. Note that 271756d68f1eSafresh1the characters C<!>, C<|>, C<+>, C<=>, and C<:> can only appear as the 271856d68f1eSafresh1first (or only) character of an alias. 2719e9ce3842Safresh1 2720e9ce3842Safresh1As of version 2.32 Getopt::Long provides auto-help, a quick and easy way 2721e9ce3842Safresh1to add the options --help and -? to your program, and handle them. 2722e9ce3842Safresh1 2723e9ce3842Safresh1See C<auto_help> in section L<Configuring Getopt::Long>. 2724e9ce3842Safresh1 2725b39c5158Smillert=head1 AUTHOR 2726b39c5158Smillert 2727b39c5158SmillertJohan Vromans <jvromans@squirrel.nl> 2728b39c5158Smillert 2729b39c5158Smillert=head1 COPYRIGHT AND DISCLAIMER 2730b39c5158Smillert 2731*3d61058aSafresh1This program is Copyright 1990,2015,2023 by Johan Vromans. 2732b39c5158SmillertThis program is free software; you can redistribute it and/or 2733b39c5158Smillertmodify it under the terms of the Perl Artistic License or the 2734b39c5158SmillertGNU General Public License as published by the Free Software 2735b39c5158SmillertFoundation; either version 2 of the License, or (at your option) any 2736b39c5158Smillertlater version. 2737b39c5158Smillert 2738b39c5158SmillertThis program is distributed in the hope that it will be useful, 2739b39c5158Smillertbut WITHOUT ANY WARRANTY; without even the implied warranty of 2740b39c5158SmillertMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 2741b39c5158SmillertGNU General Public License for more details. 2742b39c5158Smillert 2743b39c5158SmillertIf you do not have a copy of the GNU General Public License write to 2744b39c5158Smillertthe Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 2745b39c5158SmillertMA 02139, USA. 2746b39c5158Smillert 2747b39c5158Smillert=cut 2748b39c5158Smillert 2749