📄 long.pm
字号:
# GetOpt::Long.pm -- Universal options parsingpackage Getopt::Long;# RCS Status : $Id: GetoptLong.pl,v 2.26 2001-01-31 10:20:29+01 jv Exp $# Author : Johan Vromans# Created On : Tue Sep 11 15:00:12 1990# Last Modified By: Johan Vromans# Last Modified On: Sat Jan 6 17:12:27 2001# Update Count : 748# Status : Released################ Copyright ################# This program is Copyright 1990,2001 by Johan Vromans.# This program is free software; you can redistribute it and/or# modify it under the terms of the Perl Artistic License or the# GNU General Public License as published by the Free Software# Foundation; either version 2 of the License, or (at your option) any# later version.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License for more details.## If you do not have a copy of the GNU General Public License write to# the Free Software Foundation, Inc., 675 Mass Ave, Cambridge,# MA 02139, USA.################ Module Preamble ################use 5.004;use strict;use vars qw($VERSION $VERSION_STRING);$VERSION = 2.25;$VERSION_STRING = "2.25";use Exporter;use AutoLoader qw(AUTOLOAD);use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);@ISA = qw(Exporter);%EXPORT_TAGS = qw();BEGIN { # Init immediately so their contents can be used in the 'use vars' below. @EXPORT = qw(&GetOptions $REQUIRE_ORDER $PERMUTE $RETURN_IN_ORDER); @EXPORT_OK = qw();}# User visible variables.use vars @EXPORT, @EXPORT_OK;use vars qw($error $debug $major_version $minor_version);# Deprecated visible variables.use vars qw($autoabbrev $getopt_compat $ignorecase $bundling $order $passthrough);# Official invisible variables.use vars qw($genprefix $caller $gnu_compat);# Public subroutines.sub Configure (@);sub config (@); # deprecated namesub GetOptions;# Private subroutines.sub ConfigDefaults ();sub FindOption ($$$$$$$);sub Croak (@); # demand loading the real Croak################ Local Variables ################################ Resident subroutines ################sub ConfigDefaults () { # Handle POSIX compliancy. if ( defined $ENV{"POSIXLY_CORRECT"} ) { $genprefix = "(--|-)"; $autoabbrev = 0; # no automatic abbrev of options $bundling = 0; # no bundling of single letter switches $getopt_compat = 0; # disallow '+' to start options $order = $REQUIRE_ORDER; } else { $genprefix = "(--|-|\\+)"; $autoabbrev = 1; # automatic abbrev of options $bundling = 0; # bundling off by default $getopt_compat = 1; # allow '+' to start options $order = $PERMUTE; } # Other configurable settings. $debug = 0; # for debugging $error = 0; # error tally $ignorecase = 1; # ignore case when matching options $passthrough = 0; # leave unrecognized options alone $gnu_compat = 0; # require --opt=val if value is optional}# Override import.sub import { my $pkg = shift; # package my @syms = (); # symbols to import my @config = (); # configuration my $dest = \@syms; # symbols first for ( @_ ) { if ( $_ eq ':config' ) { $dest = \@config; # config next next; } push (@$dest, $_); # push } # Hide one level and call super. local $Exporter::ExportLevel = 1; $pkg->SUPER::import(@syms); # And configure. Configure (@config) if @config;}################ Initialization ################# Values for $order. See GNU getopt.c for details.($REQUIRE_ORDER, $PERMUTE, $RETURN_IN_ORDER) = (0..2);# Version major/minor numbers.($major_version, $minor_version) = $VERSION =~ /^(\d+)\.(\d+)/;ConfigDefaults();################ OO Interface ################package Getopt::Long::Parser;# NOTE: The object oriented routines use $error for thread locking.my $_lock = sub { lock ($Getopt::Long::error) if $] >= 5.005};# Store a copy of the default configuration. Since ConfigDefaults has# just been called, what we get from Configure is the default.my $default_config = do { &$_lock; Getopt::Long::Configure ()};sub new { my $that = shift; my $class = ref($that) || $that; my %atts = @_; # Register the callers package. my $self = { caller_pkg => (caller)[0] }; bless ($self, $class); # Process config attributes. if ( defined $atts{config} ) { &$_lock; my $save = Getopt::Long::Configure ($default_config, @{$atts{config}}); $self->{settings} = Getopt::Long::Configure ($save); delete ($atts{config}); } # Else use default config. else { $self->{settings} = $default_config; } if ( %atts ) { # Oops Getopt::Long::Croak(__PACKAGE__.": unhandled attributes: ". join(" ", sort(keys(%atts)))); } $self;}sub configure { my ($self) = shift; &$_lock; # Restore settings, merge new settings in. my $save = Getopt::Long::Configure ($self->{settings}, @_); # Restore orig config and save the new config. $self->{settings} = Configure ($save);}sub getoptions { my ($self) = shift; &$_lock; # Restore config settings. my $save = Getopt::Long::Configure ($self->{settings}); # Call main routine. my $ret = 0; $Getopt::Long::caller = $self->{caller_pkg}; eval { $ret = Getopt::Long::GetOptions (@_); }; # Restore saved settings. Getopt::Long::Configure ($save); # Handle errors and return value. die ($@) if $@; return $ret;}package Getopt::Long;################ Package return ################1;__END__################ AutoLoading subroutines ################# RCS Status : $Id: GetoptLongAl.pl,v 2.30 2001-01-31 10:21:11+01 jv Exp $# Author : Johan Vromans# Created On : Fri Mar 27 11:50:30 1998# Last Modified By: Johan Vromans# Last Modified On: Tue Dec 26 18:01:16 2000# Update Count : 98# Status : Releasedsub GetOptions { my @optionlist = @_; # local copy of the option descriptions my $argend = '--'; # option list terminator my %opctl = (); # table of arg.specs (long and abbrevs) my %bopctl = (); # table of arg.specs (bundles) my $pkg = $caller || (caller)[0]; # current context # Needed if linkage is omitted. my %aliases= (); # alias table my @ret = (); # accum for non-options my %linkage; # linkage my $userlinkage; # user supplied HASH my $opt; # current option my $genprefix = $genprefix; # so we can call the same module many times my @opctl; # the possible long option names $error = ''; print STDERR ("GetOpt::Long $Getopt::Long::VERSION ", "called from package \"$pkg\".", "\n ", 'GetOptionsAl $Revision: 2.30 $ ', "\n ", "ARGV: (@ARGV)", "\n ", "autoabbrev=$autoabbrev,". "bundling=$bundling,", "getopt_compat=$getopt_compat,", "gnu_compat=$gnu_compat,", "order=$order,", "\n ", "ignorecase=$ignorecase,", "passthrough=$passthrough,", "genprefix=\"$genprefix\".", "\n") if $debug; # Check for ref HASH as first argument. # First argument may be an object. It's OK to use this as long # as it is really a hash underneath. $userlinkage = undef; if ( ref($optionlist[0]) and "$optionlist[0]" =~ /^(?:.*\=)?HASH\([^\(]*\)$/ ) { $userlinkage = shift (@optionlist); print STDERR ("=> user linkage: $userlinkage\n") if $debug; } # See if the first element of the optionlist contains option # starter characters. # Be careful not to interpret '<>' as option starters. if ( $optionlist[0] =~ /^\W+$/ && !($optionlist[0] eq '<>' && @optionlist > 0 && ref($optionlist[1])) ) { $genprefix = shift (@optionlist); # Turn into regexp. Needs to be parenthesized! $genprefix =~ s/(\W)/\\$1/g; $genprefix = "([" . $genprefix . "])"; } # Verify correctness of optionlist. %opctl = (); %bopctl = (); while ( @optionlist > 0 ) { my $opt = shift (@optionlist); # Strip leading prefix so people can specify "--foo=i" if they like. $opt = $+ if $opt =~ /^$genprefix+(.*)$/s; if ( $opt eq '<>' ) { if ( (defined $userlinkage) && !(@optionlist > 0 && ref($optionlist[0])) && (exists $userlinkage->{$opt}) && ref($userlinkage->{$opt}) ) { unshift (@optionlist, $userlinkage->{$opt}); } unless ( @optionlist > 0 && ref($optionlist[0]) && ref($optionlist[0]) eq 'CODE' ) { $error .= "Option spec <> requires a reference to a subroutine\n"; next; } $linkage{'<>'} = shift (@optionlist); next; } # Match option spec. Allow '?' as an alias only. if ( $opt !~ /^((\w+[-\w]*)(\|(\?|\w[-\w]*)?)*)?([!~+]|[=:][infse][@%]?)?$/ ) { $error .= "Error in option spec: \"$opt\"\n"; next; } my ($o, $c, $a) = ($1, $5); $c = '' unless defined $c; # $linko keeps track of the primary name the user specified. # This name will be used for the internal or external linkage. # In other words, if the user specifies "FoO|BaR", it will # match any case combinations of 'foo' and 'bar', but if a global # variable needs to be set, it will be $opt_FoO in the exact case # as specified. my $linko; if ( ! defined $o ) { # empty -> '-' option $linko = $o = ''; $opctl{''} = $c; $bopctl{''} = $c if $bundling; } else { # Handle alias names my @o = split (/\|/, $o); $linko = $o = $o[0]; # Force an alias if the option name is not locase. $a = $o unless $o eq lc($o); $o = lc ($o) if $ignorecase > 1 || ($ignorecase && ($bundling ? length($o) > 1 : 1)); foreach ( @o ) { if ( $bundling && length($_) == 1 ) { $_ = lc ($_) if $ignorecase > 1; if ( $c eq '!' ) { $opctl{"no$_"} = $c; warn ("Ignoring '!' modifier for short option $_\n"); $opctl{$_} = $bopctl{$_} = ''; } else { $opctl{$_} = $bopctl{$_} = $c; } } else { $_ = lc ($_) if $ignorecase; if ( $c eq '!' ) { $opctl{"no$_"} = $c; $opctl{$_} = '' } else { $opctl{$_} = $c; } } if ( defined $a ) { # Note alias. $aliases{$_} = $a; } else { # Set primary name. $a = $_; } } } # If no linkage is supplied in the @optionlist, copy it from # the userlinkage if available. if ( defined $userlinkage ) { unless ( @optionlist > 0 && ref($optionlist[0]) ) { if ( exists $userlinkage->{$linko} && ref($userlinkage->{$linko}) ) { print STDERR ("=> found userlinkage for \"$linko\": ", "$userlinkage->{$linko}\n") if $debug; unshift (@optionlist, $userlinkage->{$linko}); } else { # Do nothing. Being undefined will be handled later. next; } } } # Copy the linkage. If omitted, link to global variable. if ( @optionlist > 0 && ref($optionlist[0]) ) { print STDERR ("=> link \"$linko\" to $optionlist[0]\n") if $debug; if ( ref($optionlist[0]) =~ /^(SCALAR|CODE)$/ ) { $linkage{$linko} = shift (@optionlist); } elsif ( ref($optionlist[0]) =~ /^(ARRAY)$/ ) { $linkage{$linko} = shift (@optionlist); $opctl{$o} .= '@' if $opctl{$o} ne '' and $opctl{$o} !~ /\@$/; $bopctl{$o} .= '@' if $bundling and defined $bopctl{$o} and $bopctl{$o} ne '' and $bopctl{$o} !~ /\@$/; } elsif ( ref($optionlist[0]) =~ /^(HASH)$/ ) { $linkage{$linko} = shift (@optionlist); $opctl{$o} .= '%' if $opctl{$o} ne '' and $opctl{$o} !~ /\%$/; $bopctl{$o} .= '%' if $bundling and defined $bopctl{$o} and $bopctl{$o} ne '' and $bopctl{$o} !~ /\%$/; } else { $error .= "Invalid option linkage for \"$opt\"\n"; } } else { # Link to global $opt_XXX variable. # Make sure a valid perl identifier results. my $ov = $linko; $ov =~ s/\W/_/g; if ( $c =~ /@/ ) { print STDERR ("=> link \"$linko\" to \@$pkg","::opt_$ov\n") if $debug; eval ("\$linkage{\$linko} = \\\@".$pkg."::opt_$ov;"); } elsif ( $c =~ /%/ ) { print STDERR ("=> link \"$linko\" to \%$pkg","::opt_$ov\n") if $debug; eval ("\$linkage{\$linko} = \\\%".$pkg."::opt_$ov;"); } else { print STDERR ("=> link \"$linko\" to \$$pkg","::opt_$ov\n") if $debug; eval ("\$linkage{\$linko} = \\\$".$pkg."::opt_$ov;"); } } } # Bail out if errors found. die ($error) if $error; $error = 0; # Sort the possible long option names. @opctl = sort(keys (%opctl)) if $autoabbrev; # Show the options tables if debugging. if ( $debug ) { my ($arrow, $k, $v); $arrow = "=> "; while ( ($k,$v) = each(%opctl) ) { print STDERR ($arrow, "\$opctl{\"$k\"} = \"$v\"\n"); $arrow = " "; } $arrow = "=> "; while ( ($k,$v) = each(%bopctl) ) { print STDERR ($arrow, "\$bopctl{\"$k\"} = \"$v\"\n"); $arrow = " "; } } # Process argument list my $goon = 1; while ( $goon && @ARGV > 0 ) { #### Get next argument #### $opt = shift (@ARGV);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -