| 1 | package Class::Usul::Options; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 18 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 18 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 18 | ▶ | use Class::Usul::Constants qw( FALSE TRUE ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 18 | ▶ | use Class::Usul::Functions qw( throw ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 18 | ▶ | use Sub::Install qw( install_sub ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | my @option_attributes | ||||||||||||||||||||||||||||||||||||||||||
| 11 | 18 | = qw( autosplit config doc format json negateable order repeatable short ); | |||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | my @banished_keywords | ||||||||||||||||||||||||||||||||||||||||||
| 14 | 18 | = qw( extra_argv new_with_options next_argv option _options_data | |||||||||||||||||||||||||||||||||||||||||
| 15 | _options_config options_usage unshift_argv untainted_argv ); | ||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | # Private functions | ||||||||||||||||||||||||||||||||||||||||||
| 18 | 265 | ▶ | my $filter_attributes = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 265 | my %attributes = @_; my %filter_key = map { $_ => 1 } @option_attributes; | |||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 1113 | return map { ( $_ => $attributes{ $_ } ) } | |||||||||||||||||||||||||||||||||||||||||
| 22 | 265 | grep { not exists $filter_key{ $_ } } keys %attributes; | |||||||||||||||||||||||||||||||||||||||||
| 23 | 18 | }; | |||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 265 | ▶ | my $validate_and_filter_options = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=10 SCAR=23.3
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 265 | my (%options) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 265 | ▶ | defined $options{doc } or $options{doc } = $options{documentation}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 265 | ▶ | defined $options{order} or $options{order} = 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 265 | ▶ | if ($options{json}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 1 | delete $options{repeatable}; delete $options{autosplit}; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 1 | delete $options{negateable}; $options{format} = 's'; | |||||||||||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 850 | my %cmdline_options = map { ( $_ => $options{ $_ } ) } | |||||||||||||||||||||||||||||||||||||||||
| 37 | 265 | grep { exists $options{ $_ } } @option_attributes, 'required'; | |||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 265 | ▶ | $cmdline_options{autosplit } and $cmdline_options{repeatable} = TRUE; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | $cmdline_options{repeatable} | ||||||||||||||||||||||||||||||||||||||||||
| 41 | and defined $cmdline_options{format} | ||||||||||||||||||||||||||||||||||||||||||
| 42 | and (substr $cmdline_options{format}, -1) ne '@' | ||||||||||||||||||||||||||||||||||||||||||
| 43 | 265 | ▶ | and $cmdline_options{format} .= '@'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$cmdline_options{"repeatable"} and defined $cmdline_options{"format"} and substr($cmdline_options{"format"}, -1) ne "\@"
$cmdline_options{"repeatable"} and defined $cmdline_options{"format"}
MC/DC0%0/3
$cmdline_options{"repeatable"} and defined $cmdline_options{"format"} and substr($cmdline_options{"format"}, -1) ne "\@"
$cmdline_options{"repeatable"}
defined $cmdline_options{"format"}
substr($cmdline_options{"format"}, -1) ne "\@"
Truth table75%3/4
A and B and C
A= $cmdline_options{"repeatable"}B= defined $cmdline_options{"format"}C= substr($cmdline_options{"format"}, -1) ne "\@"
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | $cmdline_options{negateable} and defined $cmdline_options{format} and | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 265 | ▶ | throw 'Negateable parameters are not usable with a non boolean values'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$cmdline_options{"negateable"} and defined $cmdline_options{"format"}
MC/DC0%0/2
$cmdline_options{"negateable"} and defined $cmdline_options{"format"}
$cmdline_options{"negateable"}
defined $cmdline_options{"format"}
Truth table66%2/3
A and B
A= $cmdline_options{"negateable"}B= defined $cmdline_options{"format"}
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | 265 | return %cmdline_options; | |||||||||||||||||||||||||||||||||||||||||
| 49 | 18 | }; | |||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | # Public functions | ||||||||||||||||||||||||||||||||||||||||||
| 52 | 88 | ▶ | sub default_options_config () { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_options_config: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 88 | return getopt_conf => [], | |||||||||||||||||||||||||||||||||||||||||
| 54 | prefer_commandline => TRUE, | ||||||||||||||||||||||||||||||||||||||||||
| 55 | protect_argv => TRUE, | ||||||||||||||||||||||||||||||||||||||||||
| 56 | show_defaults => FALSE, | ||||||||||||||||||||||||||||||||||||||||||
| 57 | skip_options => [], | ||||||||||||||||||||||||||||||||||||||||||
| 58 | usage_conf => {}, | ||||||||||||||||||||||||||||||||||||||||||
| 59 | usage_opt => 'Usage: %c %o [method]'; | ||||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 88 | ▶ | sub import { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 88 | my ($class, @args) = @_; my $target = caller; | |||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 88 | my $options_config = { default_options_config, @args }; | |||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 88 | for my $want (grep { not $target->can( $_ ) } qw( around has with )) { | |||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | throw 'Method [_1] not found in class [_2]', [ $want, $target ]; | |||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 88 | my $around = $target->can( 'around' ); | |||||||||||||||||||||||||||||||||||||||||
| 72 | 88 | my $has = $target->can( 'has' ); | |||||||||||||||||||||||||||||||||||||||||
| 73 | 88 | my $with = $target->can( 'with' ); | |||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | 18 | ▶ | my @target_isa; { no strict 'refs'; @target_isa = @{ "${target}::ISA" } }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 88 | ▶ | if (@target_isa) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | # Don't add this to a role. The ISA of a role is always empty! | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 6 | ▶ | install_sub { as => '_options_config', into => $target, code => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 6 | return shift->maybe::next::method( @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 81 | 20 | }, }; | |||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 6 | ▶ | install_sub { as => '_options_data', into => $target, code => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 6 | return shift->maybe::next::method( @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 85 | 20 | }, }; | |||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 6 | ▶ | $around->( '_options_config' => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 6 | my ($orig, $self, @args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | 6 | return $orig->( $self, @args ), %{ $options_config }; | |||||||||||||||||||||||||||||||||||||||||
| 91 | 20 | } ); | |||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 88 | my $options_data = {}; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 353 | ▶ | my $apply_modifiers = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 353 | ▶ | $target->can( 'new_with_options' ) and return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 156 | $with->( 'Class::Usul::TraitFor::UntaintedGetopts' ); | |||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 22 | ▶ | $around->( '_options_data' => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 22 | my ($orig, $self, @args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | 22 | return $orig->( $self, @args ), %{ $options_data }; | |||||||||||||||||||||||||||||||||||||||||
| 104 | 156 | } ); | |||||||||||||||||||||||||||||||||||||||||
| 105 | 88 | }; | |||||||||||||||||||||||||||||||||||||||||
| 106 | 265 | ▶ | my $option = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 265 | my ($name, %attributes) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | 265 | for my $ban (grep { $_ eq $name } @banished_keywords) { | |||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | throw 'Method [_1] used by class [_2] as an attribute', | |||||||||||||||||||||||||||||||||||||||||
| 111 | [ $ban, $target ]; | ||||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | 265 | $has->( $name => $filter_attributes->( %attributes ) ); | |||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | 265 | $options_data->{ $name } | |||||||||||||||||||||||||||||||||||||||||
| 117 | = { $validate_and_filter_options->( %attributes ) }; | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | 265 | $apply_modifiers->(); # TODO: I think this can go | |||||||||||||||||||||||||||||||||||||||||
| 120 | 265 | return; | |||||||||||||||||||||||||||||||||||||||||
| 121 | 88 | }; | |||||||||||||||||||||||||||||||||||||||||
| 122 | 88 | my $info; $info = $Role::Tiny::INFO{ $target } | |||||||||||||||||||||||||||||||||||||||||
| 123 | 88 | ▶ | and $info->{not_methods}{ $option } = $option; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | 88 | install_sub { as => 'option', into => $target, code => $option, }; | |||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | 88 | $apply_modifiers->(); | |||||||||||||||||||||||||||||||||||||||||
| 128 | 88 | return; | |||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | 18 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | __END__ | ||||||||||||||||||||||||||||||||||||||||||