| 1 | package CLI::Driver::Option; | ||||||||||
| 2 | |||||||||||
| 3 | 17 | ▶ | use Modern::Perl; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 17 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 17 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 17 | ▶ | use Kavorka '-all'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 17 | ▶ | use Data::Printer alias => 'pdump'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 17 | ▶ | use Getopt::Long 'GetOptionsFromArray'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 17 | Getopt::Long::Configure('no_ignore_case'); | |||||||||
| 11 | 17 | Getopt::Long::Configure('pass_through'); | |||||||||
| 12 | |||||||||||
| 13 | 17 | with 'CLI::Driver::CommonRole'; | |||||||||
| 14 | |||||||||||
| 15 | ############################### | ||||||||||
| 16 | ###### PUBLIC ATTRIBUTES ###### | ||||||||||
| 17 | ############################### | ||||||||||
| 18 | |||||||||||
| 19 | 17 | has class => ( | |||||||||
| 20 | is => 'rw', | ||||||||||
| 21 | isa => 'Str', | ||||||||||
| 22 | ); | ||||||||||
| 23 | |||||||||||
| 24 | 17 | has cli_arg => ( | |||||||||
| 25 | is => 'rw', | ||||||||||
| 26 | isa => 'Str' | ||||||||||
| 27 | ); | ||||||||||
| 28 | |||||||||||
| 29 | 17 | has method_arg => ( | |||||||||
| 30 | is => 'rw', | ||||||||||
| 31 | isa => 'Str|Undef' | ||||||||||
| 32 | ); | ||||||||||
| 33 | |||||||||||
| 34 | 17 | has required => ( | |||||||||
| 35 | is => 'rw', | ||||||||||
| 36 | isa => 'Bool' | ||||||||||
| 37 | ); | ||||||||||
| 38 | |||||||||||
| 39 | 17 | has hard => ( | |||||||||
| 40 | is => 'rw', | ||||||||||
| 41 | isa => 'Bool', | ||||||||||
| 42 | ); | ||||||||||
| 43 | |||||||||||
| 44 | 17 | has flag => ( | |||||||||
| 45 | is => 'rw', | ||||||||||
| 46 | isa => 'Bool', | ||||||||||
| 47 | ); | ||||||||||
| 48 | |||||||||||
| 49 | 17 | has is_array => ( | |||||||||
| 50 | is => 'rw', | ||||||||||
| 51 | isa => 'Bool', | ||||||||||
| 52 | ); | ||||||||||
| 53 | |||||||||||
| 54 | 17 | has value => ( | |||||||||
| 55 | is => 'rw', | ||||||||||
| 56 | isa => 'Any', | ||||||||||
| 57 | ); | ||||||||||
| 58 | |||||||||||
| 59 | 17 | has 'use_argv_map' => ( is => 'rw', isa => 'Bool' ); | |||||||||
| 60 | |||||||||||
| 61 | ############################################### | ||||||||||
| 62 | |||||||||||
| 63 | 17 | ▶ | method get_signature { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
get_signature: called CC=3 SCAR=11.0
Branch: 1 branch
| |||||||||||
| 64 | |||||||||||
| 65 | 68 | my %sig; | |||||||||
| 66 | |||||||||||
| 67 | 68 | my $val = $self->_get_val; | |||||||||
| 68 | 68 | $self->value($val); | |||||||||
| 69 | 68 | ▶ | if ( defined $val ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 49 | $sig{ $self->method_arg } = $val; | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | 68 | return %sig; | |||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | 17 | ▶ | method is_required { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
is_required: called CC=3 SCAR=11.0
Branch: 1 branch
| |||||||||||
| 77 | |||||||||||
| 78 | 35 | ▶ | if ( $self->required ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | 20 | return 1; | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | 15 | return 0; | |||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | 17 | ▶ | method is_flag { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
is_flag: called CC=3 SCAR=11.0
Branch: 1 branch
| |||||||||||
| 86 | |||||||||||
| 87 | 68 | ▶ | if ( $self->flag ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 4 | return 1; | |||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | 64 | return 0; | |||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | 17 | ▶ | method is_optional { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=1.2
Subroutine
is_optional: not called CC=3 SCAR=17.9
Branch: 1 branch
| |||||||||||
| 95 | |||||||||||
| 96 | 0 | ▶ | if ( !$self->required ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | 0 | return 1; | |||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | 0 | return 0; | |||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | 17 | ▶ | method is_hard { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
is_hard: called CC=3 SCAR=11.0
Branch: 1 branch
| |||||||||||
| 104 | |||||||||||
| 105 | 9 | ▶ | if ( $self->hard ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 106 | 5 | return 1; | |||||||||
| 107 | } | ||||||||||
| 108 | |||||||||||
| 109 | 4 | return 0; | |||||||||
| 110 | } | ||||||||||
| 111 | |||||||||||
| 112 | 17 | ▶ | method is_soft { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
is_soft: called CC=3 SCAR=11.0
Branch: 1 branch
| |||||||||||
| 113 | |||||||||||
| 114 | 16 | ▶ | if ( !$self->hard ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 115 | 2 | return 1; | |||||||||
| 116 | } | ||||||||||
| 117 | |||||||||||
| 118 | 14 | return 0; | |||||||||
| 119 | } | ||||||||||
| 120 | |||||||||||
| 121 | 17 | ▶ | method _get_val { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
_get_val: called CC=12 SCAR=28.2
Branch: 1 branch
| |||||||||||
| 122 | |||||||||||
| 123 | 68 | my $arg = $self->cli_arg; | |||||||||
| 124 | 68 | my $val; | |||||||||
| 125 | |||||||||||
| 126 | 68 | ▶ | if ( $self->is_flag ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 127 | |||||||||||
| 128 | # - just a cli switch | ||||||||||
| 129 | # - never required from cmdline | ||||||||||
| 130 | |||||||||||
| 131 | 4 | ▶ | if ( $self->use_argv_map ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 132 | 0 | $val = $ARGV{$self->method_arg}; | |||||||||
| 133 | 0 | delete $ARGV{$self->method_arg}; | |||||||||
| 134 | 0 | return $val; | |||||||||
| 135 | } | ||||||||||
| 136 | else { | ||||||||||
| 137 | 4 | my $success = GetOptionsFromArray( \@ARGV, "$arg" => \$val, ); | |||||||||
| 138 | 4 | ▶ | if ($success) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 139 | 4 | ▶ | return $val ? 1 : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 140 | } | ||||||||||
| 141 | } | ||||||||||
| 142 | |||||||||||
| 143 | 0 | confess "something went sideways?"; | |||||||||
| 144 | } | ||||||||||
| 145 | else { | ||||||||||
| 146 | |||||||||||
| 147 | 64 | ▶ | if ( $self->use_argv_map ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 148 | 4 | ▶ | if ( $ARGV{$self->method_arg} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 149 | 4 | $val = $ARGV{$self->method_arg}; | |||||||||
| 150 | 4 | delete $ARGV{$self->method_arg}; | |||||||||
| 151 | 4 | return $val; | |||||||||
| 152 | } | ||||||||||
| 153 | } | ||||||||||
| 154 | else { | ||||||||||
| 155 | # get "-arg <val>" from cmdline if exists | ||||||||||
| 156 | 60 | my $arg_type = "s"; | |||||||||
| 157 | 60 | ▶ | $arg_type.= '@' if $self->is_array; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 158 | |||||||||||
| 159 | 60 | my $success = | |||||||||
| 160 | GetOptionsFromArray( \@ARGV, "$arg=$arg_type" => \$val, ); | ||||||||||
| 161 | 60 | ▶ | if ($success) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 162 | 60 | return $val; | |||||||||
| 163 | } | ||||||||||
| 164 | |||||||||||
| 165 | 0 | ▶ | confess "something went sideways?" if !$success; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 166 | } | ||||||||||
| 167 | |||||||||||
| 168 | # we didn't find it in @ARGV | ||||||||||
| 169 | 0 | ▶ | if ( $self->required ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 170 | 0 | confess "failed to get arg from argv: $arg"; | |||||||||
| 171 | } | ||||||||||
| 172 | } | ||||||||||
| 173 | |||||||||||
| 174 | 0 | return; | |||||||||
| 175 | } | ||||||||||
| 176 | |||||||||||
| 177 | 17 | 1; | |||||||||