blib/lib/CLI/Osprey.pm

filter: Statementstmt 97.0% 100 / 103 Branchbran 72.2% 13 / 18 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 23 / 23 Podpod n/a totaltotal 93.2% 139 / 149 CC 13 SCAR 9.6
CC
13
Cov
93%
import
6.0
_option_attributes
4.0
__ANON__
4.0
CRAP 2.6
« blib/lib/CLI/Osprey/Descriptive/Usage.pm ↑ Summary blib/lib/CLI/Osprey/InlineSubcommand.pm »
1 package CLI::Osprey;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # ABSTRACT: MooX::Options + MooX::Cmd + Sanity
6 4 our $VERSION = '0.08'; # VERSION
7 4 our $AUTHORITY = 'cpan:ARODLAND'; # AUTHORITY
8  
9 4 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Module::Runtime 'use_module';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 ▶use Moo::Role qw(); # only want class methods, not setting up a role
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 4 ▶use CLI::Osprey::InlineSubcommand ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 4 my @OPTIONS_ATTRIBUTES = qw(
18 option option_name format short repeatable negatable spacer_before spacer_after doc long_doc format_doc order hidden
19 );
20  
21 sub import {
22 8 ▶ my (undef, @import_options) = @_;
Subroutine import: called CC=6 SCAR=18.0
23 8 my $target = caller;
24  
25 8 for my $method (qw(with around has)) {
26 24 ▶ next if $target->can($method);
Branch: 1 branch
BranchTrueFalseTotal
if $target->can($method) 24 0 24
27 0 croak "Can't find the method '$method' in package '$target'. CLI::Osprey requires a Role::Tiny-compatible object system like Moo or Moose.";
28 }
29  
30 8 my $with = $target->can('with');
31 8 my $around = $target->can('around');
32 8 my $has = $target->can('has');
33  
34 8 ▶ if ( ! Moo::Role->is_role( $target ) ) { # not in a role
Branch: 1 branch
BranchTrueFalseTotal
unless ("Moo::Role"->is_role($target)) 8 0 8
35 8 ▶ eval "package $target;\n" . q{
Subroutine _osprey_config: called CC=1 SCAR=0.0
Subroutine _osprey_config: called CC=1 SCAR=0.0
Subroutine _osprey_options: called CC=1 SCAR=0.0
Subroutine _osprey_options: called CC=1 SCAR=0.0
Subroutine _osprey_subcommands: called CC=1 SCAR=0.0
Subroutine _osprey_subcommands: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
eval "package $target;\n" . "\n sub _osprey_options {\n my \$class = shift;\n return \$class->maybe::next::method(\@_);\n }\n\n sub _osprey_config {\n my \$class = shift;\n return \$class->maybe::next::method(\@_);\n }\n\n sub _osprey_subcommands {\n my \$class = shift;\n return \$class->maybe::next::method(\@_);\n }\n 1;\n " or croak($@) 0 8 8
36 sub _osprey_options {
37 my $class = shift;
38 return $class->maybe::next::method(@_);
39 }
40  
41 sub _osprey_config {
42 my $class = shift;
43 return $class->maybe::next::method(@_);
44 }
45  
46 sub _osprey_subcommands {
47 my $class = shift;
48 return $class->maybe::next::method(@_);
49 }
50 1;
51 } || croak($@);
52 }
53  
54 8 my $osprey_config = {
55 preserve_argv => 1,
56 abbreviate => 1,
57 prefer_commandline => 1,
58 @import_options,
59 };
60  
61 $around->(_osprey_config => sub {
62 32 ▶ my ($orig, $self) = (shift, shift);
Subroutine __ANON__: called CC=1 SCAR=0.0
63 32 return $self->$orig(@_), %$osprey_config;
64 8 });
65  
66 8 my $options_data = { };
67 8 my $subcommands = { };
68  
69 my $apply_modifiers = sub {
70 16 ▶ return if $target->can('new_with_options');
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if $target->can("new_with_options") 8 8 16
71 8 $with->('CLI::Osprey::Role');
72 $around->(_osprey_options => sub {
73 19 ▶ my ($orig, $self) = (shift, shift);
Subroutine __ANON__: called CC=1 SCAR=0.0
74 19 return $self->$orig(@_), %$options_data;
75 8 });
76 $around->(_osprey_subcommands => sub {
77 32 ▶ my ($orig, $self) = (shift, shift);
Subroutine __ANON__: called CC=1 SCAR=0.0
78 32 return $self->$orig(@_), %$subcommands;
79 8 });
80 8 };
81  
82 8 my $added_order = 0;
83  
84 my $option = sub {
85 2 ▶ my ($name, %attributes) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
86  
87 2 $has->($name => _non_option_attributes(%attributes));
88 2 $options_data->{$name} = _option_attributes($name, %attributes);
89 2 $options_data->{$name}{added_order} = ++$added_order;
90 2 $apply_modifiers->();
91 8 };
92  
93 my $subcommand = sub {
94 6 ▶ my ($name, $subobject) = @_;
Subroutine __ANON__: called CC=4 SCAR=13.9
95  
96 6 ▶ if (ref($subobject) && reftype($subobject) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $subobject and builtin::reftype($subobject) eq 'CODE') { } 1 5 6
Condition66%2/3
ref $subobject and builtin::reftype($subobject) eq "CODE"
!ll&&!rl&&r
501
MC/DC50%1/2
ref $subobject and builtin::reftype($subobject) eq "CODE"
ref $subobject builtin::reftype($subobject) eq "CODE"
Truth table66%2/3
A and B
A= ref $subobjectB= builtin::reftype($subobject) eq "CODE"
ABresult
0X0
100
111
97 1 my @args = @_[2 .. $#_];
98 1 $subobject = CLI::Osprey::InlineSubcommand->new(
99 name => $name,
100 method => $subobject,
101 @args,
102 );
103 }
104 else {
105 5 ▶ use_module($subobject) unless $osprey_config->{on_demand};
Branch: 1 branch
BranchTrueFalseTotal
unless $osprey_config->{"on_demand"} 3 2 5
106 }
107  
108 6 $subcommands->{$name} = $subobject;
109 6 $apply_modifiers->();
110 8 };
111  
112 8 ▶ if (my $info = $Role::Tiny::INFO{$target}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $info = $Role::Tiny::INFO{$target}) 0 8 8
113 0 $info->{not_methods}{$option} = $option;
114 0 $info->{not_methods}{$subcommand} = $subcommand;
115 }
116  
117 {
118 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
119 8 *{"${target}::option"} = $option;
120 8 *{"${target}::subcommand"} = $subcommand;
121 }
122  
123 8 $apply_modifiers->();
124  
125 8 return;
126 }
127  
128 sub _non_option_attributes {
129 2 ▶ my (%attributes) = @_;
Subroutine _non_option_attributes: called CC=1 SCAR=0.0
130 2 my %filter_out;
131 2 @filter_out{@OPTIONS_ATTRIBUTES} = ();
132 return map {
133 4 $_ => $attributes{$_}
134 } grep {
135 2 !exists $filter_out{$_}
136 } keys %attributes;
137 }
138  
139 sub _option_attributes {
140 2 ▶ my ($name, %attributes) = @_;
Subroutine _option_attributes: called CC=4 SCAR=13.9
141  
142 2 ▶ unless (defined $attributes{option}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $attributes{"option"}) 2 0 2
143 2 ($attributes{option} = $name) =~ tr/_/-/;
144 }
145 2 my $ret = {};
146 2 for (@OPTIONS_ATTRIBUTES) {
147 26 ▶ $ret->{$_} = $attributes{$_} if exists $attributes{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $attributes{$_} 6 20 26
148 }
149 2 return $ret;
150 }
151  
152 4 1;
153  
154 __END__
« blib/lib/CLI/Osprey/Descriptive/Usage.pm ↑ Summary blib/lib/CLI/Osprey/InlineSubcommand.pm »