| 1 | #============================================================================= | ||||||||||
| 2 | # | ||||||||||
| 3 | # Module: Term::CLI::Argument::Bool | ||||||||||
| 4 | # | ||||||||||
| 5 | # Description: Class for "enum" arguments in Term::CLI | ||||||||||
| 6 | # | ||||||||||
| 7 | # Author: Steven Bakker (SBAKKER), <sbakker@cpan.org> | ||||||||||
| 8 | # Created: 22/01/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::Argument::Bool 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 Types::Standard 1.000005 qw( | |||||||||
| 27 | ArrayRef | ||||||||||
| 28 | Str | ||||||||||
| 29 | Bool | ||||||||||
| 30 | 7 | ▶ | ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 31 | |||||||||||
| 32 | 7 | ▶ | use Term::CLI::L10N qw( loc ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 33 | 7 | ▶ | use Term::CLI::Util qw( is_prefix_str ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 34 | |||||||||||
| 35 | 7 | ▶ | use Moo 1.000001; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 36 | 7 | ▶ | use namespace::clean 0.25; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 37 | |||||||||||
| 38 | 7 | extends 'Term::CLI::Argument'; | |||||||||
| 39 | |||||||||||
| 40 | has 'true_values' => ( | ||||||||||
| 41 | is => 'rw', | ||||||||||
| 42 | isa => ArrayRef [Str], | ||||||||||
| 43 | 0 | ▶ | default => sub { [qw( true 1 )] }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 44 | 7 | ); | |||||||||
| 45 | |||||||||||
| 46 | has 'false_values' => ( | ||||||||||
| 47 | is => 'rw', | ||||||||||
| 48 | isa => ArrayRef [Str], | ||||||||||
| 49 | 0 | ▶ | default => sub { [qw( false 0 )] }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 50 | 7 | ); | |||||||||
| 51 | |||||||||||
| 52 | has 'ignore_case' => ( | ||||||||||
| 53 | is => 'rw', | ||||||||||
| 54 | isa => Bool, | ||||||||||
| 55 | 1 | ▶ | default => sub {1}, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 56 | 7 | ); | |||||||||
| 57 | |||||||||||
| 58 | sub validate { | ||||||||||
| 59 | 8 | ▶ | my ($self, $value) = @_; | ||||||||
|
Subroutine
validate: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||
| 60 | |||||||||||
| 61 | 8 | ▶ | defined $self->SUPER::validate($value) or return; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | |||||||||||
| 63 | 6 | my ( $true, $false ); | |||||||||
| 64 | |||||||||||
| 65 | 6 | ▶ | if ( $self->ignore_case ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 4 | $value = lc $value; | |||||||||
| 67 | |||||||||||
| 68 | 4 | $true = [ map {lc} @{ $self->true_values } ]; | |||||||||
| 69 | 4 | $false = [ map {lc} @{ $self->false_values } ]; | |||||||||
| 70 | } | ||||||||||
| 71 | else { | ||||||||||
| 72 | 2 | $true = $self->true_values; | |||||||||
| 73 | 2 | $false = $self->false_values; | |||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | 6 | my @true_match = grep { is_prefix_str( $value, $_ ) } @{$true}; | |||||||||
| 77 | 6 | my @false_match = grep { is_prefix_str( $value, $_ ) } @{$false}; | |||||||||
| 78 | |||||||||||
| 79 | 6 | ▶ | if (@true_match) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | 3 | ▶ | if (@false_match) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | 2 | return $self->set_error( | |||||||||
| 82 | loc('ambiguous boolean value' | ||||||||||
| 83 | . ' (matches ~[[_1]~] and ~[[_2]~])', | ||||||||||
| 84 | join( ', ', @true_match ), | ||||||||||
| 85 | join( ', ', @false_match ), | ||||||||||
| 86 | ) | ||||||||||
| 87 | ); | ||||||||||
| 88 | } | ||||||||||
| 89 | 1 | return 1; | |||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | 3 | ▶ | return 0 if @false_match; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 93 | |||||||||||
| 94 | 2 | return $self->set_error( loc('invalid boolean value') ); | |||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | sub complete { | ||||||||||
| 98 | 6 | ▶ | my ( $self, $value ) = @_; | ||||||||
|
Subroutine
complete: called CC=5 SCAR=17.5
Pod
documented
| |||||||||||
| 99 | |||||||||||
| 100 | 6 | my @values = ( @{ $self->true_values }, @{ $self->false_values } ); | |||||||||
| 101 | |||||||||||
| 102 | 6 | ▶ | return ( sort @values ) if $value eq q{}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | |||||||||||
| 104 | 5 | ▶ | if ( $self->ignore_case ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 105 | 3 | my @matches = grep { is_prefix_str( lc $value, lc $_ ) } @values; | |||||||||
| 106 | |||||||||||
| 107 | # Cannot just return @matches because of potential case differences. | ||||||||||
| 108 | 3 | ▶ | if ( $value =~ m{ \p{Lu}\z }xsm ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 109 | 3 | return ( sort map { $value . uc substr $_, length $value } @matches ); | |||||||||
| 110 | } | ||||||||||
| 111 | 0 | ▶ | if ( $value =~ m{ \p{Ll}\z }xsm ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 112 | 0 | return ( sort map { $value . lc substr $_, length $value } @matches ); | |||||||||
| 113 | } | ||||||||||
| 114 | 0 | return ( sort map { $value . substr $_, length $value } @matches ); | |||||||||
| 115 | } | ||||||||||
| 116 | 2 | return ( sort grep { is_prefix_str( $value, $_ ) } @values ); | |||||||||
| 117 | } | ||||||||||
| 118 | |||||||||||
| 119 | 7 | 1; | |||||||||
| 120 | |||||||||||
| 121 | __END__ | ||||||||||
| 122 | |||||||||||
| 123 | =pod | ||||||||||
| 124 | |||||||||||
| 125 | =head1 NAME | ||||||||||
| 126 | |||||||||||
| 127 | Term::CLI::Argument::Bool - class for "boolean" arguments in Term::CLI | ||||||||||
| 128 | |||||||||||
| 129 | =head1 VERSION | ||||||||||
| 130 | |||||||||||
| 131 | version 0.061000 | ||||||||||
| 132 | |||||||||||
| 133 | =head1 SYNOPSIS | ||||||||||
| 134 | |||||||||||
| 135 | use Term::CLI::Argument::Bool; | ||||||||||
| 136 | |||||||||||
| 137 | # Case-insensitive booleans (default) | ||||||||||
| 138 | my $arg = Term::CLI::Argument::Bool->new( | ||||||||||
| 139 | name => 'arg1', | ||||||||||
| 140 | ignore_case => 1, # default | ||||||||||
| 141 | ); | ||||||||||
| 142 | |||||||||||
| 143 | $arg->validate( 'true' ); # -> returns 1 | ||||||||||
| 144 | $arg->validate( 'tRuE' ); # -> returns 1 | ||||||||||
| 145 | $arg->validate( '1' ); # -> returns 1 | ||||||||||
| 146 | |||||||||||
| 147 | $arg->validate( 'false' ); # -> returns 0 | ||||||||||
| 148 | $arg->validate( 'FaLsE' ); # -> returns 0 | ||||||||||
| 149 | $arg->validate( '0' ); # -> returns 0 | ||||||||||
| 150 | |||||||||||
| 151 | $arg->validate( 'never' ); # -> returns undef, sets error. | ||||||||||
| 152 | |||||||||||
| 153 | # Case-sensitive booleans | ||||||||||
| 154 | $arg = Term::CLI::Argument::Bool->new( | ||||||||||
| 155 | name => 'arg1', | ||||||||||
| 156 | ignore_case => 0, | ||||||||||
| 157 | ); | ||||||||||
| 158 | |||||||||||
| 159 | $arg->validate( 'tRuE' ); # -> returns undef, sets error. | ||||||||||
| 160 | $arg->validate( 'FaLsE' ); # -> returns undef, sets error. | ||||||||||
| 161 | |||||||||||
| 162 | # Alternative booleans | ||||||||||
| 163 | $arg = Term::CLI::Argument::Bool->new( | ||||||||||
| 164 | name => 'arg1', | ||||||||||
| 165 | true_values => ['on', 'yes'], | ||||||||||
| 166 | false_values => ['off', 'no'], | ||||||||||
| 167 | ); | ||||||||||
| 168 | |||||||||||
| 169 | $arg->validate( 'on' ); # -> returns 1 | ||||||||||
| 170 | $arg->validate( 'off' ); # -> returns 0 | ||||||||||
| 171 | |||||||||||
| 172 | # Abbreviation | ||||||||||
| 173 | $arg->validate( 'y' ); # -> returns 1 | ||||||||||
| 174 | $arg->validate( 'n' ); # -> returns 0 | ||||||||||
| 175 | $arg->validate( 'o' ); # ambiguous -> returns undef, sets error. | ||||||||||
| 176 | |||||||||||
| 177 | =head1 DESCRIPTION | ||||||||||
| 178 | |||||||||||
| 179 | Class for "boolean" string arguments in L<Term::CLI>(3p). | ||||||||||
| 180 | |||||||||||
| 181 | This class inherits from | ||||||||||
| 182 | the L<Term::CLI::Argument>(3p) class. | ||||||||||
| 183 | |||||||||||
| 184 | By default, the valid strings for a boolean are: | ||||||||||
| 185 | |||||||||||
| 186 | =over | ||||||||||
| 187 | |||||||||||
| 188 | =item C<true>, C<1> | ||||||||||
| 189 | |||||||||||
| 190 | A true value. | ||||||||||
| 191 | |||||||||||
| 192 | =item C<false>, C<0> | ||||||||||
| 193 | |||||||||||
| 194 | A false value. | ||||||||||
| 195 | |||||||||||
| 196 | =back | ||||||||||
| 197 | |||||||||||
| 198 | =head2 Case-Insensitive Matching | ||||||||||
| 199 | |||||||||||
| 200 | By default, the object's L<validate()|/validate> and | ||||||||||
| 201 | L<complete()|/complete> methods ignore case, so C<FAlsE> | ||||||||||
| 202 | validates as "false", and C<TR> will have a completion of | ||||||||||
| 203 | C<TRue>. | ||||||||||
| 204 | |||||||||||
| 205 | Set the L<ignore_case|/ignore_case> flag to 0 to do | ||||||||||
| 206 | case-sensitive matching. | ||||||||||
| 207 | |||||||||||
| 208 | =head2 Abbreviations | ||||||||||
| 209 | |||||||||||
| 210 | The L<validate|/validate> method accepts abbreviations | ||||||||||
| 211 | as long as they are uniquely identifying either one or more | ||||||||||
| 212 | "true" values I<or> one or more "false" values. | ||||||||||
| 213 | |||||||||||
| 214 | For example, if you specify the following: | ||||||||||
| 215 | |||||||||||
| 216 | =over | ||||||||||
| 217 | |||||||||||
| 218 | =item B<true_values> =E<gt> [ B<"on">, B<"yes"> ] | ||||||||||
| 219 | |||||||||||
| 220 | =item B<false_values> =E<gt> [ B<"off">, B<"never">, B<"no"> ] | ||||||||||
| 221 | |||||||||||
| 222 | =back | ||||||||||
| 223 | |||||||||||
| 224 | Then the string C<o> will not validate since it matches both a "true" | ||||||||||
| 225 | value (C<on>) I<and> a "false" value (C<off>). On the other hand, the | ||||||||||
| 226 | string C<n> I<will> validate, for although it matches both C<never> | ||||||||||
| 227 | and C<no>, those values are both "false" values, so there is no ambiguity. | ||||||||||
| 228 | |||||||||||
| 229 | =head1 CLASS STRUCTURE | ||||||||||
| 230 | |||||||||||
| 231 | =head2 Inherits from: | ||||||||||
| 232 | |||||||||||
| 233 | L<Term::CLI::Argument>(3p). | ||||||||||
| 234 | |||||||||||
| 235 | =head2 Consumes: | ||||||||||
| 236 | |||||||||||
| 237 | None. | ||||||||||
| 238 | |||||||||||
| 239 | =head1 CONSTRUCTORS | ||||||||||
| 240 | |||||||||||
| 241 | =over | ||||||||||
| 242 | |||||||||||
| 243 | =item B<new> ( B<name> =E<gt> I<STRING>, ... ) | ||||||||||
| 244 | |||||||||||
| 245 | See also L<Term::CLI::Argument>(3p). | ||||||||||
| 246 | |||||||||||
| 247 | Additional attributes: | ||||||||||
| 248 | |||||||||||
| 249 | =over | ||||||||||
| 250 | |||||||||||
| 251 | =item B<true_values> =E<gt> I<ArrayRef>[I<Str>] | ||||||||||
| 252 | X<true_values> | ||||||||||
| 253 | |||||||||||
| 254 | List of values that are considered to be "true". Default | ||||||||||
| 255 | is C<['true', '1']>. | ||||||||||
| 256 | |||||||||||
| 257 | =item B<false_values> =E<gt> I<ArrayRef>[I<Str>] | ||||||||||
| 258 | X<false_values> | ||||||||||
| 259 | |||||||||||
| 260 | List of values that are considered to be "false". Default | ||||||||||
| 261 | is C<['false', '0']>. | ||||||||||
| 262 | |||||||||||
| 263 | =item B<ignore_case> =E<gt> I<Bool> | ||||||||||
| 264 | X<ignore_case> | ||||||||||
| 265 | |||||||||||
| 266 | Whether or not matching should ignore case. Default is 1 (so C<True> | ||||||||||
| 267 | and C<FALSE> are valid as well). | ||||||||||
| 268 | |||||||||||
| 269 | =back | ||||||||||
| 270 | |||||||||||
| 271 | =back | ||||||||||
| 272 | |||||||||||
| 273 | =head1 ACCESSORS | ||||||||||
| 274 | |||||||||||
| 275 | See also L<Term::CLI::Argument>(3p). | ||||||||||
| 276 | |||||||||||
| 277 | =over | ||||||||||
| 278 | |||||||||||
| 279 | =item B<ignore_case> ( [ I<Bool> ] ) | ||||||||||
| 280 | X<ignore_case> | ||||||||||
| 281 | |||||||||||
| 282 | Get or set the C<ignore_case> flag. | ||||||||||
| 283 | |||||||||||
| 284 | =item B<true_values> ( [ I<ArrayRef>[I<Str>] ] ) | ||||||||||
| 285 | X<true_values> | ||||||||||
| 286 | |||||||||||
| 287 | Get or set the list of strings that denote a "true" value. | ||||||||||
| 288 | |||||||||||
| 289 | =item B<false_values> ( [ I<ArrayRef>[I<Str>] ] ) | ||||||||||
| 290 | X<false_values> | ||||||||||
| 291 | |||||||||||
| 292 | Get or set the list of strings that denote a "false" value. | ||||||||||
| 293 | |||||||||||
| 294 | =back | ||||||||||
| 295 | |||||||||||
| 296 | =head1 METHODS | ||||||||||
| 297 | |||||||||||
| 298 | See also L<Term::CLI::Argument>(3p). | ||||||||||
| 299 | |||||||||||
| 300 | The following methods are added or overloaded: | ||||||||||
| 301 | |||||||||||
| 302 | =over | ||||||||||
| 303 | |||||||||||
| 304 | =item B<validate> ( I<Str> ) | ||||||||||
| 305 | |||||||||||
| 306 | Validate I<Str> to see if it is a uniquely "true" or "false" value. Return | ||||||||||
| 307 | 1 if it is a "true" value, 0 if it is a "false" value. | ||||||||||
| 308 | |||||||||||
| 309 | If the true/false validity cannot be determined, the object's C<error> | ||||||||||
| 310 | attribute is set and C<undef> is returned. | ||||||||||
| 311 | |||||||||||
| 312 | =item B<complete> ( I<Str> ) | ||||||||||
| 313 | |||||||||||
| 314 | Return a list of possible completions for I<Str>. If C<ignore_case> is true, | ||||||||||
| 315 | then values like C<FA> will result in C<('FAlse')>. | ||||||||||
| 316 | |||||||||||
| 317 | =back | ||||||||||
| 318 | |||||||||||
| 319 | =head1 SEE ALSO | ||||||||||
| 320 | |||||||||||
| 321 | L<Term::CLI::Argument>(3p), | ||||||||||
| 322 | L<Term::CLI>(3p). | ||||||||||
| 323 | |||||||||||
| 324 | =head1 AUTHOR | ||||||||||
| 325 | |||||||||||
| 326 | Steven Bakker E<lt>sbakker@cpan.orgE<gt>, 2018. | ||||||||||
| 327 | |||||||||||
| 328 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 329 | |||||||||||
| 330 | Copyright (c) 2018 Steven Bakker | ||||||||||
| 331 | |||||||||||
| 332 | This module is free software; you can redistribute it and/or modify | ||||||||||
| 333 | it under the same terms as Perl itself. See "perldoc perlartistic." | ||||||||||
| 334 | |||||||||||
| 335 | This software is distributed in the hope that it will be useful, | ||||||||||
| 336 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||
| 337 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||||||||||
| 338 | |||||||||||
| 339 | =cut | ||||||||||