| 1 | package MouseX::Getopt::OptionTypeMap; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Storage for the option to type mappings | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 22 | ▶ | use Mouse 'confess', 'blessed'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 22 | ▶ | use Mouse::Util::TypeConstraints 'find_type_constraint'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 22 | my %option_type_map = ( | |||||||||||||||||||||||||||
| 8 | 'Bool' => '!', | ||||||||||||||||||||||||||||
| 9 | 'Str' => '=s', | ||||||||||||||||||||||||||||
| 10 | 'Int' => '=i', | ||||||||||||||||||||||||||||
| 11 | 'Num' => '=f', | ||||||||||||||||||||||||||||
| 12 | 'ArrayRef' => '=s@', | ||||||||||||||||||||||||||||
| 13 | 'HashRef' => '=s%', | ||||||||||||||||||||||||||||
| 14 | ); | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | sub has_option_type { | ||||||||||||||||||||||||||||
| 17 | 545 | ▶ | my (undef, $type_or_name) = @_; | ||||||||||||||||||||||||||
|
Subroutine
has_option_type: called CC=7 SCAR=22.1
Pod
documented
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 545 | ▶ | return 1 if exists $option_type_map{blessed($type_or_name) ? $type_or_name->name : $type_or_name}; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 2 | ▶ | my $current = blessed($type_or_name) ? $type_or_name : find_type_constraint($type_or_name); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 2 | ▶ | (defined $current) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | || confess "Could not find the type constraint for '$type_or_name'"; | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 2 | ▶ | while (my $parent = $current->parent) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 2 | ▶ | return 1 if exists $option_type_map{$parent->name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 0 | $current = $parent; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 0 | return 0; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | sub get_option_type { | ||||||||||||||||||||||||||||
| 35 | 545 | ▶ | my (undef, $type_or_name) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_option_type: called CC=7 SCAR=21.2
Pod
documented
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 545 | ▶ | my $name = blessed($type_or_name) ? $type_or_name->name : $type_or_name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 545 | ▶ | return $option_type_map{$name} if exists $option_type_map{$name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 2 | ▶ | my $current = ref $type_or_name ? $type_or_name : find_type_constraint($type_or_name); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 2 | ▶ | (defined $current) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | || confess "Could not find the type constraint for '$type_or_name'"; | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 2 | ▶ | while ( $current = $current->parent ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | return $option_type_map{$current->name} | ||||||||||||||||||||||||||||
| 48 | 2 | ▶ | if exists $option_type_map{$current->name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | return; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | sub add_option_type_to_map { | ||||||||||||||||||||||||||||
| 55 | 9 | ▶ | my (undef, $type_name, $option_string) = @_; | ||||||||||||||||||||||||||
|
Subroutine
add_option_type_to_map: called CC=5 SCAR=17.0
Pod
documented
| |||||||||||||||||||||||||||||
| 56 | 9 | ▶ | (defined $type_name && defined $option_string) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $type_name and defined $option_string
MC/DC0%0/2
defined $type_name and defined $option_string
defined $type_name
defined $option_string
Truth table33%1/3
A and B
A= defined $type_nameB= defined $option_string
| |||||||||||||||||||||||||||||
| 57 | || confess "You must supply both a type name and an option string"; | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 9 | ▶ | if ( blessed($type_name) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 4 | $type_name = $type_name->name; | |||||||||||||||||||||||||||
| 61 | } else { | ||||||||||||||||||||||||||||
| 62 | 5 | ▶ | (find_type_constraint($type_name)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | || confess "The type constraint '$type_name' does not exist"; | ||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 9 | $option_type_map{$type_name} = $option_string; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 22 | ▶ | no Mouse::Util::TypeConstraints; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 70 | 22 | ▶ | no Mouse; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 22 | 1; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | See the I<Custom Type Constraints> section in the L<MouseX::Getopt> docs | ||||||||||||||||||||||||||||
| 77 | for more info about how to use this module. | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | =over 4 | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =item B<has_option_type ($type_or_name)> | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =item B<get_option_type ($type_or_name)> | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =item B<add_option_type_to_map ($type_name, $option_spec)> | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =back | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =cut | ||||||||||||||||||||||||||||