/usr/share/perl5/Getopt/Tabular.pm is in libgetopt-tabular-perl 0.3-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 | package Getopt::Tabular;
#
# Getopt/Tabular.pm
#
# Perl module for table-driven argument parsing, somewhat like Tk's
# ParseArgv. To use the package, you just have to set up an argument table
# (a list of array references), and call &GetOptions (the name is exported
# from the module). &GetOptions takes two or three arguments; a reference
# to your argument table (which is not modified), a reference to the list
# of command line arguments, e.g. @ARGV (or a copy of it), and (optionally)
# a reference to a new empty array. In the two argument form, the second
# argument is modified in place to remove all options and their arguments.
# In the three argument form, the second argument is unmodified, and the
# third argument is set to a copy of it with options removed.
#
# The argument table consists of one element per valid command-line option;
# each element should be a reference to a list of the form:
#
# ( option_name, type, num_values, option_data, help_string, arg_desc )
#
# See Getopt/Tabular.pod for complete information.
#
# originally by Greg Ward 1995/07/06-07/09 as ParseArgs.pm
# renamed to Getopt::Tabular and somewhat reorganized/reworked,
# 1996/11/08-11/10
#
# $Id: Tabular.pm,v 1.8 1999/04/08 01:11:24 greg Exp $
# Copyright (c) 1995-98 Greg Ward. All rights reserved. This package is
# free software; you can redistribute it and/or modify it under the same
# terms as Perl itself.
require Exporter;
use Carp;
use strict;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
use vars qw/%Patterns %OptionHandlers %TypeDescriptions @OptionPatterns
%SpoofCode $OptionTerminator $HelpOption
$LongHelp $Usage $ErrorClass $ErrorMessage/;
$VERSION = 0.3;
@ISA = qw/Exporter/;
@EXPORT = qw/GetOptions/;
@EXPORT_OK = qw/SetHelp SetHelpOption SetError GetError SpoofGetOptions/;
# -------------------------------------------------------------------- #
# Private global variables #
# -------------------------------------------------------------------- #
# The regexp for floating point numbers here is a little more permissive
# than the C standard -- it recognizes "0", "0.", ".0", and "0.0" (where 0
# can be substituted by any string of one or more digits), preceded by an
# optional sign, and followed by an optional exponent.
%Patterns = ('integer' => '[+-]?\d+',
'float' => '[+-]? ( \d+(\.\d*)? | \.\d+ ) ([Ee][+-]?\d+)?',
'string' => '.*');
# This hash defines the allowable option types, and what to do when we
# see an argument of a given type in the argument list. New types
# can be added by calling AddType, as long as you supply an option
# handler that acts like one of the existing handlers. (Ie. takes
# the same three arguments, returns 1 for success and 0 for failure,
# and calls SetError appropriately.)
%OptionHandlers = ("string", \&process_pattern_option,
"integer", \&process_pattern_option,
"float", \&process_pattern_option,
"boolean", \&process_boolean_option,
"const", \&process_constant_option,
"copy", \&process_constant_option,
"arrayconst",\&process_constant_option,
"hashconst", \&process_constant_option,
"call", \&process_call_option,
"eval", \&process_eval_option,
"section", undef);
# This hash is used for building error messages for pattern types. A
# subtle point is that the description should be such that it can be
# pluralized by adding an "s". OK, OK, you can supply an alternate
# plural form by making the description a reference to a two-element list,
# singular and plural forms. I18N fanatics should be happy.
%TypeDescriptions = ("integer" => "integer",
"float" => "floating-point number",
"string" => "string");
@OptionPatterns = ('(-)(\w+)'); # two parts: "prefix" and "body"
$OptionTerminator = "--";
$HelpOption = "-help";
# The %SpoofCode hash is for storing alternate versions of callbacks
# for call or eval options. The alternate versions should have no side
# effects apart from changing the argument list identically to their
# "real" alternatives.
%SpoofCode = ();
$ErrorClass = ""; # can be "bad_option", "bad_value",
# "bad_eval", or "help"
$ErrorMessage = ""; # can be anything
# -------------------------------------------------------------------- #
# Public (but not exported) subroutines used to set options before #
# calling GetOptions. #
# -------------------------------------------------------------------- #
sub SetHelp
{
$LongHelp = shift;
$Usage = shift;
}
sub SetOptionPatterns
{
@OptionPatterns = @_;
}
sub SetHelpOption
{
$HelpOption = shift;
}
sub SetTerminator
{
$OptionTerminator = shift;
}
sub UnsetTerminator
{
undef $OptionTerminator;
}
sub AddType
{
my ($type, $handler) = @_;
croak "AddType: \$handler must be a code ref"
unless ref $handler eq 'CODE';
$OptionHandlers{$type} = $handler;
}
sub AddPatternType
{
my ($type, $pattern, $description) = @_;
$OptionHandlers{$type} = \&process_pattern_option;
$Patterns{$type} = $pattern;
$TypeDescriptions{$type} = ($description || $type);
}
sub GetPattern
{
my ($type) = @_;
$Patterns{$type};
}
sub SetSpoofCodes
{
my ($option, $code);
croak "Even number of arguments required"
unless (@_ > 0 && @_ % 2 == 0);
while (@_)
{
($option, $code) = (shift, shift);
$SpoofCode{$option} = $code;
}
}
sub SetError
{
$ErrorClass = shift;
$ErrorMessage = shift;
}
sub GetError
{
($ErrorClass, $ErrorMessage);
}
# --------------------------------------------------------------------
# Private utility subroutines:
# quote_strings
# print_help
# scan_table
# match_abbreviation
# option_error
# check_value
# split_option
# find_calling_package
# --------------------------------------------------------------------
#
# "e_strings
#
# prepares strings for printing in a list of default values (for the
# help text). If a string is empty or contains whitespace, it is quoted;
# otherwise, it is left alone. The input list of strings is returned
# concatenated into a single space-separated string. This is *not*
# rigorous by any stretch; it's just to make the help text look nice.
#
sub quote_strings
{
my @strings = @_;
my $string;
foreach $string (@strings)
{
$string = qq["$string"] if ($string eq '' || $string =~ /\s/);
}
return join (' ', @strings);
}
#
# &print_help
#
# walks through an argument table and prints out nicely-formatted
# option help for all entries that provide it. Also does the Right Thing
# (trust me) if you supply "argument description" text after the help.
#
# Don't read this code if you can possibly avoid it. It's pretty gross.
#
sub print_help
{
confess ("internal error, wrong number of input args to &print_help")
if (scalar (@_) != 1);
my ($argtable) = @_;
my ($maxoption, $maxargdesc, $numcols, $opt, $breakers);
my ($textlength, $std_format, $alt_format);
my ($option, $type, $num, $value, $help, $argdesc);
$maxoption = 0;
$maxargdesc = 0;
# Loop over all options to determine the length of the longest option name
foreach $opt (@$argtable)
{
my ($argdesclen, $neg_option);
my ($option, $type, $help, $argdesc) = @{$opt} [0,1,4,5];
next if $type eq "section" or ! defined $help;
# Boolean options contribute *two* lines to the help: one for the
# option, and one for its negative. Other options just contribute
# one line, so they're a bit simpler.
if ($type eq 'boolean')
{
my ($pos, $neg) = &split_option ($opt);
my $pos_len = length ($pos);
my $neg_len = length ($neg);
$maxoption = $pos_len if ($pos_len > $maxoption);
$maxoption = $neg_len if ($pos_len > $maxoption);
carp "Getopt::Tabular: argument descriptions ignored " .
"for boolean option \"$option\""
if defined $argdesc;
}
else
{
my $optlen = length ($option);
$maxoption = $optlen if ($optlen > $maxoption);
if (defined $argdesc)
{
$argdesclen = length ($argdesc);
$maxargdesc = $argdesclen if ($argdesclen > $maxargdesc);
}
}
}
# We need to construct and eval code that looks something like this:
# format STANDARD =
# @<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
# $option, $help
# ~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
# $help
# .
#
# with an alternative format like this:
# format ALTERNATIVE =
# @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
# $option, $argdesc
# ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
# $help
# ~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
# $help
# .
# in order to nicely print out the help. Can't hardcode a format,
# though, because we don't know until now how much space to allocate
# for the option (ie. $maxoption).
local $: = " \n";
local $~;
$numcols = 80; # not always accurate, but faster!
# width of text = width of terminal, with columns removed as follows:
# 3 (for left margin), $maxoption (option names), 2 (gutter between
# option names and help text), and 2 (right margin)
$textlength = $numcols - 3 - $maxoption - 2 - 2;
$std_format = "format STANDARD =\n" .
" @" . ("<" x $maxoption) . " ^" . ("<" x ($textlength-1)) . "\n".
"\$option, \$help\n" .
"~~ " . (" " x $maxoption) . " ^" . ("<" x ($textlength-1)) . "\n" .
"\$help\n.";
$alt_format = "format ALTERNATIVE =\n" .
" @" . ("<" x ($maxoption + $maxargdesc)) . "\n" .
"\$option\n" .
" " . (" " x $maxoption) . " ^" . ("<" x ($textlength-1)) . "\n" .
"\$help\n" .
"~~ " . (" " x $maxoption) . " ^" . ("<" x ($textlength-1)) . "\n" .
"\$help\n.";
eval $std_format;
confess ("internal error with format \"$std_format\": $@") if $@;
eval $alt_format;
confess ("internal error with format \"$alt_format\": $@") if $@;
my $show_defaults = 1;
print $LongHelp . "\n" if defined $LongHelp;
print "Summary of options:\n";
foreach $opt (@$argtable)
{
($option, $type, $num, $value, $help, $argdesc) = @$opt;
if ($type eq "section")
{
printf "\n-- %s %s\n", $option, "-" x ($numcols-4-length($option));
next;
}
next unless defined $help;
$argdesc = "" unless defined $argdesc;
my $show_default = $show_defaults && $help !~ /\[default/;
$~ = 'STANDARD';
if ($type eq 'boolean')
{
undef $option; # arg! why is this necessary?
my ($pos, $neg) = &split_option ($opt);
$option = $pos;
$help .= ' [default]'
if $show_default && defined $$value && $$value;
write;
$help = "opposite of $pos";
$help .= ' [default]'
if $show_default && defined $$value && ! $$value;
$option = $neg;
write;
}
else
{
# If the option type is of the argument-taking variety, then
# we'll try to help out by saying what the default value(s)
# is/are
if ($OptionHandlers{$type} == \&process_pattern_option)
{
if ($num == 1) # expectes a scalar value
{
$help .= ' [default: ' . quote_strings ($$value) . ']'
if ($show_default && defined $$value);
}
else # expects a vector value
{
$help .= ' [default: ' . quote_strings (@$value) . ']'
if ($show_default &&
@$value && ! grep (! defined $_, @$value));
}
}
if ($argdesc)
{
my $expanded_option = $option . " " . $argdesc if $argdesc;
$option = $expanded_option;
if (length ($expanded_option) > $maxoption+1)
{
$~ = 'ALTERNATIVE';
}
}
write;
}
}
print "\n";
print $Usage if defined $Usage;
}
#
# &scan_table
#
# walks through an argument table, building a hash that lets us quickly
# and painlessly look up an option.
#
sub scan_table
{
my ($argtable, $arghash) = @_;
my ($opt, $option, $type, $value);
my $i;
for $i (0 .. $#$argtable)
{
$opt = $argtable->[$i];
($option, $type, $value) = @$opt;
unless (exists $OptionHandlers{$type})
{
croak "Unknown option type \"$type\" supplied for option $option";
}
if ($type eq "boolean")
{
my ($pos,$neg) = &split_option($opt);
$arghash->{$pos} = $i;
$arghash->{$neg} = $i if defined $neg;
}
elsif ($type ne "section")
{
$arghash->{$option} = $i;
}
}
}
#
# &match_abbreviation
#
# Given a string $s and a list of words @$words, finds the word for which
# $s is a non-ambiguous abbreviation. If $s is found to be ambiguous or
# doesn't match, a clear and concise error message is printed, using
# $err_format as a format for sprintf. Suggested form for $err_format is
# "%s option: %s"; the first %s will be substituted with either "ambiguous"
# or "unknown" (depending on the problem), and the second will be
# substituted with $s. Thus, with this format, the error message will look
# something like "unknown option: -foo" or "ambiguous option: -f".
#
sub match_abbreviation
{
my ($s, $words, $err_format) = @_;
my ($match);
my $word;
foreach $word (@$words)
{
# If $s is a prefix of $word, it's at least an approximate match,
# so try to do better
next unless ($s eq substr ($word, 0, length ($s)));
# We have an exact match, so return it now
return $word if ($s eq $word);
# We have an approx. match, and already had one before
if ($match)
{
&SetError ("bad_option", sprintf ("$err_format", "ambiguous", $s));
return 0;
}
$match = $word;
}
&SetError ("bad_option", sprintf ("$err_format", "unknown", $s))
if !$match;
$match;
}
#
# &option_error
#
# Constructs a useful error message to deal with an option that expects
# a certain number of values of certain types, but a command-line that
# falls short of this mark. $option should be the option that triggers
# the situation; $type should be the expected type; $n should be the
# number of values expected.
#
# The error message (returned by the function) will look something like
# "-foo option must be followed by an integer" (yes, it does pick "a"
# or "an", depending on whether the description of the type starts
# with a vowel) or "-bar option must be followed by 3 strings".
#
# The error message is put in the global $ErrorMessage, as well as returned
# by the function. Also, the global $ErrorClass is set to "bad_value".
#
sub option_error
{
my ($option, $type, $n) = @_;
my ($typedesc, $singular, $plural, $article, $desc);
$typedesc = $TypeDescriptions{$type};
($singular,$plural) = (ref $typedesc eq 'ARRAY')
? @$typedesc
: ($typedesc, $typedesc . "s");
$article = ($typedesc =~ /^[aeiou]/) ? "an" : "a";
$desc = ($n > 1) ?
"$n $plural" :
"$article $singular";
&SetError ("bad_value", "$option option must be followed by $desc");
}
#
# &check_value
#
# Verifies that a value (presumably from the command line) satisfies
# the requirements for the expected type.
#
# Calls &option_error (to set $ErrorClass and $ErrorMessage globals) and returns
# 0 if the value isn't up to scratch.
#
sub check_value
{
my ($val, $option, $type, $n) = @_;
unless (defined $val && $val =~ /^$Patterns{$type}$/x)
{
&option_error ($option, $type, $n);
return 0;
}
}
#
# &split_option
#
# Splits a boolean option into positive and negative alternatives. The
# two alternatives are returned as a two-element array.
#
# Croaks if it can't figure out the alternatives, or if there appear to be
# more than 2 alternatives specified.
#
sub split_option
{
my ($opt_desc) = @_;
my ($option, @options);
$option = $opt_desc->[0];
return ($option) if $opt_desc->[1] ne "boolean";
@options = split ('\|', $option);
if (@options == 2)
{
return @options;
}
elsif (@options == 1)
{
my ($pattern, $prefix, $positive_alt, $negative_alt);
for $pattern (@OptionPatterns)
{
my ($prefix, $body);
if (($prefix, $body) = $option =~ /^$pattern$/)
{
$negative_alt = $prefix . "no" . $body;
return ($option, $negative_alt);
}
}
croak "Boolean option \"$option\" did not match " .
"any option prefixes - unable to guess negative alternative";
return ($option);
}
else
{
croak "Too many alternatives supplied for boolean option \"$option\"";
}
}
#
# &find_calling_package
#
# walks up the call stack until we find a caller in a different package
# from the current one. (Handy for `eval' options, when we want to
# eval a chunk of code in the package that called GetOptions.)
#
sub find_calling_package
{
my ($i, $this_pkg, $up_pkg, @caller);
$i = 0;
$this_pkg = (caller(0))[0];
while (@caller = caller($i++))
{
$up_pkg = $caller[0];
last if $up_pkg ne $this_pkg;
}
$up_pkg;
}
# ----------------------------------------------------------------------
# Option-handling routines:
# process_constant_option
# process_boolean_option
# process_call_option
# process_eval_option
# ----------------------------------------------------------------------
# General description of these routines:
# * each one is passed exactly four options:
# $arg - the argument that triggered this routine, expanded
# into unabbreviated form
# $arglist - reference to list containing rest of command line
# $opt_desc - reference to an option descriptor list
# $spoof - flag: if true, then no side effects
# * they are called from GetOptions, through code references in the
# %OptionHandlers hash
# * if they return a false value, then GetOptions immediately returns
# 0 to its caller, with no error message -- thus, the option handlers
# should print out enough of an error message for the end user to
# figure out what went wrong; also, the option handlers should be
# careful to explicitly return 1 if everything went well!
sub process_constant_option
{
my ($arg, $arglist, $opt_desc, $spoof) = @_;
my ($type, $n, $value) = @$opt_desc[1,2,3];
return 1 if $spoof;
if ($type eq "const")
{
$$value = $n;
}
elsif ($type eq "copy")
{
$$value = (defined $n) ? ($n) : ($arg);
}
elsif ($type eq "arrayconst")
{
@$value = @$n;
}
elsif ($type eq "hashconst")
{
%$value = %$n;
}
else
{
confess ("internal error: can't handle option type \"$type\"");
}
1;
}
sub process_boolean_option
{
my ($arg, $arglist, $opt_desc, $spoof) = @_;
my ($value) = $$opt_desc[3];
return 1 if $spoof;
my ($pos,$neg) = &split_option ($opt_desc);
confess ("internal error: option $arg not found in argument hash")
if ($arg ne $pos && $arg ne $neg);
$$value = ($arg eq $pos) ? 1 : 0;
1;
}
sub process_call_option
{
my ($arg, $arglist, $opt_desc, $spoof) = @_;
my ($option, $args, $value) = @$opt_desc[0,2,3];
croak "Invalid option table entry for option \"$option\" -- \"value\" " .
"field must be a code reference"
unless (ref $value eq 'CODE');
# This will crash 'n burn big time if there is no spoof code for
# this option -- but that's why we check %SpoofCode against the
# arg table from GetOptions!
$value = $SpoofCode{$arg} if ($spoof);
my @args = (ref $args eq 'ARRAY') ? (@$args) : ();
my $result = &$value ($arg, $arglist, @args);
if (!$result)
{
# Wouldn't it be neat if we could get the sub name from the code ref?
&SetError
($ErrorClass || "bad_call",
$ErrorMessage || "subroutine call from option \"$arg\" failed");
}
return $result;
} # &process_call_option
sub process_eval_option
{
my ($arg, $arglist, $opt_desc, $spoof) = @_;
my ($value) = $$opt_desc[3];
$value = $SpoofCode{$arg} if ($spoof);
my $up_pkg = &find_calling_package ();
# print "package $up_pkg; $value"; # DEBUG ONLY
my $result = eval "package $up_pkg; no strict; $value";
if ($@) # any error string set?
{
&SetError ("bad_eval",
"error evaluating \"$value\" (from $arg option): $@");
return 0;
}
if (!$result)
{
&SetError
($ErrorClass || "bad_call",
$ErrorMessage || "code eval'd for option \"$arg\" failed");
}
return $result;
}
sub process_pattern_option
{
my ($arg, $arglist, $opt_desc, $spoof) = @_;
my ($type, $n, $value) = @$opt_desc[1,2,3];
my ($dummy, @dummies);
# This code looks a little more complicated than you might at first
# think necessary. But the ugliness is necessary because $value might
# reference a scalar or an array, depending on whether $n is 1 (scalar)
# or not (array). Thus, we can't just assume that either @$value or
# $$value is valid -- we always have to check which of the two it should
# be.
if ($n == 1) # scalar-valued option (one argument)
{
croak "GetOptions: \"$arg\" option must be associated with a scalar ref"
unless ref $value eq 'SCALAR';
$value = \$dummy if $spoof;
$$value = shift @$arglist;
return 0 unless &check_value ($$value, $arg, $type, $n);
}
else # it's a "vector-valued" option
{ # (fixed number of arguments)
croak "GetOptions: \"$arg\" option must be associated with an array ref"
unless ref $value eq 'ARRAY';
$value = \@dummies if $spoof;
@$value = splice (@$arglist, 0, $n);
if (scalar @$value != $n)
{
&option_error ($arg, $type, $n);
return 0;
}
my $val;
foreach $val (@$value)
{
return 0 unless &check_value ($val, $arg, $type, $n);
}
} # else
return 1;
} # &process_pattern_option
# --------------------------------------------------------------------
# The main public subroutine: GetOptions
# --------------------------------------------------------------------
sub GetOptions
{
my ($opt_table, $arglist, $new_arglist, $spoof) = @_;
my (%argpos, $arg, $pos, $opt_ref);
my ($option_re, @option_list);
$new_arglist = $arglist if !defined $new_arglist;
&SetError ("", "");
# Build a hash mapping option -> position in option table
&scan_table ($opt_table, \%argpos);
# Regexp to let us recognize options on the command line
$option_re = join ("|", @OptionPatterns);
# Build a list of all acceptable options -- used to match abbreviations
my $opt_desc;
foreach $opt_desc (@$opt_table)
{
push (@option_list, &split_option ($opt_desc))
unless $opt_desc->[1] eq "section";
}
push (@option_list, $HelpOption) if $HelpOption;
# If in spoof mode: make sure we have spoof code for all call/eval options
if ($spoof)
{
my ($opt, $type, $spoof);
foreach $opt_desc (@$opt_table)
{
$opt = $opt_desc->[0];
$type = $opt_desc->[1];
$spoof = $SpoofCode{$opt};
next unless $type eq 'call' || $type eq 'eval';
croak "No alternate code supplied for option $opt in spoof mode"
unless defined $spoof;
croak "Alternate code must be a CODE ref for option $opt"
if ($type eq 'call' && ref $spoof ne 'CODE');
croak "Alternate code must be a string for option $opt"
if ($type eq 'eval' && ref $spoof);
}
}
# Now walk over the argument list
my @tmp_arglist = @$arglist;
@$new_arglist = ();
while (defined ($arg = shift @tmp_arglist))
{
# print "arg: $arg\n";
# If this argument is the option terminator (usually "--") then
# transfer all remaining arguments to the new arg list and stop
# processing immediately.
if (defined $OptionTerminator && $arg eq $OptionTerminator)
{
push (@$new_arglist, @tmp_arglist);
last;
}
# If this argument isn't an option at all, just append it to
# @$new_arglist and go to the next one.
if ($arg !~ /^($option_re)/o)
{
push (@$new_arglist, $arg);
next;
}
# We know we have something that looks like an option; see if it
# matches or is an abbreviation for one of the strings in
# @option_list
$arg = &match_abbreviation ($arg, \@option_list, "%s option: %s");
if (! $arg)
{
warn $Usage if defined $Usage;
warn "$ErrorMessage\n";
return 0;
}
# If it's the help option, print out the help and return
# (even if in spoof mode!)
if ($arg eq $HelpOption)
{
&print_help ($opt_table);
&SetError ("help", "");
return 0;
}
# Now we know it's a valid option, and it's not the help option --
# so it must be in the caller's option table. Look up its
# entry there, and use that for the actual option processing.
$pos = $argpos{$arg};
confess ("internal error: didn't find arg in arg hash even " .
"after resolving abbreviation")
unless defined $pos;
my $opt_desc = $opt_table->[$pos];
my $type = $opt_desc->[1];
my $handler = $OptionHandlers{$type};
if (defined $handler && ref ($handler) eq 'CODE')
{
if (! &$handler ($arg, \@tmp_arglist, $opt_desc, $spoof))
{
warn $Usage if defined $Usage;
warn "$ErrorMessage\n";
return 0;
}
}
else
{
croak "Unknown option type \"$type\" (found for arg $arg)";
}
} # while ($arg = shift @$arglist)
return 1;
} # GetOptions
sub SpoofGetOptions
{
&GetOptions (@_[0..2], 1);
}
1;
|