MouseX-Getopt
view release on metacpan or search on metacpan
lib/MouseX/Getopt/Basic.pm view on Meta::CPAN
package MouseX::Getopt::Basic;
# ABSTRACT: MouseX::Getopt::Basic - role to implement the Getopt::Long functionality
use Mouse::Role;
use MouseX::Getopt::OptionTypeMap;
use MouseX::Getopt::Meta::Attribute;
use MouseX::Getopt::Meta::Attribute::NoGetopt;
use Carp ();
use Getopt::Long 2.37 ();
has ARGV => (is => 'rw', isa => 'ArrayRef', metaclass => "NoGetopt");
has extra_argv => (is => 'rw', isa => 'ArrayRef', metaclass => "NoGetopt");
sub new_with_options {
my ($class, @params) = @_;
my $config_from_file;
if($class->meta->does_role('MouseX::ConfigFromFile')) {
local @ARGV = @ARGV;
# just get the configfile arg now; the rest of the args will be
# fetched later
my $configfile;
my $opt_parser = Getopt::Long::Parser->new( config => [ qw( no_auto_help pass_through ) ] );
$opt_parser->getoptions( "configfile=s" => \$configfile );
if(!defined $configfile) {
my $cfmeta = $class->meta->find_attribute_by_name('configfile');
$configfile = $cfmeta->default if $cfmeta->has_default;
if (ref $configfile eq 'CODE') {
# not sure theres a lot you can do with the class and may break some assumptions
# warn?
$configfile = &$configfile($class);
}
if (defined $configfile) {
$config_from_file = eval {
$class->get_config_from_file($configfile);
};
if ($@) {
die $@ unless $@ =~ /Specified configfile '\Q$configfile\E' does not exist/;
}
}
}
else {
$config_from_file = $class->get_config_from_file($configfile);
}
}
my $constructor_params = ( @params == 1 ? $params[0] : {@params} );
Carp::croak("Single parameters to new_with_options() must be a HASH ref")
unless ref($constructor_params) eq 'HASH';
my %processed = $class->_parse_argv(
options => [
$class->_attrs_to_options( $config_from_file )
],
params => $constructor_params,
);
my $params = $config_from_file ? { %$config_from_file, %{$processed{params}} } : $processed{params};
# did the user request usage information?
if ( $processed{usage} and $params->{help_flag} )
{
$class->_getopt_full_usage($processed{usage});
}
$class->new(
ARGV => $processed{argv_copy},
extra_argv => $processed{argv},
( $processed{usage} ? ( usage => $processed{usage} ) : () ),
%$constructor_params, # explicit params to ->new
%$params, # params from CLI
);
}
sub _getopt_spec { shift->_traditional_spec(@_); }
sub _parse_argv {
my ( $class, %params ) = @_;
local @ARGV = @{ $params{params}{argv} || \@ARGV };
my ( $opt_spec, $name_to_init_arg ) = $class->_getopt_spec(%params);
# Get a clean copy of the original @ARGV
my $argv_copy = [ @ARGV ];
my @warnings;
my ( $parsed_options, $usage ) = eval {
local $SIG{__WARN__} = sub { push @warnings, @_ };
return $class->_getopt_get_options(\%params, $opt_spec);
};
my $e = $@;
$class->_getopt_spec_warnings(@warnings) if @warnings;
$class->_getopt_spec_exception(\@warnings, $e) if $e;
# Get a copy of the Getopt::Long-mangled @ARGV
my $argv_mangled = [ @ARGV ];
my %constructor_args = (
map {
$name_to_init_arg->{$_} => $parsed_options->{$_}
} keys %$parsed_options,
);
return (
params => \%constructor_args,
argv_copy => $argv_copy,
argv => $argv_mangled,
( defined($usage) ? ( usage => $usage ) : () ),
);
}
sub _getopt_get_options {
my ($class, $params, $opt_spec) = @_;
my %options;
Getopt::Long::GetOptions(\%options, @$opt_spec);
return ( \%options, undef );
}
sub _getopt_spec_warnings { }
sub _getopt_spec_exception {
my ($self, $warnings, $exception) = @_;
die @$warnings, $exception;
}
sub _getopt_full_usage {
my ($self, $usage) = @_;
$usage->die;
}
sub _usage_format {
return "usage: %c %o";
}
sub _traditional_spec {
my ( $class, %params ) = @_;
( run in 0.501 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )