X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMooseX%2FGetopt.pm;h=04557236fefb6452eef3544223db6713f5a9c395;hb=b56c81234573df88729d5ce348bb460470abc4ff;hp=97d59c8e4bbc9bf33338291cb03daf4362d0da2e;hpb=9f1ec7c0c8860dae99efddfcbba9b30b4dd4bb3f;p=gitmo%2FMooseX-Getopt.git diff --git a/lib/MooseX/Getopt.pm b/lib/MooseX/Getopt.pm index 97d59c8..0455723 100644 --- a/lib/MooseX/Getopt.pm +++ b/lib/MooseX/Getopt.pm @@ -6,10 +6,12 @@ use MooseX::Getopt::OptionTypeMap; use MooseX::Getopt::Meta::Attribute; use MooseX::Getopt::Meta::Attribute::NoGetopt; +use Carp (); + use Getopt::Long (); # GLD uses it anyway, doesn't hurt use constant HAVE_GLD => not not eval { require Getopt::Long::Descriptive }; -our $VERSION = '0.12_01'; +our $VERSION = '0.13'; our $AUTHORITY = 'cpan:STEVAN'; has ARGV => (is => 'rw', isa => 'ArrayRef', metaclass => "NoGetopt"); @@ -36,10 +38,16 @@ sub new_with_options { } } + 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}; @@ -121,12 +129,14 @@ sub _gld_spec { my ( @options, %name_to_init_arg ); + my $constructor_params = $params{params}; + foreach my $opt ( @{ $params{options} } ) { push @options, [ $opt->{opt_string}, $opt->{doc} || ' ', # FIXME new GLD shouldn't need this hack { - ( $opt->{required} ? (required => $opt->{required}) : () ), + ( ( $opt->{required} && !exists($constructor_params->{$opt->{init_arg}}) ) ? (required => $opt->{required}) : () ), ( exists $opt->{default} ? (default => $opt->{default}) : () ), }, ]; @@ -242,10 +252,12 @@ of your attribute as the command line option, and if there is a type constraint defined, it will configure Getopt::Long to handle the option accordingly. -You can use the attribute metaclass L -to get non-default commandline option names and aliases. +You can use the trait L or the +attribute metaclass L to get non-default +commandline option names and aliases. -You can use the attribute metaclass L +You can use the trait L +or the attribute metaclass L to have C ignore your attribute in the commandline options. By default, attributes which start with an underscore are not given