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;
13 use Getopt::Long 2.37 ();
15 has ARGV => (is => 'rw', isa => 'ArrayRef', traits => ['NoGetopt']);
16 has extra_argv => (is => 'rw', isa => 'ArrayRef', traits => ['NoGetopt']);
19 my ($class, @params) = @_;
21 my $constructor_params = ( @params == 1 ? $params[0] : {@params} );
24 if($class->meta->does_role('MooseX::ConfigFromFile')) {
27 # just get the configfile arg now out of @ARGV; the rest of the args
28 # will be fetched later
30 my $opt_parser = Getopt::Long::Parser->new( config => [ qw( no_auto_help pass_through no_auto_version ) ] );
31 $opt_parser->getoptions( "configfile=s" => \$configfile );
33 my $cfmeta = $class->meta->find_attribute_by_name('configfile');
34 my $init_arg = $cfmeta->init_arg;
36 # was it passed to the constructor?
37 if (!defined $configfile)
39 $configfile = $constructor_params->{$init_arg} if defined $init_arg;
42 if(!defined $configfile) {
43 # this is a classic legacy usecase documented in
44 # MooseX::ConfigFromFile that we should continue to support
45 $configfile = try { $class->configfile };
47 $configfile = $cfmeta->default
48 if not defined $configfile and $cfmeta->has_default;
50 # note that this will die horribly if the default sub depends on
52 $configfile = $configfile->($class) if ref $configfile eq 'CODE';
53 if (defined $configfile) {
54 $config_from_file = try {
55 $class->get_config_from_file($configfile);
58 die $_ unless /Specified configfile '\Q$configfile\E' does not exist/;
62 $constructor_params->{$init_arg} = $configfile
63 if defined $configfile and defined $init_arg;
66 $config_from_file = $class->get_config_from_file($configfile);
70 Carp::croak("Single parameters to new_with_options() must be a HASH ref")
71 unless ref($constructor_params) eq 'HASH';
73 my %processed = $class->_parse_argv(
75 $class->_attrs_to_options( $config_from_file )
77 params => $constructor_params,
80 my $params = $config_from_file ? { %$config_from_file, %{$processed{params}} } : $processed{params};
82 # did the user request usage information?
83 if ( $processed{usage} and $params->{help_flag} ) {
84 $class->print_usage_text($processed{usage});
87 return MooseX::Getopt::ProcessedArgv->new(
88 argv_copy => $processed{argv_copy},
89 extra_argv => $processed{argv},
90 usage => $processed{usage},
91 constructor_params => $constructor_params, # explicit params to ->new
92 cli_params => $params, # params from CLI
96 sub new_with_options {
97 my ($class, @params) = @_;
99 my $pa = $class->process_argv(@params);
102 ARGV => $pa->argv_copy,
103 extra_argv => $pa->extra_argv,
104 ( $pa->usage ? ( usage => $pa->usage ) : () ),
105 %{ $pa->constructor_params }, # explicit params to ->new
106 %{ $pa->cli_params }, # params from CLI
110 sub _getopt_spec { shift->_traditional_spec(@_); }
113 my ( $class, %params ) = @_;
115 local @ARGV = @{ $params{params}{argv} || \@ARGV };
117 my ( $opt_spec, $name_to_init_arg ) = $class->_getopt_spec(%params);
119 # Get a clean copy of the original @ARGV
120 my $argv_copy = [ @ARGV ];
123 my ( $parsed_options, $usage ) = try {
124 local $SIG{__WARN__} = sub { push @warnings, @_ };
126 return $class->_getopt_get_options(\%params, $opt_spec);
129 $class->_getopt_spec_exception(\@warnings, $_);
132 $class->_getopt_spec_warnings(@warnings) if @warnings;
134 # Get a copy of the Getopt::Long-mangled @ARGV
135 my $argv_mangled = [ @ARGV ];
137 my %constructor_args = (
139 $name_to_init_arg->{$_} => $parsed_options->{$_}
140 } keys %$parsed_options,
144 params => \%constructor_args,
145 argv_copy => $argv_copy,
146 argv => $argv_mangled,
147 ( defined($usage) ? ( usage => $usage ) : () ),
151 sub _getopt_get_options {
152 my ($class, $params, $opt_spec) = @_;
154 Getopt::Long::GetOptions(\%options, @$opt_spec);
155 return ( \%options, undef );
158 sub _getopt_spec_warnings { }
160 sub _getopt_spec_exception {
161 my ($self, $warnings, $exception) = @_;
162 die @$warnings, $exception;
165 # maintained for backwards compatibility only
166 sub _getopt_full_usage
168 my ($self, $usage) = @_;
172 #(this is already documented in MooseX::Getopt. But FIXME later, via RT#82195)
176 sub print_usage_text { shift->_getopt_full_usage(@_) }
179 return "usage: %c %o";
182 sub _traditional_spec {
183 my ( $class, %params ) = @_;
185 my ( @options, %name_to_init_arg, %options );
187 foreach my $opt ( @{ $params{options} } ) {
188 push @options, $opt->{opt_string};
190 my $identifier = $opt->{name};
191 $identifier =~ s/\W/_/g; # Getopt::Long does this to all option names
193 $name_to_init_arg{$identifier} = $opt->{init_arg};
196 return ( \@options, \%name_to_init_arg );
199 sub _compute_getopt_attrs {
201 sort { $a->insertion_order <=> $b->insertion_order }
203 $_->does("MooseX::Getopt::Meta::Attribute::Trait")
207 !$_->does('MooseX::Getopt::Meta::Attribute::Trait::NoGetopt')
208 } $class->meta->get_all_attributes
211 sub _get_cmd_flags_for_attr {
212 my ( $class, $attr ) = @_;
214 my $flag = $attr->name;
218 if ($attr->does('MooseX::Getopt::Meta::Attribute::Trait')) {
219 $flag = $attr->cmd_flag if $attr->has_cmd_flag;
220 @aliases = @{ $attr->cmd_aliases } if $attr->has_cmd_aliases;
223 return ( $flag, @aliases );
226 sub _attrs_to_options {
228 my $config_from_file = shift || {};
232 foreach my $attr ($class->_compute_getopt_attrs) {
233 my ( $flag, @aliases ) = $class->_get_cmd_flags_for_attr($attr);
235 my $opt_string = join(q{|}, $flag, @aliases);
237 if ($attr->name eq 'configfile') {
240 elsif ($attr->has_type_constraint) {
241 my $type = $attr->type_constraint;
242 if (MooseX::Getopt::OptionTypeMap->has_option_type($type)) {
243 $opt_string .= MooseX::Getopt::OptionTypeMap->get_option_type($type)
249 init_arg => $attr->init_arg,
250 opt_string => $opt_string,
251 required => $attr->is_required && !$attr->has_default && !$attr->has_builder && !exists $config_from_file->{$attr->name},
253 # this "feature" was breaking because
254 # Getopt::Long::Descriptive would return
255 # the default value as if it was a command
256 # line flag, which would then override the
257 # one passed into a constructor.
258 # See 100_gld_default_bug.t for an example
260 #( ( $attr->has_default && ( $attr->is_default_a_coderef xor $attr->is_lazy ) ) ? ( default => $attr->default({}) ) : () ),
261 ( $attr->has_documentation ? ( doc => $attr->documentation ) : () ),
277 with 'MooseX::Getopt::Basic';
279 has 'out' => (is => 'rw', isa => 'Str', required => 1);
280 has 'in' => (is => 'rw', isa => 'Str', required => 1);
282 # ... rest of the class here
289 my $app = My::App->new_with_options();
290 # ... rest of the script here
292 ## on the command line
293 % perl my_app_script.pl --in file.input --out file.dump
297 This is like L<MooseX::Getopt> and can be used instead except that it
298 doesn't make use of L<Getopt::Long::Descriptive> (or "GLD" for short).
300 =method new_with_options
302 See L<MooseX::Getopt/new_with_options>.
306 See L<MooseX::Getopt/process_argv>.