blib/lib/MouseX/Getopt/Basic.pm

filter: Statementstmt 99.1% 111 / 112 Branchbran 87.5% 42 / 48 Conditioncond 85.7% 12 / 14 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 100% 20 / 20 Podpod 100% 1 / 1 totaltotal 94.5% 191 / 202 SCAR 34.7
CC
32
Cov
95%
new_with_options
14.2
_attrs_to_options
10.2
_parse_argv
5.0
CRAP 32.1
↑ Summary blib/lib/MouseX/Getopt/OptionTypeMap.pm »
1 package MouseX::Getopt::Basic;
2 # ABSTRACT: MouseX::Getopt::Basic - role to implement the Getopt::Long functionality
3  
4 22 ▶use Mouse::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 22 ▶use MouseX::Getopt::OptionTypeMap;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 22 ▶use MouseX::Getopt::Meta::Attribute;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 22 ▶use MouseX::Getopt::Meta::Attribute::NoGetopt;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 22 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 22 ▶use Getopt::Long 2.37 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 22 has ARGV => (is => 'rw', isa => 'ArrayRef', metaclass => "NoGetopt");
14 22 has extra_argv => (is => 'rw', isa => 'ArrayRef', metaclass => "NoGetopt");
15  
16 sub new_with_options {
17 109 ▶ my ($class, @params) = @_;
Subroutine new_with_options: called CC=14 SCAR=26.5
Pod documented
18  
19 109 my $config_from_file;
20 109 ▶ if($class->meta->does_role('MouseX::ConfigFromFile')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->meta->does_role("MouseX::ConfigFromFile")) 11 98 109
21 11 local @ARGV = @ARGV;
22  
23 # just get the configfile arg now; the rest of the args will be
24 # fetched later
25 11 my $configfile;
26 11 my $opt_parser = Getopt::Long::Parser->new( config => [ qw( no_auto_help pass_through ) ] );
27 11 $opt_parser->getoptions( "configfile=s" => \$configfile );
28  
29 11 ▶ if(!defined $configfile) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $configfile) { } 6 5 11
30 6 my $cfmeta = $class->meta->find_attribute_by_name('configfile');
31 6 ▶ $configfile = $cfmeta->default if $cfmeta->has_default;
Branch: 1 branch
BranchTrueFalseTotal
if $cfmeta->has_default 3 3 6
32 6 ▶ if (ref $configfile eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $configfile eq "CODE") 2 4 6
33 # not sure theres a lot you can do with the class and may break some assumptions
34 # warn?
35 2 $configfile = &$configfile($class);
36 }
37 6 ▶ if (defined $configfile) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $configfile) 3 3 6
38 3 $config_from_file = eval {
39 3 $class->get_config_from_file($configfile);
40 };
41 3 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 3 3
42 0 ▶ die $@ unless $@ =~ /Specified configfile '\Q$configfile\E' does not exist/;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ =~ /Specified configfile '\Q$configfile\E' does not exist/ 0 0 0
43 }
44 }
45 }
46 else {
47 5 $config_from_file = $class->get_config_from_file($configfile);
48 }
49 }
50  
51 109 ▶ my $constructor_params = ( @params == 1 ? $params[0] : {@params} );
Branch: 1 branch
BranchTrueFalseTotal
@params == 1 ? : 21 88 109
52  
53 109 ▶ Carp::croak("Single parameters to new_with_options() must be a HASH ref")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $constructor_params eq "HASH" 0 109 109
54 unless ref($constructor_params) eq 'HASH';
55  
56 109 my %processed = $class->_parse_argv(
57 options => [
58 $class->_attrs_to_options( $config_from_file )
59 ],
60 params => $constructor_params,
61 );
62  
63 99 ▶ my $params = $config_from_file ? { %$config_from_file, %{$processed{params}} } : $processed{params};
Branch: 1 branch
BranchTrueFalseTotal
$config_from_file ? : 7 92 99
64  
65 # did the user request usage information?
66 99 ▶ if ( $processed{usage} and $params->{help_flag} )
Branch: 1 branch
BranchTrueFalseTotal
if ($processed{"usage"} and $params->{"help_flag"}) 6 93 99
Condition100%3/3
$processed{"usage"} and $params->{"help_flag"}
!ll&&!rl&&r
23706
MC/DC100%2/2
$processed{"usage"} and $params->{"help_flag"}
$processed{"usage"} $params->{"help_flag"}
Truth table100%3/3
A and B
A= $processed{"usage"}B= $params->{"help_flag"}
ABresult
0X0
100
111
67 {
68 6 $class->_getopt_full_usage($processed{usage});
69 }
70  
71 $class->new(
72 ARGV => $processed{argv_copy},
73 extra_argv => $processed{argv},
74 93 ▶ ( $processed{usage} ? ( usage => $processed{usage} ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
$processed{'usage'} ? : 70 23 93
75 %$constructor_params, # explicit params to ->new
76 %$params, # params from CLI
77 );
78 }
79  
80 23 ▶sub _getopt_spec { shift->_traditional_spec(@_); }
Subroutine _getopt_spec: called CC=1 SCAR=0.0
81  
82 sub _parse_argv {
83 109 ▶ my ( $class, %params ) = @_;
Subroutine _parse_argv: called CC=5 SCAR=16.1
84  
85 109 ▶ local @ARGV = @{ $params{params}{argv} || \@ARGV };
Branch: 1 branch
BranchTrueFalseTotal
$params{"params"}{"argv"} or \@ARGV 87 22 109
86  
87 109 my ( $opt_spec, $name_to_init_arg ) = $class->_getopt_spec(%params);
88  
89 # Get a clean copy of the original @ARGV
90 109 my $argv_copy = [ @ARGV ];
91  
92 109 my @warnings;
93 109 my ( $parsed_options, $usage ) = eval {
94 109 ▶ local $SIG{__WARN__} = sub { push @warnings, @_ };
Subroutine __ANON__: called CC=1 SCAR=0.0
95  
96 109 return $class->_getopt_get_options(\%params, $opt_spec);
97 };
98 109 my $e = $@;
99  
100 109 ▶ $class->_getopt_spec_warnings(@warnings) if @warnings;
Branch: 1 branch
BranchTrueFalseTotal
if @warnings 13 96 109
101 109 ▶ $class->_getopt_spec_exception(\@warnings, $e) if $e;
Branch: 1 branch
BranchTrueFalseTotal
if $e 10 99 109
102  
103 # Get a copy of the Getopt::Long-mangled @ARGV
104 99 my $argv_mangled = [ @ARGV ];
105  
106 my %constructor_args = (
107 map {
108 99 $name_to_init_arg->{$_} => $parsed_options->{$_}
109 } keys %$parsed_options,
110 );
111  
112 return (
113 99 ▶ params => \%constructor_args,
Branch: 1 branch
BranchTrueFalseTotal
defined $usage ? : 76 23 99
114 argv_copy => $argv_copy,
115 argv => $argv_mangled,
116 ( defined($usage) ? ( usage => $usage ) : () ),
117 );
118 }
119  
120 sub _getopt_get_options {
121 23 ▶ my ($class, $params, $opt_spec) = @_;
Subroutine _getopt_get_options: called CC=1 SCAR=0.0
122 23 my %options;
123 23 Getopt::Long::GetOptions(\%options, @$opt_spec);
124 23 return ( \%options, undef );
125 }
126  
127 ▶sub _getopt_spec_warnings { }
Subroutine _getopt_spec_warnings: called CC=1 SCAR=0.0
128  
129 sub _getopt_spec_exception {
130 10 ▶ my ($self, $warnings, $exception) = @_;
Subroutine _getopt_spec_exception: called CC=1 SCAR=0.0
131 10 die @$warnings, $exception;
132 }
133  
134 sub _getopt_full_usage {
135 6 ▶ my ($self, $usage) = @_;
Subroutine _getopt_full_usage: called CC=1 SCAR=0.0
136 6 $usage->die;
137 }
138  
139 sub _usage_format {
140 86 ▶ return "usage: %c %o";
Subroutine _usage_format: called CC=1 SCAR=0.0
141 }
142  
143 sub _traditional_spec {
144 23 ▶ my ( $class, %params ) = @_;
Subroutine _traditional_spec: called CC=2 SCAR=6.9
145  
146 23 my ( @options, %name_to_init_arg, %options );
147  
148 23 foreach my $opt ( @{ $params{options} } ) {
149 119 push @options, $opt->{opt_string};
150  
151 119 my $identifier = $opt->{name};
152 119 $identifier =~ s/\W/_/g; # Getopt::Long does this to all option names
153  
154 119 $name_to_init_arg{$identifier} = $opt->{init_arg};
155 }
156  
157 23 return ( \@options, \%name_to_init_arg );
158 }
159  
160 sub _compute_getopt_attrs {
161 109 ▶ my $class = shift;
Subroutine _compute_getopt_attrs: called CC=2 SCAR=6.9
162 915 sort { $a->insertion_order <=> $b->insertion_order }
163 grep {
164 610 ▶ $_->does("MouseX::Getopt::Meta::Attribute::Trait")
Branch: 1 branch
BranchTrueFalseTotal
$_->does("MouseX::Getopt::Meta::Attribute::Trait") or not $_->name =~ /^_/ 291 319 610
165 or
166 $_->name !~ /^_/
167 } grep {
168 109 !$_->does('MouseX::Getopt::Meta::Attribute::Trait::NoGetopt')
169 } $class->meta->get_all_attributes
170 }
171  
172 sub _get_cmd_flags_for_attr {
173 556 ▶ my ( $class, $attr ) = @_;
Subroutine _get_cmd_flags_for_attr: called CC=4 SCAR=13.9
174  
175 556 my $flag = $attr->name;
176  
177 556 my @aliases;
178  
179 556 ▶ if ($attr->does('MouseX::Getopt::Meta::Attribute::Trait')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr->does("MouseX::Getopt::Meta::Attribute::Trait")) 319 237 556
180 319 ▶ $flag = $attr->cmd_flag if $attr->has_cmd_flag;
Branch: 1 branch
BranchTrueFalseTotal
if $attr->has_cmd_flag 226 93 319
181 319 ▶ @aliases = @{ $attr->cmd_aliases } if $attr->has_cmd_aliases;
Branch: 1 branch
BranchTrueFalseTotal
if $attr->has_cmd_aliases 223 96 319
182 }
183  
184 556 return ( $flag, @aliases );
185 }
186  
187 sub _attrs_to_options {
188 109 ▶ my $class = shift;
Subroutine _attrs_to_options: called CC=10 SCAR=23.2
189 109 ▶ my $config_from_file = shift || {};
Condition100%2/2
shift() || {}
l!l
8101
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
190  
191 109 my @options;
192  
193 109 foreach my $attr ($class->_compute_getopt_attrs) {
194 556 my ( $flag, @aliases ) = $class->_get_cmd_flags_for_attr($attr);
195  
196 556 my $opt_string = join(q{|}, $flag, @aliases);
197  
198 556 ▶ if ($attr->name eq 'configfile') {
Branch: 2 branches
BranchTrueFalseTotal
if ($attr->name eq 'configfile') { } 11 545 556
elsif ($attr->has_type_constraint) { } 545 0 545
199 11 $opt_string .= '=s';
200 }
201 elsif ($attr->has_type_constraint) {
202 545 my $type = $attr->type_constraint;
203 545 ▶ if (MouseX::Getopt::OptionTypeMap->has_option_type($type)) {
Branch: 1 branch
BranchTrueFalseTotal
if ("MouseX::Getopt::OptionTypeMap"->has_option_type($type)) 545 0 545
204 545 $opt_string .= MouseX::Getopt::OptionTypeMap->get_option_type($type)
205 }
206 }
207  
208 push @options, {
209 name => $flag,
210 init_arg => $attr->init_arg,
211 opt_string => $opt_string,
212 556 ▶ required => $attr->is_required && !$attr->has_default && !$attr->has_builder && !exists $config_from_file->{$attr->name},
Branch: 1 branch
BranchTrueFalseTotal
$attr->has_documentation ? : 93 463 556
Condition77%7/9
$attr->is_required && !$attr->has_default && !$attr->has_builder && !exists($$config_from_file{$attr->name})
!ll&&!rl&&r
536713
$attr->is_required and not $attr->has_default and not $attr->has_builder
!ll&&!rl&&r
536020
$attr->is_required and not $attr->has_default
!ll&&!rl&&r
536020
MC/DC50%2/4
$attr->is_required && !$attr->has_default && !$attr->has_builder && !exists($$config_from_file{$attr->name})
$attr->is_required not $attr->has_default not $attr->has_builder !exists($$config_from_file{$attr->name})
Truth table60%3/5
A and not(B) and not(C) && not(D)
A= $attr->is_requiredB= not $attr->has_defaultC= not $attr->has_builderD= !exists($$config_from_file{$attr->name})
ABCDresult
0XXX0
10XX0
110X0
11100
11111
213 # NOTE:
214 # this "feature" was breaking because
215 # Getopt::Long::Descriptive would return
216 # the default value as if it was a command
217 # line flag, which would then override the
218 # one passed into a constructor.
219 # See 100_gld_default_bug.t for an example
220 # - SL
221 #( ( $attr->has_default && ( $attr->is_default_a_coderef xor $attr->is_lazy ) ) ? ( default => $attr->default({}) ) : () ),
222 ( $attr->has_documentation ? ( doc => $attr->documentation ) : () ),
223 }
224 }
225  
226 109 return @options;
227 }
228  
229 22 ▶no Mouse::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
230 22 1;
231  
232 =head1 SYNOPSIS
233  
234 ## In your class
235 package My::App;
236 use Mouse;
237  
238 with 'MouseX::Getopt::Basic';
239  
240 has 'out' => (is => 'rw', isa => 'Str', required => 1);
241 has 'in' => (is => 'rw', isa => 'Str', required => 1);
242  
243 # ... rest of the class here
244  
245 ## in your script
246 #!/usr/bin/perl
247  
248 use My::App;
249  
250 my $app = My::App->new_with_options();
251 # ... rest of the script here
252  
253 ## on the command line
254 % perl my_app_script.pl --in file.input --out file.dump
255  
256 =head1 DESCRIPTION
257  
258 This is like L<MouseX::Getopt> and can be used instead except that it
259 doesn't make use of L<Getopt::Long::Descriptive> (or "GLD" for short).
260  
261 =over 4
262  
263 =item new_with_options
264  
265 See L<MouseX::Getopt/new_with_options>.
266  
267 =back
268  
269 =cut
↑ Summary blib/lib/MouseX/Getopt/OptionTypeMap.pm »