Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
295 changes: 149 additions & 146 deletions lib/Getopt/Std.pm
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Getopt::Std - Process single-character switches with switch clustering
use Getopt::Std;

getopts('oif:'); # -o & -i are boolean flags, -f takes an argument
# Sets $opt_* global variables as a side effect
# Sets $opt_* global variables as a side effect
getopts('oif:', \my %opts); # Options as above, values in %opts
getopt('oDI'); # -o, -D & -I take arguments
# Sets $opt_* global variables as a side effect
Expand Down Expand Up @@ -84,7 +84,7 @@ and C<version_mess()> with the switches string as an argument.

our @ISA = qw(Exporter);
our @EXPORT = qw(getopt getopts);
our $VERSION = '1.14';
our $VERSION = '1.15';
# uncomment the next line to disable 1.03-backward compatibility paranoia
# $STANDARD_HELP_VERSION = 1;

Expand All @@ -95,7 +95,7 @@ our $VERSION = '1.14';
# whether there is a space between the switch and the argument.

# Usage:
# getopt('oDI'); # -o, -D & -I take arg. Sets opt_* as a side effect.
# getopt('oDI'); # -o, -D & -I take arg. Sets opt_* as a side effect.

sub getopt (;$$) {
my ($argumentative, $hash) = @_;
Expand All @@ -105,56 +105,56 @@ sub getopt (;$$) {
local @EXPORT;

while (@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
($first, $rest) = ($1, $2);
if (/^--$/) { # early exit if --
shift @ARGV;
last;
}
if (index($argumentative, $first) >= 0) {
if ($rest ne '') {
shift(@ARGV);
}
else {
shift(@ARGV);
$rest = shift(@ARGV);
}
if (ref $hash) {
$$hash{$first} = $rest;
}
else {
no strict 'refs';
${"opt_$first"} = $rest;
push( @EXPORT, "\$opt_$first" );
}
}
else {
if (ref $hash) {
$$hash{$first} = 1;
}
else {
no strict 'refs';
${"opt_$first"} = 1;
push( @EXPORT, "\$opt_$first" );
}
if ($rest ne '') {
$ARGV[0] = "-$rest";
}
else {
shift(@ARGV);
}
}
($first, $rest) = ($1, $2);
if (/^--$/) { # early exit if --
shift @ARGV;
last;
}
if (index($argumentative, $first) >= 0) {
if ($rest ne '') {
shift(@ARGV);
}
else {
shift(@ARGV);
$rest = shift(@ARGV);
}
if (ref $hash) {
$$hash{$first} = $rest;
}
else {
no strict 'refs';
${"opt_$first"} = $rest;
push( @EXPORT, "\$opt_$first" );
}
}
else {
if (ref $hash) {
$$hash{$first} = 1;
}
else {
no strict 'refs';
${"opt_$first"} = 1;
push( @EXPORT, "\$opt_$first" );
}
if ($rest ne '') {
$ARGV[0] = "-$rest";
}
else {
shift(@ARGV);
}
}
}
unless (ref $hash) {
local $Exporter::ExportLevel = 1;
Getopt::Std->import;
unless (ref $hash) {
local $Exporter::ExportLevel = 1;
Getopt::Std->import;
}
}

our ($OUTPUT_HELP_VERSION, $STANDARD_HELP_VERSION);
sub output_h () {
return $OUTPUT_HELP_VERSION if defined $OUTPUT_HELP_VERSION;
return \*STDOUT if $STANDARD_HELP_VERSION;
return \*STDERR;
return $OUTPUT_HELP_VERSION if defined $OUTPUT_HELP_VERSION;
return \*STDOUT if $STANDARD_HELP_VERSION;
return \*STDERR;
}

sub try_exit () {
Expand All @@ -170,15 +170,16 @@ sub version_mess ($;$) {
my $args = shift;
my $h = output_h;
if (@_ and defined &main::VERSION_MESSAGE) {
main::VERSION_MESSAGE($h, __PACKAGE__, $VERSION, $args);
} else {
my $v = $main::VERSION;
$v = '[unknown]' unless defined $v;
my $myv = $VERSION;
$myv .= ' [paranoid]' unless $STANDARD_HELP_VERSION;
my $perlv = $];
$perlv = sprintf "%vd", $^V if $] >= 5.006;
print $h <<EOH;
main::VERSION_MESSAGE($h, __PACKAGE__, $VERSION, $args);
}
else {
my $v = $main::VERSION;
$v = '[unknown]' unless defined $v;
my $myv = $VERSION;
$myv .= ' [paranoid]' unless $STANDARD_HELP_VERSION;
my $perlv = $];
$perlv = sprintf "%vd", $^V if $] >= 5.006;
print $h <<EOH;
$0 version $v calling Getopt::Std::getopts (version $myv),
running under Perl version $perlv.
EOH
Expand All @@ -189,47 +190,48 @@ sub help_mess ($;$) {
my $args = shift;
my $h = output_h;
if (@_ and defined &main::HELP_MESSAGE) {
main::HELP_MESSAGE($h, __PACKAGE__, $VERSION, $args);
} else {
my (@witharg) = ($args =~ /(\S)\s*:/g);
my (@rest) = ($args =~ /([^\s:])(?!\s*:)/g);
my ($help, $arg) = ('', '');
if (@witharg) {
$help .= "\n\tWith arguments: -" . join " -", @witharg;
$arg = "\nSpace is not required between options and their arguments.";
}
if (@rest) {
$help .= "\n\tBoolean (without arguments): -" . join " -", @rest;
}
my ($scr) = ($0 =~ m,([^/\\]+)$,);
print $h <<EOH if @_; # Let the script override this
main::HELP_MESSAGE($h, __PACKAGE__, $VERSION, $args);
}
else {
my (@witharg) = ($args =~ /(\S)\s*:/g);
my (@rest) = ($args =~ /([^\s:])(?!\s*:)/g);
my ($help, $arg) = ('', '');
if (@witharg) {
$help .= "\n\tWith arguments: -" . join " -", @witharg;
$arg = "\nSpace is not required between options and their arguments.";
}
if (@rest) {
$help .= "\n\tBoolean (without arguments): -" . join " -", @rest;
}
my ($scr) = ($0 =~ m,([^/\\]+)$,);
print $h <<EOH if @_; # Let the script override this

Usage: $scr [-OPTIONS [-MORE_OPTIONS]] [--] [PROGRAM_ARG1 ...]
EOH
print $h <<EOH;
print $h <<EOH;

The following single-character options are accepted:$help

Options may be merged together. -- stops processing of options.$arg
EOH
my $has_pod;
if ( defined $0 and $0 ne '-e' and -f $0 and -r $0
and open my $script, '<', $0 ) {
while (<$script>) {
$has_pod = 1, last if /^=(pod|head1)/;
}
}
print $h <<EOH if $has_pod;
my $has_pod;
if ( defined $0 and $0 ne '-e' and -f $0 and -r $0
and open my $script, '<', $0 ) {
while (<$script>) {
$has_pod = 1, last if /^=(pod|head1)/;
}
}
print $h <<EOH if $has_pod;

For more details run
perldoc -F $0
perldoc -F $0
EOH
}
}

# Usage:
# getopts('a:bc'); # -a takes arg. -b & -c not. Sets opt_* as a
# # side effect.
# getopts('a:bc'); # -a takes arg. -b & -c not. Sets opt_* as a
# # side effect.

sub getopts ($;$) {
my ($argumentative, $hash) = @_;
Expand All @@ -240,71 +242,72 @@ sub getopts ($;$) {

@args = split( / */, $argumentative );
while(@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/s) {
($first, $rest) = ($1, $2);
if (/^--$/) { # early exit if --
shift @ARGV;
last;
}
my $pos = index($argumentative, $first);
if ($pos >= 0) {
if (defined($args[$pos+1]) and ($args[$pos+1] eq ':')) {
shift(@ARGV);
if ($rest eq '') {
++$errs unless @ARGV;
$rest = shift(@ARGV);
}
if (ref $hash) {
$$hash{$first} = $rest;
}
else {
no strict 'refs';
${"opt_$first"} = $rest;
push( @EXPORT, "\$opt_$first" );
}
}
else {
if (ref $hash) {
$$hash{$first} = 1;
}
else {
no strict 'refs';
${"opt_$first"} = 1;
push( @EXPORT, "\$opt_$first" );
}
if ($rest eq '') {
shift(@ARGV);
}
else {
$ARGV[0] = "-$rest";
}
}
}
else {
if ($first eq '-' and $rest eq 'help') {
version_mess($argumentative, 'main');
help_mess($argumentative, 'main');
try_exit();
shift(@ARGV);
next;
} elsif ($first eq '-' and $rest eq 'version') {
version_mess($argumentative, 'main');
try_exit();
shift(@ARGV);
next;
}
warn "Unknown option: $first\n";
++$errs;
if ($rest ne '') {
$ARGV[0] = "-$rest";
}
else {
shift(@ARGV);
}
}
($first, $rest) = ($1, $2);
if (/^--$/) { # early exit if --
shift @ARGV;
last;
}
my $pos = index($argumentative, $first);
if ($pos >= 0) {
if (defined($args[$pos+1]) and ($args[$pos+1] eq ':')) {
shift(@ARGV);
if ($rest eq '') {
++$errs unless @ARGV;
$rest = shift(@ARGV);
}
if (ref $hash) {
$$hash{$first} = $rest;
}
else {
no strict 'refs';
${"opt_$first"} = $rest;
push( @EXPORT, "\$opt_$first" );
}
}
else {
if (ref $hash) {
$$hash{$first} = 1;
}
else {
no strict 'refs';
${"opt_$first"} = 1;
push( @EXPORT, "\$opt_$first" );
}
if ($rest eq '') {
shift(@ARGV);
}
else {
$ARGV[0] = "-$rest";
}
}
}
else {
if ($first eq '-' and $rest eq 'help') {
version_mess($argumentative, 'main');
help_mess($argumentative, 'main');
try_exit();
shift(@ARGV);
next;
}
elsif ($first eq '-' and $rest eq 'version') {
version_mess($argumentative, 'main');
try_exit();
shift(@ARGV);
next;
}
warn "Unknown option: $first\n";
++$errs;
if ($rest ne '') {
$ARGV[0] = "-$rest";
}
else {
shift(@ARGV);
}
}
}
unless (ref $hash) {
local $Exporter::ExportLevel = 1;
Getopt::Std->import;
unless (ref $hash) {
local $Exporter::ExportLevel = 1;
Getopt::Std->import;
}
$errs == 0;
}
Expand Down
Loading
Loading