Getopt/Basic.pm

filter: Statementstmt 99.2% 136 / 137 Branchbran 84.0% 42 / 50 Conditioncond 80.0% 20 / 25 MC/DCmcdc 64.2% 9 / 14 Subroutinesub 93.1% 27 / 29 Podpod 66.6% 2 / 3 totaltotal 91.4% 236 / 258 CC 37 SCAR 20.1
CC
37
Cov
93%
process_argv
17.7
_attrs_to_options
10.1
__ANON__
6.0
CRAP 7.4
« Getopt/OptionTypeMap.pm ↑ Summary Getopt/GLD.pm »
1 package MooseX::Getopt::Basic;
2 # ABSTRACT: MooseX::Getopt::Basic - role to implement the Getopt::Long functionality
3  
4 27 our $VERSION = '0.78';
5  
6 27 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 27 ▶use MooseX::Getopt::OptionTypeMap;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 27 ▶use MooseX::Getopt::Meta::Attribute;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 27 ▶use MooseX::Getopt::Meta::Attribute::NoGetopt;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 27 ▶use MooseX::Getopt::ProcessedArgv;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 27 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 27 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 27 ▶use Getopt::Long 2.37 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 27 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 27 has ARGV => (is => 'rw', isa => 'ArrayRef', traits => ['NoGetopt']);
19 27 has extra_argv => (is => 'rw', isa => 'ArrayRef', traits => ['NoGetopt']);
20  
21 113 ▶sub process_argv {
Subroutine process_argv: called CC=17 SCAR=28.7
Pod documented
22 113 my ($class, @params) = @_;
23  
24 113 ▶ my $constructor_params = ( @params == 1 ? $params[0] : {@params} );
Branch: 1 branch
BranchTrueFalseTotal
@params == 1 ? : 13 100 113
25  
26 113 my $config_from_file;
27 113 ▶ if($class->meta->does_role('MooseX::ConfigFromFile')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->meta->does_role("MooseX::ConfigFromFile")) 14 99 113
28 14 local @ARGV = @ARGV;
29  
30 # just get the configfile arg now out of @ARGV; the rest of the args
31 # will be fetched later
32 14 my $configfile;
33 14 my $opt_parser = Getopt::Long::Parser->new( config => [ qw( no_auto_help pass_through no_auto_version ) ] );
34 14 $opt_parser->getoptions( "configfile=s" => \$configfile );
35  
36 14 my $cfmeta = $class->meta->find_attribute_by_name('configfile');
37 14 my $init_arg = $cfmeta->init_arg;
38  
39 # was it passed to the constructor?
40 14 ▶ if (!defined $configfile)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $configfile) 8 6 14
41 {
42 8 ▶ $configfile = $constructor_params->{$init_arg} if defined $init_arg;
Branch: 1 branch
BranchTrueFalseTotal
if defined $init_arg 8 0 8
43 }
44  
45 14 ▶ if(!defined $configfile) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $configfile) { } 7 7 14
46 # this is a classic legacy usecase documented in
47 # MooseX::ConfigFromFile that we should continue to support
48 7 ▶ $configfile = try { $class->configfile };
Subroutine __ANON__: called CC=1 SCAR=0.0
49  
50 7 ▶ $configfile = $cfmeta->default
Branch: 1 branch
BranchTrueFalseTotal
if not defined $configfile and $cfmeta->has_default 7 0 7
Condition33%1/3
not defined $configfile and $cfmeta->has_default
!ll&&!rl&&r
007
MC/DC0%0/2
not defined $configfile and $cfmeta->has_default
not defined $configfile $cfmeta->has_default
Truth table33%1/3
not(A) and B
A= not defined $configfileB= $cfmeta->has_default
ABresult
0X0
100
111
51 if not defined $configfile and $cfmeta->has_default;
52  
53 # note that this will die horribly if the default sub depends on
54 # other attributes
55 7 ▶ $configfile = $configfile->($class) if ref $configfile eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $configfile eq "CODE" 6 1 7
56 7 ▶ if (defined $configfile) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $configfile) 4 3 7
57 4 ▶ $config_from_file = try {
Subroutine __ANON__: called CC=1 SCAR=0.0
58 4 $class->get_config_from_file($configfile);
59 }
60 0 ▶ catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
61 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
62 4 };
63 }
64  
65 7 ▶ $constructor_params->{$init_arg} = $configfile
Branch: 1 branch
BranchTrueFalseTotal
if defined $configfile and defined $init_arg 4 3 7
Condition66%2/3
defined $configfile and defined $init_arg
!ll&&!rl&&r
304
MC/DC50%1/2
defined $configfile and defined $init_arg
defined $configfile defined $init_arg
Truth table66%2/3
A and B
A= defined $configfileB= defined $init_arg
ABresult
0X0
100
111
66 if defined $configfile and defined $init_arg;
67 }
68 else {
69 7 $config_from_file = $class->get_config_from_file($configfile);
70 }
71 }
72  
73 113 ▶ Carp::croak("Single parameters to new_with_options() must be a HASH ref")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $constructor_params eq "HASH" 0 113 113
74 unless ref($constructor_params) eq 'HASH';
75  
76 113 ▶ my %processed = $class->_parse_argv(
Branch: 1 branch
BranchTrueFalseTotal
$config_from_file ? : 11 102 113
77 options => [
78 $class->_attrs_to_options( $config_from_file )
79 ],
80 params => $config_from_file ? { %$config_from_file, %$constructor_params } : $constructor_params,
81 );
82  
83 103 ▶ my $params = $config_from_file ? { %$config_from_file, %{$processed{params}} } : $processed{params};
Branch: 1 branch
BranchTrueFalseTotal
$config_from_file ? : 10 93 103
84  
85 # did the user request usage information?
86 103 ▶ if ( $processed{usage} and $params->{help_flag} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($processed{"usage"} and $params->{"help_flag"}) 10 93 103
Condition100%3/3
$processed{"usage"} and $params->{"help_flag"}
!ll&&!rl&&r
187510
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
87 10 $class->print_usage_text($processed{usage});
88 9 exit 0;
89 }
90  
91 return MooseX::Getopt::ProcessedArgv->new(
92 argv_copy => $processed{argv_copy},
93 extra_argv => $processed{argv},
94 usage => $processed{usage},
95 93 constructor_params => $constructor_params, # explicit params to ->new
96 cli_params => $params, # params from CLI
97 );
98 }
99  
100 112 ▶sub new_with_options {
Subroutine new_with_options: called CC=3 SCAR=11.0
Pod documented
101 112 my ($class, @params) = @_;
102  
103 112 my $pa = $class->process_argv(@params);
104  
105 # $pa->constructor_params contains everything passed to new_with_options,
106 # so it may contain the "argv" key, which may not exist on the class
107 92 my %constructor_params = %{ $pa->constructor_params };
108 92 ▶ delete $constructor_params{argv} if (not $class->meta->find_attribute_by_name('argv'));
Branch: 1 branch
BranchTrueFalseTotal
unless $class->meta->find_attribute_by_name("argv") 92 0 92
109  
110 $class->new(
111 ARGV => $pa->argv_copy,
112 extra_argv => $pa->extra_argv,
113 ( $pa->usage ? ( usage => $pa->usage ) : () ),
114 %constructor_params, # explicit params to ->new
115 92 ▶ %{ $pa->cli_params }, # params from CLI
Branch: 1 branch
BranchTrueFalseTotal
$pa->usage ? : 74 18 92
116 );
117 }
118  
119 18 ▶sub _getopt_spec { shift->_traditional_spec(@_); }
Subroutine _getopt_spec: called CC=1 SCAR=0.0
120  
121 113 ▶sub _parse_argv {
Subroutine _parse_argv: called CC=4 SCAR=13.9
122 113 my ( $class, %params ) = @_;
123  
124 113 ▶ local @ARGV = @{ $params{params}{argv} || \@ARGV };
Condition100%2/2
$params{'params'}{'argv'} || \@ARGV
l!l
1697
MC/DC100%1/1
$params{'params'}{'argv'} || \@ARGV
$params{'params'}{'argv'}
Truth table100%2/2
A
A= $params{'params'}{'argv'}
Aresult
11
00
125  
126 113 my ( $opt_spec, $name_to_init_arg ) = $class->_getopt_spec(%params);
127  
128 # Get a clean copy of the original @ARGV
129 113 my $argv_copy = [ @ARGV ];
130  
131 113 my @warnings;
132 113 ▶ my ( $parsed_options, $usage ) = try {
Subroutine __ANON__: called CC=1 SCAR=0.0
133 113 ▶ local $SIG{__WARN__} = sub { push @warnings, @_ };
Subroutine __ANON__: called CC=1 SCAR=0.0
134  
135 113 return $class->_getopt_get_options(\%params, $opt_spec);
136 }
137 10 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
138 10 $class->_getopt_spec_exception(\@warnings, $_);
139 113 };
140  
141 103 ▶ $class->_getopt_spec_warnings(@warnings) if @warnings;
Branch: 1 branch
BranchTrueFalseTotal
if @warnings 0 103 103
142  
143 # Get a copy of the Getopt::Long-mangled @ARGV
144 103 my $argv_mangled = [ @ARGV ];
145  
146 my %constructor_args = (
147 map {
148 103 $name_to_init_arg->{$_} => $parsed_options->{$_}
149 } keys %$parsed_options,
150 );
151  
152 return (
153 103 ▶ params => \%constructor_args,
Branch: 1 branch
BranchTrueFalseTotal
defined $usage ? : 85 18 103
154 argv_copy => $argv_copy,
155 argv => $argv_mangled,
156 ( defined($usage) ? ( usage => $usage ) : () ),
157 );
158 }
159  
160 18 ▶sub _getopt_get_options {
Subroutine _getopt_get_options: called CC=1 SCAR=0.0
161 18 my ($class, $params, $opt_spec) = @_;
162 18 my %options;
163 18 Getopt::Long::GetOptions(\%options, @$opt_spec);
164 18 return ( \%options, undef );
165 }
166  
167 0 ▶sub _getopt_spec_warnings { }
Subroutine _getopt_spec_warnings: not called CC=1 SCAR=0.0
168  
169 10 ▶sub _getopt_spec_exception {
Subroutine _getopt_spec_exception: called CC=1 SCAR=0.0
170 10 my ($self, $warnings, $exception) = @_;
171 10 die @$warnings, $exception;
172 }
173  
174 # maintained for backwards compatibility only
175 sub _getopt_full_usage
176 9 ▶{
Subroutine _getopt_full_usage: called CC=1 SCAR=0.0
177 9 my ($self, $usage) = @_;
178 9 print $usage->text;
179 }
180 #(this is already documented in MooseX::Getopt. But FIXME later, via RT#82195)
181 #pod =for Pod::Coverage
182 #pod print_usage_text
183 #pod =cut
184 9 ▶sub print_usage_text { shift->_getopt_full_usage(@_) }
Subroutine print_usage_text: called CC=1 SCAR=0.0
Pod undocumented
185  
186 95 ▶sub _usage_format {
Subroutine _usage_format: called CC=1 SCAR=0.0
187 95 return "usage: %c %o";
188 }
189  
190 18 ▶sub _traditional_spec {
Subroutine _traditional_spec: called CC=2 SCAR=6.9
191 18 my ( $class, %params ) = @_;
192  
193 18 my ( @options, %name_to_init_arg );
194  
195 18 foreach my $opt ( @{ $params{options} } ) {
196 109 push @options, $opt->{opt_string};
197  
198 109 my $identifier = $opt->{name};
199 109 $identifier =~ s/\W/_/g; # Getopt::Long does this to all option names
200  
201 109 $name_to_init_arg{$identifier} = $opt->{init_arg};
202 }
203  
204 18 return ( \@options, \%name_to_init_arg );
205 }
206  
207 113 ▶sub _compute_getopt_attrs {
Subroutine _compute_getopt_attrs: called CC=2 SCAR=6.9
208 113 my $class = shift;
209 1113 sort { $a->insertion_order <=> $b->insertion_order }
210 grep {
211 639 ▶ $_->does("MooseX::Getopt::Meta::Attribute::Trait")
Condition100%3/3
$_->does('MooseX::Getopt::Meta::Attribute::Trait') || !($_->init_arg =~ /^_/)
l!l&&r!l&&!r
32327739
MC/DC100%2/2
$_->does('MooseX::Getopt::Meta::Attribute::Trait') || !($_->init_arg =~ /^_/)
$_->does('MooseX::Getopt::Meta::Attribute::Trait') !($_->init_arg =~ /^_/)
Truth table100%3/3
A || not(B)
A= $_->does('MooseX::Getopt::Meta::Attribute::Trait')B= !($_->init_arg =~ /^_/)
ABresult
1X1
011
000
212 or
213 $_->init_arg !~ /^_/
214 } grep {
215 640 defined $_->init_arg
216 } grep {
217 113 !$_->does('MooseX::Getopt::Meta::Attribute::Trait::NoGetopt')
218 } $class->meta->get_all_attributes
219 }
220  
221 585 ▶sub _get_cmd_flags_for_attr {
Subroutine _get_cmd_flags_for_attr: called CC=4 SCAR=13.9
222 585 my ( $class, $attr ) = @_;
223  
224 585 my $flag = $attr->init_arg;
225  
226 585 my @aliases;
227  
228 585 ▶ if ($attr->does('MooseX::Getopt::Meta::Attribute::Trait')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr->does("MooseX::Getopt::Meta::Attribute::Trait")) 323 262 585
229 323 ▶ $flag = $attr->cmd_flag if $attr->has_cmd_flag;
Branch: 1 branch
BranchTrueFalseTotal
if $attr->has_cmd_flag 235 88 323
230 323 ▶ @aliases = @{ $attr->cmd_aliases } if $attr->has_cmd_aliases;
Branch: 1 branch
BranchTrueFalseTotal
if $attr->has_cmd_aliases 214 109 323
231 }
232  
233 585 return ( $flag, @aliases );
234 }
235  
236 113 ▶sub _attrs_to_options {
Subroutine _attrs_to_options: called CC=10 SCAR=23.1
237 113 my $class = shift;
238 113 ▶ my $config_from_file = shift || {};
Condition100%2/2
shift() || {}
l!l
11102
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
239  
240 113 my @options;
241  
242 113 foreach my $attr ($class->_compute_getopt_attrs) {
243 585 my ( $flag, @aliases ) = $class->_get_cmd_flags_for_attr($attr);
244  
245 585 my $opt_string = join(q{|}, $flag, @aliases);
246  
247 585 ▶ if ($attr->name eq 'configfile') {
Branch: 2 branches
BranchTrueFalseTotal
if ($attr->name eq 'configfile') { } 14 571 585
elsif ($attr->has_type_constraint) { } 475 96 571
248 14 $opt_string .= '=s';
249 }
250 elsif ($attr->has_type_constraint) {
251 475 my $type = $attr->type_constraint;
252 475 ▶ if (MooseX::Getopt::OptionTypeMap->has_option_type($type)) {
Branch: 1 branch
BranchTrueFalseTotal
if ("MooseX::Getopt::OptionTypeMap"->has_option_type($type)) 475 0 475
253 475 $opt_string .= MooseX::Getopt::OptionTypeMap->get_option_type($type)
254 }
255 }
256  
257 my $push = {
258 name => $flag,
259 init_arg => $attr->init_arg,
260 opt_string => $opt_string,
261 585 ▶ required => $attr->is_required && !$attr->has_default && !$attr->has_builder && !exists $config_from_file->{$attr->name},
Condition77%7/9
$attr->is_required && !$attr->has_default && !$attr->has_builder && !exists($$config_from_file{$attr->name})
!ll&&!rl&&r
558918
$attr->is_required and not $attr->has_default and not $attr->has_builder
!ll&&!rl&&r
558027
$attr->is_required and not $attr->has_default
!ll&&!rl&&r
558027
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
262 # NOTE:
263 # this "feature" was breaking because
264 # Getopt::Long::Descriptive would return
265 # the default value as if it was a command
266 # line flag, which would then override the
267 # one passed into a constructor.
268 # See 100_gld_default_bug.t for an example
269 # - SL
270 #( ( $attr->has_default && ( $attr->is_default_a_coderef xor $attr->is_lazy ) ) ? ( default => $attr->default({}) ) : () ),
271 };
272 585 ▶ if ($attr->has_documentation) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr->has_documentation) 105 480 585
273 105 my $doc = $attr->documentation;
274 105 $doc =~ s/[\r\n]+/ /g;
275 105 $push->{doc} = $doc;
276 }
277 585 push @options, $push;
278 }
279  
280 113 return @options;
281 }
282  
283 27 1;
284  
285 __END__
« Getopt/OptionTypeMap.pm ↑ Summary Getopt/GLD.pm »