| 1 | #============================================================================= | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # Module: Term::CLI::Argument::Enum | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # Description: Class for "enum" arguments in Term::CLI | ||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||
| 7 | # Author: Steven Bakker (SBAKKER), <cpanman@monkey-mind.net> | ||||||||||||||||||||||||||||
| 8 | # Created: 22/Jan/2018 | ||||||||||||||||||||||||||||
| 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::Enum 0.061001; | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 10 | ▶ | use 5.014; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 10 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 10 | ▶ | use Term::CLI::L10N qw( loc ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 10 | ▶ | use Term::CLI::Util qw( is_prefix_str find_text_matches ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 10 | ▶ | use Types::Standard 1.000005 qw( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | ArrayRef | ||||||||||||||||||||||||||||
| 31 | CodeRef | ||||||||||||||||||||||||||||
| 32 | 10 | ); | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 10 | ▶ | use Moo 1.000001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 10 | ▶ | use List::Util 1.23 qw( first ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 10 | ▶ | use Scalar::Util 1.23 qw( reftype ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 10 | ▶ | use namespace::clean 0.25; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 10 | extends 'Term::CLI::Argument'; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | has value_list => ( | ||||||||||||||||||||||||||||
| 43 | is => 'ro', | ||||||||||||||||||||||||||||
| 44 | isa => ArrayRef | CodeRef, | ||||||||||||||||||||||||||||
| 45 | required => 1, | ||||||||||||||||||||||||||||
| 46 | 15 | ▶ | coerce => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 47 | 15 | my ($arg) = @_; | |||||||||||||||||||||||||||
| 48 | 15 | ▶ | return $arg if ref $arg && reftype $arg ne 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $arg and builtin::reftype($arg) ne "ARRAY"
MC/DC50%1/2
ref $arg and builtin::reftype($arg) ne "ARRAY"
ref $arg
builtin::reftype($arg) ne "ARRAY"
Truth table66%2/3
A and B
A= ref $argB= builtin::reftype($arg) ne "ARRAY"
| |||||||||||||||||||||||||||||
| 49 | 13 | return [ sort @$arg ]; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | 10 | ); | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | has cache_values => ( | ||||||||||||||||||||||||||||
| 54 | is => 'rw', | ||||||||||||||||||||||||||||
| 55 | default => 0, | ||||||||||||||||||||||||||||
| 56 | # Trigger for when caching gets disabled to immediately clear the | ||||||||||||||||||||||||||||
| 57 | # cache. This allows for quicker cleanup of objects, file handles, etc. | ||||||||||||||||||||||||||||
| 58 | 2 | ▶ | trigger => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 59 | 2 | my ($self, $new) = @_; | |||||||||||||||||||||||||||
| 60 | 2 | ▶ | $self->_clear_value_cache if ! $new; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | 10 | ); | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 10 | has _value_cache => ( | |||||||||||||||||||||||||||
| 65 | is => 'rw', | ||||||||||||||||||||||||||||
| 66 | isa => ArrayRef, | ||||||||||||||||||||||||||||
| 67 | predicate => 1, | ||||||||||||||||||||||||||||
| 68 | clearer => 1, | ||||||||||||||||||||||||||||
| 69 | ); | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 29 | ▶ | sub values { | ||||||||||||||||||||||||||
|
Subroutine
values: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 72 | 29 | my ($self) = @_; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 29 | my $value_list = $self->value_list; | |||||||||||||||||||||||||||
| 75 | 29 | ▶ | return $value_list if reftype $value_list eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # Dynamic values... | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # Return cache if possible. | ||||||||||||||||||||||||||||
| 80 | 11 | ▶ | if ( $self->_has_value_cache ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 1 | return $self->_value_cache; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 10 | my $list_ref = [ sort @{ $value_list->($self) } ]; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 10 | ▶ | if ( $self->cache_values ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 1 | $self->_value_cache( $list_ref ); | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 10 | return $list_ref; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 14 | ▶ | sub validate { | ||||||||||||||||||||||||||
|
Subroutine
validate: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 95 | 14 | my ( $self, $value ) = @_; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 14 | ▶ | defined $self->SUPER::validate($value) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 10 | my $values_r = $self->values; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 10 | my @found = find_text_matches( $value, $values_r, exact => 1 ); | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 10 | ▶ | if ( @found == 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 3 | return $self->set_error( loc("not a valid value") ); | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 7 | ▶ | return $found[0] if @found == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 2 | return $self->set_error( | |||||||||||||||||||||||||||
| 110 | loc( "ambiguous value (matches: [_1])", join( ", ", @found ) ) ); | ||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 15 | ▶ | sub complete { | ||||||||||||||||||||||||||
|
Subroutine
complete: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 114 | 15 | my ( $self, $text ) = @_; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 15 | my $values_r = $self->values; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 15 | ▶ | return @{$values_r} if !length $text; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 13 | return find_text_matches( $text, $values_r ); | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 10 | 1; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | __END__ | ||||||||||||||||||||||||||||