Getopt-Long-More
view release on metacpan or search on metacpan
lib/Getopt/Long/More.pm view on Meta::CPAN
## no critic: Modules::ProhibitAutomaticExportation
package Getopt::Long::More;
our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
our $DATE = '2020-04-08'; # DATE
our $DIST = 'Getopt-Long-More'; # DIST
our $VERSION = '0.007'; # VERSION
use strict;
use Exporter qw(import);
our @EXPORT = qw(GetOptions optspec OptSpec);
our @EXPORT_OK = qw(HelpMessage VersionMessage Configure
GetOptionsFromArray GetOptionsFromString
OptionsPod);
sub optspec {
Getopt::Long::More::OptSpec->new(@_);
}
# synonym for convenience
sub OptSpec {
Getopt::Long::More::OptSpec->new(@_);
}
sub VersionMessage {
require Getopt::Long;
goto &Getopt::Long::VersionMessage;
}
sub Configure {
require Getopt::Long;
goto &Getopt::Long::Configure;
}
# copied verbatim from Getopt::Long, with a bit of modification (add my)
sub GetOptionsFromString(@) {
my ($string) = shift;
require Text::ParseWords;
my $args = [ Text::ParseWords::shellwords($string) ];
local $Getopt::Long::caller ||= (caller)[0];
my $ret = GetOptionsFromArray($args, @_);
return ( $ret, $args ) if wantarray;
if ( @$args ) {
$ret = 0;
warn("GetOptionsFromString: Excess data \"@$args\" in string \"$string\"\n");
}
$ret;
}
# copied verbatim from Getopt::Long
sub GetOptions(@) {
# Shift in default array.
unshift(@_, \@ARGV);
# Try to keep caller() and Carp consistent.
goto &GetOptionsFromArray;
}
my $_cur_opts_spec = [];
sub GetOptionsFromArray {
require Getopt::Long;
my $ary = shift;
local $Getopt::Long::caller ||= (caller)[0]; # grab and set this asap.
my @go_opts_spec;
if ( ref($_[0]) ) {
require Scalar::Util;
if ( Scalar::Util::reftype ($_[0]) eq 'HASH') {
push @go_opts_spec, shift; # 'hash-storage' is now directly supported
}
}
my @opts_spec = @_;
# provide explicit --help|?, for completion. also, we need to override the
# option destination to use our HelpMessage.
if ($Getopt::Long::auto_help) {
unshift @opts_spec, 'help|?' => optspec(
destination => sub { HelpMessage() },
summary => 'Print help message and exit',
);
}
local $Getopt::Long::auto_help = 0;
# provide explicit --version, for completion
if ($Getopt::Long::auto_version) {
unshift @opts_spec, 'version' => optspec(
destination => sub { VersionMessage() },
summary => 'Print program version and exit',
);
}
local $Getopt::Long::auto_version = 0;
# to allow our HelpMessage to generate usage/help based on options spec
$_cur_opts_spec = [@opts_spec];
# strip the optspec objects
my $prev;
my $has_arg_handler;
my $arg_handler_accessed;
MAPPING: # Resulting in the complete EVAPORATION of OptSpec objects, replaced by their destination, if one exists.
for my $e (@opts_spec) {
unless ( ref($e) eq 'Getopt::Long::More::OptSpec' ) {
push @go_opts_spec, $e;
next;
}
next unless exists $e->{destination};
if ( $prev eq '<>' ) {
$has_arg_handler++;
push @go_opts_spec, sub {
$arg_handler_accessed++;
$e->{destination}->(@_);
};
} else {
push @go_opts_spec, $e->{destination};
}
} continue {
$prev = $e;
}
( run in 2.223 seconds using v1.01-cache-2.11-cpan-364913b4093 )