| 1 | #============================================================================= | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # Module: Term::CLI::ArgumentSet | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # Description: Role for sets of arguments in Term::CLI | ||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||
| 7 | # Author: Steven Bakker (SBAKKER), <sbakker@cpan.org> | ||||||||||||||||||||||||||||
| 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.061000; | ||||||||||||||||||||||||||||
| 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( | |||||||||||||||||||||||||||
| 29 | ArrayRef | ||||||||||||||||||||||||||||
| 30 | InstanceOf | ||||||||||||||||||||||||||||
| 31 | Maybe | ||||||||||||||||||||||||||||
| 32 | 7 | ▶ | ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 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 | coerce => sub { | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # Copy the array, so the reference we store becomes | ||||||||||||||||||||||||||||
| 46 | # "internal", preventing accidental modification | ||||||||||||||||||||||||||||
| 47 | # from the outside. | ||||||||||||||||||||||||||||
| 48 | 28 | ▶ | return [ @{ $_[0] } ]; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.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 | sub _trigger__arguments { ## no critic (ProhibitUnusedPrivateSubroutines) | ||||||||||||||||||||||||||||
| 57 | 27 | ▶ | my ( $self, $arg_list ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_trigger__arguments: called CC=5 SCAR=17.2
| |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 27 | ▶ | return if !$arg_list; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # Set the parent for each argument object. | ||||||||||||||||||||||||||||
| 62 | 27 | ▶ | if (ref $arg_list && reftype $arg_list eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $arg_list and builtin::reftype($arg_list) eq "ARRAY"
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"
| |||||||||||||||||||||||||||||
| 63 | 27 | for my $arg_obj ( @{$arg_list} ) { | |||||||||||||||||||||||||||
| 64 | 30 | $arg_obj->_set_parent($self); | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | 27 | return; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | sub arguments { | ||||||||||||||||||||||||||||
| 71 | 94 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
arguments: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 72 | 94 | ▶ | return @{ $self->_arguments // [] }; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->_arguments // []
MC/DC100%1/1
$self->_arguments // []
$self->_arguments
Truth table100%2/2
A
A= $self->_arguments
| |||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | sub set_arguments { | ||||||||||||||||||||||||||||
| 76 | 1 | ▶ | my ( $self, @arguments ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
set_arguments: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 1 | $self->_set_arguments( [] ); | |||||||||||||||||||||||||||
| 79 | 1 | $self->add_argument(@arguments); | |||||||||||||||||||||||||||
| 80 | 1 | return; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | sub has_arguments { | ||||||||||||||||||||||||||||
| 84 | 123 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
has_arguments: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | 123 | ▶ | return ( $self->_arguments and scalar @{ $self->_arguments } > 0 ); | ||||||||||||||||||||||||||
|
Condition100%3/3
$self->_arguments && scalar @{$self->_arguments;} > 0
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
| |||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | sub add_argument { | ||||||||||||||||||||||||||||
| 89 | 4 | ▶ | my ( $self, @arguments ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
add_argument: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 4 | ▶ | if ( !$self->_arguments ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | sub argument_names { | ||||||||||||||||||||||||||||
| 104 | 3 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
argument_names: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 105 | 3 | return map { $_->name } $self->arguments; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 7 | 1; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | __END__ | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =pod | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =head1 NAME | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | Term::CLI::Role::ArgumentSet - Manage command arguments in Term::CLI | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =head1 VERSION | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | version 0.061000 | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | package Term::CLI::Command { | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | use Moo; | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | with('Term::CLI::Role::ArgumentSet'); | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | ... | ||||||||||||||||||||||||||||
| 131 | }; | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | my $cmd = Term::CLI::Command->new( ... ); | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | $cmd->add_argument( Term::CLI::Argument->new(...) ); | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | say "argument names:", join(', ', $cmd->argument_names); | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | Role for L<Term::CLI::Command>(3p) elements to represent | ||||||||||||||||||||||||||||
| 142 | a set of L<Term::CLI::Argument>(3p) objects. | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | This role is consumed by L<Term::CLI::Command>(3p). | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | This role defines two additional attributes: | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =over | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =item B<arguments> =E<gt> I<ArrayRef> | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | Reference to an array containing C<Term::CLI::Argument> object | ||||||||||||||||||||||||||||
| 155 | instances that describe the parameters that the command takes, | ||||||||||||||||||||||||||||
| 156 | or C<undef>. | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | Note that the elements of the array are copied over to an internal | ||||||||||||||||||||||||||||
| 159 | array, so modifications to the I<ArrayRef> will not be seen. | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =back | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =head1 ACCESSORS AND PREDICATES | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =over | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =item B<has_arguments> | ||||||||||||||||||||||||||||
| 168 | X<has_arguments> | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | Predicate function that returns whether or not any | ||||||||||||||||||||||||||||
| 171 | L<Term::CLI::Argument|Term::CLI::Argument>s have been | ||||||||||||||||||||||||||||
| 172 | added. | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | =item B<arguments> | ||||||||||||||||||||||||||||
| 175 | X<arguments> | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | Return the list of | ||||||||||||||||||||||||||||
| 178 | L<Term::CLI::Argument|Term::CLI::Argument> object | ||||||||||||||||||||||||||||
| 179 | references that are owned by this object. | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =back | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =head1 METHODS | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =over | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =item B<set_arguments> ( I<ARG>, ... ) | ||||||||||||||||||||||||||||
| 188 | X<set_arguments> | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | Reset the list of arguments to (I<ARG>, ...). | ||||||||||||||||||||||||||||
| 191 | Each I<ARG> should be a reference to a | ||||||||||||||||||||||||||||
| 192 | L<Term::CLI::Argument|Term::CLI::Argument> object. | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | =item B<add_argument> ( I<ARG>, ... ) | ||||||||||||||||||||||||||||
| 195 | X<add_argument> | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | Add I<ARG>(s) to the argument set. | ||||||||||||||||||||||||||||
| 198 | Each I<ARG> should be a reference to a | ||||||||||||||||||||||||||||
| 199 | L<Term::CLI::Argument|Term::CLI::Argument> object. | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | =item B<argument_names> | ||||||||||||||||||||||||||||
| 202 | X<argument_names> | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | Return the list of (sub-)command names (in the order they were specified). | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | =back | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | L<Term::CLI>(3p), | ||||||||||||||||||||||||||||
| 211 | L<Term::CLI::Command>(3p). | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | Steven Bakker E<lt>sbakker@cpan.orgE<gt>, 2018. | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | Copyright (c) 2018 Steven Bakker | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | This module is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 222 | it under the same terms as Perl itself. See "perldoc perlartistic." | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | This software is distributed in the hope that it will be useful, | ||||||||||||||||||||||||||||
| 225 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||||||||||||||||
| 226 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | =begin __PODCOVERAGE | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | =head1 THIS SECTION SHOULD BE HIDDEN | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | This section is meant for methods that should not be considered | ||||||||||||||||||||||||||||
| 233 | for coverage. This typically includes things like BUILD and DEMOLISH from | ||||||||||||||||||||||||||||
| 234 | Moo/Moose. It is possible to skip these when using the Pod::Coverage class | ||||||||||||||||||||||||||||
| 235 | (using C<also_private>), but this is not an option when running C<cover> | ||||||||||||||||||||||||||||
| 236 | from the command line. | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | The simplest trick is to add a hidden section with an item list containing | ||||||||||||||||||||||||||||
| 239 | these methods. | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | =over | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | =item BUILD | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | =item DEMOLISH | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | =back | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | =end __PODCOVERAGE | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | =cut | ||||||||||||||||||||||||||||