1 package MooseX::Getopt::Basic;
2 # ABSTRACT: MooseX::Getopt::Basic - role to implement the Getopt::Long functionality
6 use MooseX::Getopt::OptionTypeMap;
7 use MooseX::Getopt::Meta::Attribute;
8 use MooseX::Getopt::Meta::Attribute::NoGetopt;
11 use Getopt::Long 2.37 ();
13 has ARGV => (is => 'rw', isa => 'ArrayRef', metaclass => "NoGetopt");
14 has extra_argv => (is => 'rw', isa => 'ArrayRef', metaclass => "NoGetopt");
16 sub new_with_options {
17 my ($class, @params) = @_;
20 if($class->meta->does_role('MooseX::ConfigFromFile')) {
24 my $opt_parser = Getopt::Long::Parser->new( config => [ qw( pass_through ) ] );
25 $opt_parser->getoptions( "configfile=s" => \$configfile );
27 if(!defined $configfile) {
28 my $cfmeta = $class->meta->find_attribute_by_name('configfile');
29 $configfile = $cfmeta->default if $cfmeta->has_default;
30 if (ref $configfile eq 'CODE') {
31 # not sure theres a lot you can do with the class and may break some assumptions
33 $configfile = &$configfile($class);
35 if (defined $configfile) {
36 $config_from_file = eval {
37 $class->get_config_from_file($configfile);
40 die $@ unless $@ =~ /Specified configfile '\Q$configfile\E' does not exist/;
45 $config_from_file = $class->get_config_from_file($configfile);
49 my $constructor_params = ( @params == 1 ? $params[0] : {@params} );
51 Carp::croak("Single parameters to new_with_options() must be a HASH ref")
52 unless ref($constructor_params) eq 'HASH';
54 my %processed = $class->_parse_argv(
56 $class->_attrs_to_options( $config_from_file )
58 params => $constructor_params,
61 my $params = $config_from_file ? { %$config_from_file, %{$processed{params}} } : $processed{params};
63 # did the user request usage information?
64 if ( $processed{usage} && ($params->{'?'} or $params->{help} or $params->{usage}) )
66 $class->_getopt_full_usage($processed{usage});
70 ARGV => $processed{argv_copy},
71 extra_argv => $processed{argv},
72 %$constructor_params, # explicit params to ->new
73 %$params, # params from CLI
77 sub _getopt_spec { shift->_traditional_spec(@_); }
80 my ( $class, %params ) = @_;
82 local @ARGV = @{ $params{params}{argv} || \@ARGV };
84 my ( $opt_spec, $name_to_init_arg ) = $class->_getopt_spec(%params);
86 # Get a clean copy of the original @ARGV
87 my $argv_copy = [ @ARGV ];
90 my ( $parsed_options, $usage ) = eval {
91 local $SIG{__WARN__} = sub { push @warnings, @_ };
93 return $class->_getopt_get_options(\%params, $opt_spec);
96 $class->_getopt_spec_warnings(@warnings) if @warnings;
97 $class->_getopt_spec_exception(\@warnings, $@) if $@;
99 # Get a copy of the Getopt::Long-mangled @ARGV
100 my $argv_mangled = [ @ARGV ];
102 my %constructor_args = (
104 $name_to_init_arg->{$_} => $parsed_options->{$_}
105 } keys %$parsed_options,
109 params => \%constructor_args,
110 argv_copy => $argv_copy,
111 argv => $argv_mangled,
112 ( defined($usage) ? ( usage => $usage ) : () ),
116 sub _getopt_get_options {
117 my ($class, $params, $opt_spec) = @_;
119 Getopt::Long::GetOptions(\%options, @$opt_spec);
120 return ( \%options, undef );
123 sub _getopt_spec_warnings { }
125 sub _getopt_spec_exception {
126 my ($self, $warnings, $exception) = @_;
127 die @$warnings, $exception;
130 sub _getopt_full_usage {
131 my ($self, $usage) = @_;
136 return "usage: %c %o";
139 sub _traditional_spec {
140 my ( $class, %params ) = @_;
142 my ( @options, %name_to_init_arg, %options );
144 foreach my $opt ( @{ $params{options} } ) {
145 push @options, $opt->{opt_string};
147 my $identifier = $opt->{name};
148 $identifier =~ s/\W/_/g; # Getopt::Long does this to all option names
150 $name_to_init_arg{$identifier} = $opt->{init_arg};
153 return ( \@options, \%name_to_init_arg );
156 sub _compute_getopt_attrs {
159 $_->does("MooseX::Getopt::Meta::Attribute::Trait")
163 !$_->does('MooseX::Getopt::Meta::Attribute::Trait::NoGetopt')
164 } $class->meta->get_all_attributes
167 sub _get_cmd_flags_for_attr {
168 my ( $class, $attr ) = @_;
170 my $flag = $attr->name;
174 if ($attr->does('MooseX::Getopt::Meta::Attribute::Trait')) {
175 $flag = $attr->cmd_flag if $attr->has_cmd_flag;
176 @aliases = @{ $attr->cmd_aliases } if $attr->has_cmd_aliases;
179 return ( $flag, @aliases );
182 sub _attrs_to_options {
184 my $config_from_file = shift || {};
188 foreach my $attr ($class->_compute_getopt_attrs) {
189 my ( $flag, @aliases ) = $class->_get_cmd_flags_for_attr($attr);
191 my $opt_string = join(q{|}, $flag, @aliases);
193 if ($attr->name eq 'configfile') {
196 elsif ($attr->has_type_constraint) {
197 my $type = $attr->type_constraint;
198 if (MooseX::Getopt::OptionTypeMap->has_option_type($type)) {
199 $opt_string .= MooseX::Getopt::OptionTypeMap->get_option_type($type)
205 init_arg => $attr->init_arg,
206 opt_string => $opt_string,
207 required => $attr->is_required && !$attr->has_default && !$attr->has_builder && !exists $config_from_file->{$attr->name},
209 # this "feature" was breaking because
210 # Getopt::Long::Descriptive would return
211 # the default value as if it was a command
212 # line flag, which would then override the
213 # one passed into a constructor.
214 # See 100_gld_default_bug.t for an example
216 #( ( $attr->has_default && ( $attr->is_default_a_coderef xor $attr->is_lazy ) ) ? ( default => $attr->default({}) ) : () ),
217 ( $attr->has_documentation ? ( doc => $attr->documentation ) : () ),
233 with 'MooseX::Getopt::Basic';
235 has 'out' => (is => 'rw', isa => 'Str', required => 1);
236 has 'in' => (is => 'rw', isa => 'Str', required => 1);
238 # ... rest of the class here
245 my $app = My::App->new_with_options();
246 # ... rest of the script here
248 ## on the command line
249 % perl my_app_script.pl --in file.input --out file.dump
253 This is like L<MooseX::Getopt> and can be used instead except that it
254 doesn't make use of L<Getopt::Long::Descriptive> (or "GLD" for short).
256 =method new_with_options
258 See L<MooseX::Getopt/new_with_options>.