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;
9 use MooseX::Getopt::ProcessedArgv;
12 use Getopt::Long 2.37 ();
14 has ARGV => (is => 'rw', isa => 'ArrayRef', metaclass => "NoGetopt");
15 has extra_argv => (is => 'rw', isa => 'ArrayRef', metaclass => "NoGetopt");
18 my ($class, @params) = @_;
20 my $constructor_params = ( @params == 1 ? $params[0] : {@params} );
23 if($class->meta->does_role('MooseX::ConfigFromFile')) {
26 # just get the configfile arg now out of @ARGV; the rest of the args
27 # will be fetched later
29 my $opt_parser = Getopt::Long::Parser->new( config => [ qw( no_auto_help pass_through no_auto_version ) ] );
30 $opt_parser->getoptions( "configfile=s" => \$configfile );
32 my $cfmeta = $class->meta->find_attribute_by_name('configfile');
34 # was it passed to the constructor?
35 if (!defined $configfile)
37 my $key = $cfmeta->init_arg;
38 $configfile = $constructor_params->{$key} if $key;
41 if(!defined $configfile) {
42 $configfile = $cfmeta->default if $cfmeta->has_default;
43 if (ref $configfile eq 'CODE') {
44 # not sure theres a lot you can do with the class and may break some assumptions
46 $configfile = &$configfile($class);
48 if (defined $configfile) {
49 $config_from_file = eval {
50 $class->get_config_from_file($configfile);
53 die $@ unless $@ =~ /Specified configfile '\Q$configfile\E' does not exist/;
58 $config_from_file = $class->get_config_from_file($configfile);
62 Carp::croak("Single parameters to new_with_options() must be a HASH ref")
63 unless ref($constructor_params) eq 'HASH';
65 my %processed = $class->_parse_argv(
67 $class->_attrs_to_options( $config_from_file )
69 params => $constructor_params,
72 my $params = $config_from_file ? { %$config_from_file, %{$processed{params}} } : $processed{params};
74 # did the user request usage information?
75 if ( $processed{usage} and $params->{help_flag} ) {
76 $class->_getopt_full_usage($processed{usage});
79 return MooseX::Getopt::ProcessedArgv->new(
80 argv_copy => $processed{argv_copy},
81 extra_argv => $processed{argv},
82 usage => $processed{usage},
83 constructor_params => $constructor_params, # explicit params to ->new
84 cli_params => $params, # params from CLI
88 sub new_with_options {
89 my ($class, @params) = @_;
91 my $pa = $class->process_argv(@params);
94 ARGV => $pa->argv_copy,
95 extra_argv => $pa->extra_argv,
96 ( $pa->usage ? ( usage => $pa->usage ) : () ),
97 %{ $pa->constructor_params }, # explicit params to ->new
98 %{ $pa->cli_params }, # params from CLI
102 sub _getopt_spec { shift->_traditional_spec(@_); }
105 my ( $class, %params ) = @_;
107 local @ARGV = @{ $params{params}{argv} || \@ARGV };
109 my ( $opt_spec, $name_to_init_arg ) = $class->_getopt_spec(%params);
111 # Get a clean copy of the original @ARGV
112 my $argv_copy = [ @ARGV ];
115 my ( $parsed_options, $usage ) = eval {
116 local $SIG{__WARN__} = sub { push @warnings, @_ };
118 return $class->_getopt_get_options(\%params, $opt_spec);
121 $class->_getopt_spec_warnings(@warnings) if @warnings;
122 $class->_getopt_spec_exception(\@warnings, $@) if $@;
124 # Get a copy of the Getopt::Long-mangled @ARGV
125 my $argv_mangled = [ @ARGV ];
127 my %constructor_args = (
129 $name_to_init_arg->{$_} => $parsed_options->{$_}
130 } keys %$parsed_options,
134 params => \%constructor_args,
135 argv_copy => $argv_copy,
136 argv => $argv_mangled,
137 ( defined($usage) ? ( usage => $usage ) : () ),
141 sub _getopt_get_options {
142 my ($class, $params, $opt_spec) = @_;
144 Getopt::Long::GetOptions(\%options, @$opt_spec);
145 return ( \%options, undef );
148 sub _getopt_spec_warnings { }
150 sub _getopt_spec_exception {
151 my ($self, $warnings, $exception) = @_;
152 die @$warnings, $exception;
155 sub _getopt_full_usage {
156 my ($self, $usage) = @_;
162 return "usage: %c %o";
165 sub _traditional_spec {
166 my ( $class, %params ) = @_;
168 my ( @options, %name_to_init_arg, %options );
170 foreach my $opt ( @{ $params{options} } ) {
171 push @options, $opt->{opt_string};
173 my $identifier = $opt->{name};
174 $identifier =~ s/\W/_/g; # Getopt::Long does this to all option names
176 $name_to_init_arg{$identifier} = $opt->{init_arg};
179 return ( \@options, \%name_to_init_arg );
182 sub _compute_getopt_attrs {
184 sort { $a->insertion_order <=> $b->insertion_order }
186 $_->does("MooseX::Getopt::Meta::Attribute::Trait")
190 !$_->does('MooseX::Getopt::Meta::Attribute::Trait::NoGetopt')
191 } $class->meta->get_all_attributes
194 sub _get_cmd_flags_for_attr {
195 my ( $class, $attr ) = @_;
197 my $flag = $attr->name;
201 if ($attr->does('MooseX::Getopt::Meta::Attribute::Trait')) {
202 $flag = $attr->cmd_flag if $attr->has_cmd_flag;
203 @aliases = @{ $attr->cmd_aliases } if $attr->has_cmd_aliases;
206 return ( $flag, @aliases );
209 sub _attrs_to_options {
211 my $config_from_file = shift || {};
215 foreach my $attr ($class->_compute_getopt_attrs) {
216 my ( $flag, @aliases ) = $class->_get_cmd_flags_for_attr($attr);
218 my $opt_string = join(q{|}, $flag, @aliases);
220 if ($attr->name eq 'configfile') {
223 elsif ($attr->has_type_constraint) {
224 my $type = $attr->type_constraint;
225 if (MooseX::Getopt::OptionTypeMap->has_option_type($type)) {
226 $opt_string .= MooseX::Getopt::OptionTypeMap->get_option_type($type)
232 init_arg => $attr->init_arg,
233 opt_string => $opt_string,
234 required => $attr->is_required && !$attr->has_default && !$attr->has_builder && !exists $config_from_file->{$attr->name},
236 # this "feature" was breaking because
237 # Getopt::Long::Descriptive would return
238 # the default value as if it was a command
239 # line flag, which would then override the
240 # one passed into a constructor.
241 # See 100_gld_default_bug.t for an example
243 #( ( $attr->has_default && ( $attr->is_default_a_coderef xor $attr->is_lazy ) ) ? ( default => $attr->default({}) ) : () ),
244 ( $attr->has_documentation ? ( doc => $attr->documentation ) : () ),
260 with 'MooseX::Getopt::Basic';
262 has 'out' => (is => 'rw', isa => 'Str', required => 1);
263 has 'in' => (is => 'rw', isa => 'Str', required => 1);
265 # ... rest of the class here
272 my $app = My::App->new_with_options();
273 # ... rest of the script here
275 ## on the command line
276 % perl my_app_script.pl --in file.input --out file.dump
280 This is like L<MooseX::Getopt> and can be used instead except that it
281 doesn't make use of L<Getopt::Long::Descriptive> (or "GLD" for short).
283 =method new_with_options
285 See L<MooseX::Getopt/new_with_options>.
289 See L<MooseX::Getopt/process_agv>.