CLI/Role/ArgumentSet.pm

filter: Statementstmt 100% 49 / 49 Branchbran 66.6% 4 / 6 Conditioncond 75.0% 6 / 8 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 100% 13 / 13 Podpod 100% 5 / 5 totaltotal 93.0% 80 / 86 CC 9 SCAR 9.4
CC
9
Cov
94%
_trigger__arguments
5.6
add_argument
3.0
arguments
2.0
CRAP 2.6
« CLI/Argument/String.pm ↑ Summary CLI/Argument/Enum.pm »
1 #=============================================================================
2 #
3 # Module: Term::CLI::ArgumentSet
4 #
5 # Description: Role for sets of arguments in Term::CLI
6 #
7 # Author: Steven Bakker (SBAKKER), <cpanman@monkey-mind.net>
8 # Created: 05/02/18
9 #
10 # Copyright (c) 2018-2022 Steven Bakker
11 #
12 # This module is free software; you can redistribute it and/or modify
13 # it under the same terms as Perl itself. See "perldoc perlartistic."
14 #
15 # This software is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 #
19 #=============================================================================
20  
21 package Term::CLI::Role::ArgumentSet 0.061001;
22  
23 7 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 7 ▶use Scalar::Util qw( reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 7 ▶use Types::Standard 1.000005 qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
29 ArrayRef
30 InstanceOf
31 Maybe
32 7 );
33  
34 7 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 7 ▶use namespace::clean 0.25;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 has _arguments => (
38 is => 'rw',
39 writer => '_set_arguments',
40 trigger => 1,
41 init_arg => 'arguments',
42 isa => Maybe [ ArrayRef [ InstanceOf ['Term::CLI::Argument'] ] ],
43 28 ▶ coerce => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
44  
45 # Copy the array, so the reference we store becomes
46 # "internal", preventing accidental modification
47 # from the outside.
48 28 return [ @{ $_[0] } ];
49 },
50 7 );
51  
52 # $self->_set_arguments($ref) => $self->_trigger__arguments($ref);
53 #
54 # Trigger to run whenever the object's _commands array ref is set.
55 #
56 27 ▶sub _trigger__arguments { ## no critic (ProhibitUnusedPrivateSubroutines)
Subroutine _trigger__arguments: called CC=5 SCAR=17.2
57 27 my ( $self, $arg_list ) = @_;
58  
59 27 ▶ return if !$arg_list;
Branch: 1 branch
BranchTrueFalseTotal
unless $arg_list 0 27 27
60  
61 # Set the parent for each argument object.
62 27 ▶ if (ref $arg_list && reftype $arg_list eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg_list and builtin::reftype($arg_list) eq "ARRAY") 27 0 27
Condition33%1/3
ref $arg_list and builtin::reftype($arg_list) eq "ARRAY"
!ll&&!rl&&r
0027
MC/DC0%0/2
ref $arg_list and builtin::reftype($arg_list) eq "ARRAY"
ref $arg_list builtin::reftype($arg_list) eq "ARRAY"
Truth table33%1/3
A and B
A= ref $arg_listB= builtin::reftype($arg_list) eq "ARRAY"
ABresult
0X0
100
111
63 27 for my $arg_obj ( @{$arg_list} ) {
64 30 $arg_obj->_set_parent($self);
65 }
66 }
67 27 return;
68 }
69  
70 94 ▶sub arguments {
Subroutine arguments: called CC=2 SCAR=6.9
Pod documented
71 94 my ($self) = @_;
72 94 ▶ return @{ $self->_arguments // [] };
Condition100%2/2
$self->_arguments // []
l!l
904
MC/DC100%1/1
$self->_arguments // []
$self->_arguments
Truth table100%2/2
A
A= $self->_arguments
Aresult
11
00
73 }
74  
75 1 ▶sub set_arguments {
Subroutine set_arguments: called CC=1 SCAR=0.0
Pod documented
76 1 my ( $self, @arguments ) = @_;
77  
78 1 $self->_set_arguments( [] );
79 1 $self->add_argument(@arguments);
80 1 return;
81 }
82  
83 123 ▶sub has_arguments {
Subroutine has_arguments: called CC=2 SCAR=6.9
Pod documented
84 123 my ($self) = @_;
85 123 ▶ return ( $self->_arguments and scalar @{ $self->_arguments } > 0 );
Condition100%3/3
$self->_arguments && scalar @{$self->_arguments;} > 0
!ll&&!rl&&r
33189
MC/DC100%2/2
$self->_arguments && scalar @{$self->_arguments;} > 0
$self->_arguments scalar @{$self->_arguments;} > 0
Truth table100%3/3
A && B
A= $self->_argumentsB= scalar @{$self->_arguments;} > 0
ABresult
0X0
100
111
86 }
87  
88 4 ▶sub add_argument {
Subroutine add_argument: called CC=3 SCAR=11.0
Pod documented
89 4 my ( $self, @arguments ) = @_;
90  
91 4 ▶ if ( !$self->_arguments ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_arguments) 1 3 4
92 1 $self->_set_arguments( [] );
93 }
94  
95 4 for my $arg (@arguments) {
96 5 $arg->_set_parent($self);
97 5 push @{ $self->_arguments }, $arg;
98 }
99  
100 4 return $self;
101 }
102  
103 3 ▶sub argument_names {
Subroutine argument_names: called CC=1 SCAR=0.0
Pod documented
104 3 my ($self) = @_;
105 3 return map { $_->name } $self->arguments;
106 }
107  
108 7 1;
109  
110 __END__
« CLI/Argument/String.pm ↑ Summary CLI/Argument/Enum.pm »