| 1 | package Getopt::Base; | ||||||||||||||||||||||||||||||||
| 2 | 2 | $VERSION = v0.0.3; | |||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | Getopt::Base - foundation for oo GetOpt support | ||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | package Getopt::YAWTDI; | ||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | use base 'Getopt::Base'; | ||||||||||||||||||||||||||||||||
| 17 | ... | ||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||
| 19 | sub main { | ||||||||||||||||||||||||||||||||
| 20 | my $opt = Getopt::YAWTDI->new(%setup)->process(\@args) or return; | ||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | my $foo = $opt->foo; | ||||||||||||||||||||||||||||||||
| 23 | ... | ||||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | =head1 ABOUT | ||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||
| 28 | This module provides a foundation on which to build numerous forms of | ||||||||||||||||||||||||||||||||
| 29 | Getopt:: support, but does not supply any particular frontend. | ||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | =head1 ALPHA | ||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||
| 33 | This module is still growing. Your help with documentation and API | ||||||||||||||||||||||||||||||||
| 34 | suggestions are welcome. | ||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||
| 36 | =head1 Features | ||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | Modules built on this foundation will have the following features: | ||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||
| 40 | =over | ||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | =item object-based output | ||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | The get() method returns an object with accessors. You may supply your | ||||||||||||||||||||||||||||||||
| 45 | own object. | ||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | =item loadable modes | ||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | A program (such as svn, svk, git) with multiple modes may cleanly load | ||||||||||||||||||||||||||||||||
| 50 | an additional set of options during @args processing. | ||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | =item long/short options, types, &c | ||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||
| 54 | Options are of the --long-form or the '-s' (short form). Short options | ||||||||||||||||||||||||||||||||
| 55 | may be bundled (opterand must follow the bundle.) Long options can be | ||||||||||||||||||||||||||||||||
| 56 | give in one or two-word form (e.g. '--opt=foo' or '--opt foo'.) Options | ||||||||||||||||||||||||||||||||
| 57 | may be 'typed' as boolean/string/integer/float and and be of the single | ||||||||||||||||||||||||||||||||
| 58 | or multi-element array/hash form. All boolean-type options | ||||||||||||||||||||||||||||||||
| 59 | automatically support the '--no-foo' negated form. | ||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||
| 61 | =item ordered callbacks | ||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||
| 63 | Items in C<actions> will be triggered in as-defined order before any of | ||||||||||||||||||||||||||||||||
| 64 | the items in C<options> are processed. This allows for e.g. loading | ||||||||||||||||||||||||||||||||
| 65 | config files or printing help/version messages. | ||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||
| 67 | =item cleanly callable | ||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | It should not be necessary for any callbacks to exit(). If one of them | ||||||||||||||||||||||||||||||||
| 70 | called stop(), then get() returns false and the caller should do the | ||||||||||||||||||||||||||||||||
| 71 | same. Errors will throw an error with croak(). | ||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | =back | ||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | =cut | ||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | =head1 Constructor | ||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||
| 79 | =head2 new | ||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||
| 81 | my $go = Getopt::Base->new(%setup); | ||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | =cut | ||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||
| 85 | 18 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 86 | 18 | my $package = shift; | |||||||||||||||||||||||||||||||
| 87 | 18 | ▶ | my $class = ref($package) || $package; | ||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $package || $package
MC/DC0%0/2
ref $package || $package
ref $package
$package
Truth table33%1/3
A || B
A= ref $packageB= $package
| |||||||||||||||||||||||||||||||||
| 88 | 18 | my $self = { | |||||||||||||||||||||||||||||||
| 89 | opt_data => {}, | ||||||||||||||||||||||||||||||||
| 90 | short => {}, | ||||||||||||||||||||||||||||||||
| 91 | aliases => {}, | ||||||||||||||||||||||||||||||||
| 92 | positional => [], | ||||||||||||||||||||||||||||||||
| 93 | }; | ||||||||||||||||||||||||||||||||
| 94 | 18 | bless($self, $class); | |||||||||||||||||||||||||||||||
| 95 | 18 | $self->_prepare(@_); | |||||||||||||||||||||||||||||||
| 96 | 12 | return($self); | |||||||||||||||||||||||||||||||
| 97 | } # end subroutine new definition | ||||||||||||||||||||||||||||||||
| 98 | ######################################################################## | ||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||
| 100 | =head2 _prepare | ||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||
| 102 | $self->_prepare(%params); | ||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||
| 104 | =cut | ||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||
| 106 | 18 | ▶ | sub _prepare { | ||||||||||||||||||||||||||||||
|
Subroutine
_prepare: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||
| 107 | 18 | my $self = shift; | |||||||||||||||||||||||||||||||
| 108 | 18 | my %params = @_; | |||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||
| 110 | 18 | ▶ | my $options = $params{options} || []; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$params{'options'} || []
MC/DC100%1/1
$params{'options'} || []
$params{'options'}
Truth table100%2/2
A
A= $params{'options'}
| |||||||||||||||||||||||||||||||||
| 111 | 18 | ▶ | (@$options % 2) and croak("odd number of elements in 'options'"); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 112 | 18 | ▶ | for(my $i = 0; $i < @$options; $i+=2) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 113 | 18 | $self->add_option($options->[$i], %{$options->[$i+1]}); | |||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||
| 116 | 15 | ▶ | if(my $pos = $params{positional}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 117 | 7 | $self->add_positionals(@$pos); | |||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||
| 120 | 12 | foreach my $key (qw(arg_handler)) { | |||||||||||||||||||||||||||||||
| 121 | 12 | ▶ | $self->{$key} = $params{$key} if(exists($params{$key})); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||
| 124 | } # end subroutine _prepare definition | ||||||||||||||||||||||||||||||||
| 125 | ######################################################################## | ||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||
| 127 | =head1 Methods | ||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||
| 129 | =head2 process | ||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||
| 131 | Process the @argv, removing options and opterands in-place. | ||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||
| 133 | my $obj = $go->process(\@argv) or return; | ||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||
| 135 | The storage object may also be passed explicitly. | ||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||
| 137 | $obj = $go->process(\@argv, object => $obj) or return; | ||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||
| 139 | =cut | ||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||
| 141 | 37 | ▶ | sub process { | ||||||||||||||||||||||||||||||
|
Subroutine
process: called CC=28 SCAR=36.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 142 | 37 | my $self = shift; | |||||||||||||||||||||||||||||||
| 143 | 37 | my $args = shift; | |||||||||||||||||||||||||||||||
| 144 | 37 | ▶ | (@_ % 2) and croak('odd number of arguments'); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 145 | 37 | my %also = @_; | |||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||
| 147 | 37 | local $self->{stopped} = 0; # loop control | |||||||||||||||||||||||||||||||
| 148 | 37 | my $keep = local $self->{tokeep} = []; | |||||||||||||||||||||||||||||||
| 149 | 37 | my $toset = local $self->{toset} = []; | |||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||
| 151 | 37 | ▶ | my $o = local $self->{object} = $also{object} || $self->object; | ||||||||||||||||||||||||||||||
|
Condition33%1/3
$also{'object'} || $self->object
MC/DC0%0/2
$also{'object'} || $self->object
$also{'object'}
$self->object
Truth table33%1/3
A || B
A= $also{'object'}B= $self->object
| |||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||
| 153 | 37 | ▶ | while(@$args) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 154 | 60 | ▶ | last if($self->{stopped}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 155 | 60 | my $arg = shift(@$args); | |||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||
| 157 | 60 | ▶ | last if($arg eq '--'); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||
| 159 | 57 | my ($dash) = $arg =~ m/^(-*)/; | |||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||
| 161 | 57 | ▶ | if($dash eq '') { $self->process_arg($arg); } | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 162 | elsif($dash eq '--') { | ||||||||||||||||||||||||||||||||
| 163 | 28 | ▶ | if($arg =~ s/=(.*)//) { unshift(@$args, $1); } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 164 | 28 | $self->process_option($self->_find_option($arg), $args); | |||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||
| 166 | elsif($dash eq '-') { | ||||||||||||||||||||||||||||||||
| 167 | 6 | my @got = $self->_unbundle($arg); | |||||||||||||||||||||||||||||||
| 168 | 6 | my $last = pop(@got); | |||||||||||||||||||||||||||||||
| 169 | 6 | $self->process_option($_) for(@got); | |||||||||||||||||||||||||||||||
| 170 | 6 | $self->process_option($last, $args); | |||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||
| 172 | 0 | else { croak("oops: $arg") } | |||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||
| 174 | 37 | @$args = (@$keep, @$args); | |||||||||||||||||||||||||||||||
| 175 | 37 | ▶ | return() if($self->{stopped} < 0); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||
| 177 | 37 | my %is_set = map({$_->[0]->{name} => 1} @$toset); | |||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||
| 179 | # always call hooked options with defined defaults | ||||||||||||||||||||||||||||||||
| 180 | # (or else need to define a setting for this) | ||||||||||||||||||||||||||||||||
| 181 | 37 | foreach my $d ( | |||||||||||||||||||||||||||||||
| 182 | 50 | ▶ | grep {$_->{call} and $_->{default}} values %{$self->{opt_data}} | ||||||||||||||||||||||||||||||
|
Condition33%1/3
$_->{'call'} && $_->{'default'}
MC/DC0%0/2
$_->{'call'} && $_->{'default'}
$_->{'call'}
$_->{'default'}
Truth table33%1/3
A && B
A= $_->{'call'}B= $_->{'default'}
| |||||||||||||||||||||||||||||||||
| 183 | ) { | ||||||||||||||||||||||||||||||||
| 184 | 0 | ▶ | next if $is_set{$d->{name}}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 185 | 0 | my $def = $d->{default}; | |||||||||||||||||||||||||||||||
| 186 | 0 | ▶ | $def = (ref($def) || '') eq 'CODE' ? $def->() : $def; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
ref $def || ''
MC/DC0%0/1
ref $def || ''
ref $def
Truth table0%0/2
A
A= ref $def
| |||||||||||||||||||||||||||||||||
| 187 | 0 | $self->store($d, $def); | |||||||||||||||||||||||||||||||
| 188 | 0 | $d->{call}->($self, $def); | |||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||
| 191 | # store all other inputs | ||||||||||||||||||||||||||||||||
| 192 | 37 | $self->store(@$_) for(@$toset); | |||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||
| 194 | # evaluate positionals | ||||||||||||||||||||||||||||||||
| 195 | 36 | ▶ | if(@$args) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 196 | # TODO this needs better logic for e.g. qw(list scalar scalar) | ||||||||||||||||||||||||||||||||
| 197 | 19 | foreach my $k (@{$self->{positional}}) { | |||||||||||||||||||||||||||||||
| 198 | 17 | ▶ | if(! $is_set{$k} or $self->{opt_data}{$k}{form}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $is_set{$k} or $self->{"opt_data"}{$k}{"form"}
MC/DC50%1/2
not $is_set{$k} or $self->{"opt_data"}{$k}{"form"}
not $is_set{$k}
$self->{"opt_data"}{$k}{"form"}
Truth table66%2/3
not(A) or B
A= not $is_set{$k}B= $self->{"opt_data"}{$k}{"form"}
| |||||||||||||||||||||||||||||||||
| 199 | 6 | $self->store($k, shift(@$args)); | |||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||
| 201 | 17 | ▶ | @$args or last; # TODO check requiredness? | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||
| 205 | # pickup any lazy defaults at this point | ||||||||||||||||||||||||||||||||
| 206 | 36 | ▶ | if(my $def = $self->{_defaults}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 207 | 2 | foreach my $do (@$def) { | |||||||||||||||||||||||||||||||
| 208 | 2 | my ($k, $sub) = @$do; | |||||||||||||||||||||||||||||||
| 209 | 2 | ▶ | next if(exists $o->{$k}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 210 | 2 | ▶ | if(my $isa = $self->{opt_data}{$k}{isa}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 211 | 2 | eval("require $isa"); | |||||||||||||||||||||||||||||||
| 212 | 2 | ▶ | $@ and croak("ack: $@"); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||
| 214 | 2 | $self->store($k, $sub->()); | |||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||
| 218 | 36 | return($o); | |||||||||||||||||||||||||||||||
| 219 | } # end subroutine process definition | ||||||||||||||||||||||||||||||||
| 220 | ######################################################################## | ||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||
| 222 | =head1 Controlling process() | ||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||
| 224 | =head2 stop | ||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||
| 226 | Stops the option processing when called from an action handler. Always | ||||||||||||||||||||||||||||||||
| 227 | returns false. | ||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||
| 229 | $go->stop; | ||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||
| 231 | This is used for some forms of two-stage processing, where an action or | ||||||||||||||||||||||||||||||||
| 232 | argument indicates that all of the remaining inputs are to be handled | ||||||||||||||||||||||||||||||||
| 233 | elsewhere. | ||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||
| 235 | =head2 quit | ||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||
| 237 | Stops the option processing and prevents process() from returning an object . Always returns false. | ||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||
| 239 | $go->quit; | ||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||
| 241 | This is used for options like C<--version> and C<--help>, where you have | ||||||||||||||||||||||||||||||||
| 242 | a terminal action. | ||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||
| 244 | =cut | ||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||
| 246 | 0 | ▶ | sub stop { shift->{stopped} = 1; return(); } | ||||||||||||||||||||||||||||||
|
Subroutine
stop: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 247 | 0 | ▶ | sub quit { shift->{stopped} = -1; return(); } | ||||||||||||||||||||||||||||||
|
Subroutine
quit: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 248 | ######################################################################## | ||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||
| 250 | =head1 Handling Inputs | ||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||
| 252 | =head2 process_option | ||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||
| 254 | $self->process_option($name, \@argv); | ||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||
| 256 | =cut | ||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||
| 258 | 34 | ▶ | sub process_option { | ||||||||||||||||||||||||||||||
|
Subroutine
process_option: called CC=12 SCAR=26.4
Pod
documented
| |||||||||||||||||||||||||||||||||
| 259 | 34 | my $self = shift; | |||||||||||||||||||||||||||||||
| 260 | 34 | my ($name, $argv) = @_; | |||||||||||||||||||||||||||||||
| 261 | 34 | ▶ | $argv ||= []; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$argv ||= []
MC/DC0%0/1
$argv ||= []
$argv
Truth table50%1/2
A
A= $argv
| |||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||
| 263 | 34 | ▶ | my $toset = $self->{toset} or croak("out of context"); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||
| 265 | 34 | ▶ | my $d = ref($name) ? $name : $self->{opt_data}{$name} or | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 266 | croak("invalid: $name"); | ||||||||||||||||||||||||||||||||
| 267 | 34 | $name = $d->{name}; | |||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||
| 269 | 34 | my $v; | |||||||||||||||||||||||||||||||
| 270 | 34 | ▶ | if($d->{type} eq 'boolean') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 271 | 13 | ▶ | $v = $d->{opposes} ? 0 : 1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||
| 273 | else { | ||||||||||||||||||||||||||||||||
| 274 | 21 | ▶ | @$argv or croak("option '$d->{name}' requires a value"); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 275 | 21 | $v = shift(@$argv); | |||||||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||
| 278 | 34 | ▶ | if(my $sub = $d->{call}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 279 | # TODO should we try to set a value? | ||||||||||||||||||||||||||||||||
| 280 | # TODO this should probably also be in the store() routine? | ||||||||||||||||||||||||||||||||
| 281 | 0 | my $check = $self->_checker($name); | |||||||||||||||||||||||||||||||
| 282 | 0 | push(@$toset, [$d, $v]); | |||||||||||||||||||||||||||||||
| 283 | 0 | return $sub->($self, $check->($v)); | |||||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||||||
| 285 | else { | ||||||||||||||||||||||||||||||||
| 286 | 34 | ▶ | if(($d->{form}||'') eq 'HASH') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$d->{'form'} || ''
MC/DC100%1/1
$d->{'form'} || ''
$d->{'form'}
Truth table100%2/2
A
A= $d->{'form'}
| |||||||||||||||||||||||||||||||||
| 287 | 3 | my @pair = split(/=/, $v, 2); | |||||||||||||||||||||||||||||||
| 288 | 3 | ▶ | croak("hash options require 'key=value' form (not '$v')") | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 289 | unless(@pair == 2); | ||||||||||||||||||||||||||||||||
| 290 | 3 | push(@$toset, [$d, @pair]); | |||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||||
| 292 | else { | ||||||||||||||||||||||||||||||||
| 293 | 31 | push(@$toset, [$d, $v]); | |||||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||||||
| 296 | } # end subroutine process_option definition | ||||||||||||||||||||||||||||||||
| 297 | ######################################################################## | ||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||
| 299 | =head2 process_arg | ||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||
| 301 | $self->process_arg($arg); | ||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||
| 303 | =cut | ||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||
| 305 | 23 | ▶ | sub process_arg { | ||||||||||||||||||||||||||||||
|
Subroutine
process_arg: called CC=4 SCAR=16.4
Pod
documented
| |||||||||||||||||||||||||||||||||
| 306 | 23 | my $self = shift; | |||||||||||||||||||||||||||||||
| 307 | 23 | my ($arg) = @_; | |||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||
| 309 | 23 | ▶ | my $keep = $self->{tokeep} or croak("out of context"); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||
| 311 | # check for mode | ||||||||||||||||||||||||||||||||
| 312 | 23 | ▶ | if(my $do = $self->{arg_handler}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 313 | # XXX what's the API for this? Return vs stop and so on. | ||||||||||||||||||||||||||||||||
| 314 | 0 | ▶ | $do->($self, $arg) or return; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||
| 317 | 23 | push(@$keep, $arg); | |||||||||||||||||||||||||||||||
| 318 | } # end subroutine process_arg definition | ||||||||||||||||||||||||||||||||
| 319 | ######################################################################## | ||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||
| 321 | =head1 Setup | ||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||
| 323 | =head2 add_option | ||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||
| 325 | Add an option. | ||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||
| 327 | $go->add_option(name => %settings); | ||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||
| 329 | =cut | ||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||
| 331 | 22 | ▶ | sub add_option { | ||||||||||||||||||||||||||||||
|
Subroutine
add_option: called CC=15 SCAR=28.6
Pod
documented
| |||||||||||||||||||||||||||||||||
| 332 | 22 | my $self = shift; | |||||||||||||||||||||||||||||||
| 333 | 22 | my $name = shift; | |||||||||||||||||||||||||||||||
| 334 | 22 | ▶ | (@_ % 2) and croak("odd number of arguments"); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 335 | 22 | my %s = @_; | |||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||
| 337 | 22 | ▶ | croak("options cannot contain dashes ('$name')") if($name =~ m/-/); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 338 | 21 | ▶ | unless($s{form}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 339 | 20 | my $ref = ref($s{default}); | |||||||||||||||||||||||||||||||
| 340 | 20 | ▶ | $s{form} = $ref if($ref and $ref ne 'CODE'); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$ref and $ref ne "CODE"
MC/DC100%2/2
$ref and $ref ne "CODE"
$ref
$ref ne "CODE"
Truth table100%3/3
A and B
A= $refB= $ref ne "CODE"
| |||||||||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||||||
| 342 | else { | ||||||||||||||||||||||||||||||||
| 343 | 1 | $s{form} = uc($s{form}); | |||||||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||
| 346 | 21 | ▶ | unless($s{type}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 347 | 6 | ▶ | $s{type} = $s{form} ? 'string' : 'boolean'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||
| 350 | 21 | ▶ | if(my $callback = $s{call}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 351 | 0 | ▶ | croak("not a code reference") unless(ref($callback) ||'' eq 'CODE'); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
ref $callback or
MC/DC0%0/1
ref $callback or
ref $callback
Truth table0%0/2
A
A= ref $callback
| |||||||||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||
| 354 | 21 | $s{name} = $name; # XXX I guess | |||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||
| 356 | 21 | ▶ | if($self->{opt_data}{$name}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 357 | # warn "$name already defined\n"; | ||||||||||||||||||||||||||||||||
| 358 | # TODO no big deal? | ||||||||||||||||||||||||||||||||
| 359 | 0 | ▶ | croak("option '$name' already defined") unless($name =~ m/^no_/); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||||||
| 361 | else { | ||||||||||||||||||||||||||||||||
| 362 | 21 | $self->{opt_data}{$name} = \%s; | |||||||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||
| 365 | 21 | ▶ | if($s{type} eq 'boolean') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 366 | 6 | $self->{opt_data}{"no_$name"} = {%s, opposes => $name}; | |||||||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||
| 369 | 21 | ▶ | $self->add_aliases($name => $s{short}, @{$s{aliases} || []}); | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$s{'aliases'} || []
MC/DC100%1/1
$s{'aliases'} || []
$s{'aliases'}
Truth table100%2/2
A
A= $s{'aliases'}
| |||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||
| 371 | } # end subroutine add_option definition | ||||||||||||||||||||||||||||||||
| 372 | ######################################################################## | ||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||
| 374 | # TODO this is only sugar then? | ||||||||||||||||||||||||||||||||
| 375 | # =head2 add_action | ||||||||||||||||||||||||||||||||
| 376 | # | ||||||||||||||||||||||||||||||||
| 377 | # $go->add_action(name => sub {...}, %settings); | ||||||||||||||||||||||||||||||||
| 378 | # | ||||||||||||||||||||||||||||||||
| 379 | # =cut | ||||||||||||||||||||||||||||||||
| 380 | # | ||||||||||||||||||||||||||||||||
| 381 | # sub add_action { | ||||||||||||||||||||||||||||||||
| 382 | # my $self = shift; | ||||||||||||||||||||||||||||||||
| 383 | # my ($name, $callback, @and) = @_; | ||||||||||||||||||||||||||||||||
| 384 | # | ||||||||||||||||||||||||||||||||
| 385 | # $self->add_option($name, @and, call => $callback); | ||||||||||||||||||||||||||||||||
| 386 | # } # end subroutine add_action definition | ||||||||||||||||||||||||||||||||
| 387 | # ######################################################################## | ||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||
| 389 | =head2 add_positionals | ||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||
| 391 | $go->add_positionals(@list); | ||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||
| 393 | =cut | ||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||
| 395 | 7 | ▶ | sub add_positionals { | ||||||||||||||||||||||||||||||
|
Subroutine
add_positionals: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 396 | 7 | my $self = shift; | |||||||||||||||||||||||||||||||
| 397 | 7 | my (@list) = @_; | |||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||
| 399 | 7 | foreach my $item (@list) { | |||||||||||||||||||||||||||||||
| 400 | 8 | ▶ | my $d = $self->{opt_data}{$item} or | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 401 | croak("positional '$item' is not an option"); | ||||||||||||||||||||||||||||||||
| 402 | croak("positional '$item' cannot be a boolean") | ||||||||||||||||||||||||||||||||
| 403 | 6 | ▶ | if($d->{type} eq 'boolean'); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 404 | 5 | push(@{$self->{positional}}, $item); | |||||||||||||||||||||||||||||||
| 405 | } | ||||||||||||||||||||||||||||||||
| 406 | } # end subroutine add_positionals definition | ||||||||||||||||||||||||||||||||
| 407 | ######################################################################## | ||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||
| 409 | =head2 add_aliases | ||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||
| 411 | $go->add_aliases($canonical => \@short, @list); | ||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||
| 413 | =cut | ||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||
| 415 | 22 | ▶ | sub add_aliases { | ||||||||||||||||||||||||||||||
|
Subroutine
add_aliases: called CC=9 SCAR=22.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 416 | 22 | my $self = shift; | |||||||||||||||||||||||||||||||
| 417 | 22 | my ($canon, $short, @and) = @_; | |||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||
| 419 | 22 | ▶ | if(defined($short)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 420 | 6 | my $st = $self->{short}; | |||||||||||||||||||||||||||||||
| 421 | 6 | ▶ | ref($short) or croak("'shortlist' argument must be an array ref"); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 422 | 6 | foreach my $item (@$short) { | |||||||||||||||||||||||||||||||
| 423 | 9 | ▶ | croak("short options must be only one character ('$item')") | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 424 | if(length($item) != 1); | ||||||||||||||||||||||||||||||||
| 425 | croak("short option '$item' is already linked to '$st->{$item}'") | ||||||||||||||||||||||||||||||||
| 426 | 8 | ▶ | if(exists($st->{$item})); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 427 | 8 | $st->{$item} = $canon; | |||||||||||||||||||||||||||||||
| 428 | } | ||||||||||||||||||||||||||||||||
| 429 | } | ||||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||
| 431 | 21 | my $at = $self->{aliases}; | |||||||||||||||||||||||||||||||
| 432 | 21 | foreach my $item (@and) { | |||||||||||||||||||||||||||||||
| 433 | 5 | ▶ | croak("aliases cannot contain dashes ('$item')") if($item =~ m/-/); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 434 | croak("alias '$item' is already linked to '$at->{$item}'") | ||||||||||||||||||||||||||||||||
| 435 | 4 | ▶ | if(exists($at->{$item})); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 436 | 4 | $at->{$item} = $canon; | |||||||||||||||||||||||||||||||
| 437 | } | ||||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||||
| 439 | } # end subroutine add_aliases definition | ||||||||||||||||||||||||||||||||
| 440 | ######################################################################## | ||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||
| 442 | =head2 store | ||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||
| 444 | $go->store(key => $value, $value2, ...); | ||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||
| 446 | =cut | ||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||
| 448 | 42 | ▶ | sub store { | ||||||||||||||||||||||||||||||
|
Subroutine
store: called CC=9 SCAR=22.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 449 | 42 | my $self = shift; | |||||||||||||||||||||||||||||||
| 450 | 42 | my ($k, @v) = @_; | |||||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||||||
| 452 | 42 | ▶ | my $o = $self->{object} or croak("out of context"); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 453 | 42 | ▶ | my $d = ref($k) ? $k : $self->{opt_data}{$k} or | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 454 | croak("no such option: $k"); | ||||||||||||||||||||||||||||||||
| 455 | 42 | $k = $d->{name}; | |||||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||||||
| 457 | 42 | my $check = $self->_checker($k); | |||||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||||||
| 459 | 41 | ▶ | if(my $form = $d->{form}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 460 | 8 | ▶ | if($form eq 'HASH') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 461 | 3 | ▶ | $o->{$k} ||= {}; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$o->{$k} ||= {}
MC/DC0%0/1
$o->{$k} ||= {}
$o->{$k}
Truth table50%1/2
A
A= $o->{$k}
| |||||||||||||||||||||||||||||||||
| 462 | 3 | ▶ | (@v % 2) and croak("odd number of values to store for '$k'"); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 463 | 3 | ▶ | while(@v) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 464 | 3 | my $key = shift(@v); my $val = shift(@v); | |||||||||||||||||||||||||||||||
| 465 | 3 | $o->{$k}{$key} = $check->($val); | |||||||||||||||||||||||||||||||
| 466 | } | ||||||||||||||||||||||||||||||||
| 467 | } | ||||||||||||||||||||||||||||||||
| 468 | else { | ||||||||||||||||||||||||||||||||
| 469 | 5 | push(@{$o->{$k}}, map({$check->($_)} @v)); | |||||||||||||||||||||||||||||||
| 470 | } | ||||||||||||||||||||||||||||||||
| 471 | } | ||||||||||||||||||||||||||||||||
| 472 | else { | ||||||||||||||||||||||||||||||||
| 473 | 33 | $o->{$k} = $check->($v[0]); | |||||||||||||||||||||||||||||||
| 474 | } | ||||||||||||||||||||||||||||||||
| 475 | } # end subroutine store definition | ||||||||||||||||||||||||||||||||
| 476 | ######################################################################## | ||||||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||||||
| 478 | =head2 _checker | ||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||
| 480 | Builds a check subref for the given $name. | ||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||
| 482 | my $subref = $self->_checker($name); | ||||||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||||||
| 484 | =cut | ||||||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||||||
| 486 | 58 | ▶ | sub _checker { | ||||||||||||||||||||||||||||||
|
Subroutine
_checker: called CC=6 SCAR=18.1
| |||||||||||||||||||||||||||||||||
| 487 | 58 | my $self = shift; | |||||||||||||||||||||||||||||||
| 488 | 58 | my ($item) = @_; | |||||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||||||
| 490 | 58 | ▶ | my $d = $self->{opt_data}{$item} or die("nothing for $item"); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||||||
| 492 | 58 | my $checkcode = ''; | |||||||||||||||||||||||||||||||
| 493 | 58 | ▶ | if(my $isa = $d->{isa}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 494 | 5 | eval("require $isa"); | |||||||||||||||||||||||||||||||
| 495 | 5 | ▶ | $@ and croak("ack: $@"); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 496 | 4 | $checkcode .= '$val = ' . "$isa" . '->new($val) ' . | |||||||||||||||||||||||||||||||
| 497 | " unless(eval {\$val->isa('$isa')});"; | ||||||||||||||||||||||||||||||||
| 498 | } | ||||||||||||||||||||||||||||||||
| 499 | 57 | ▶ | if(my $type = $d->{type}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 500 | # TODO check integer/number-ness | ||||||||||||||||||||||||||||||||
| 501 | } | ||||||||||||||||||||||||||||||||
| 502 | 57 | my $check = eval("sub { | |||||||||||||||||||||||||||||||
| 503 | my \$val = shift; | ||||||||||||||||||||||||||||||||
| 504 | $checkcode | ||||||||||||||||||||||||||||||||
| 505 | return(\$val); | ||||||||||||||||||||||||||||||||
| 506 | }"); | ||||||||||||||||||||||||||||||||
| 507 | 57 | ▶ | $@ and die "ouch $@"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||||||
| 509 | 57 | return($check); | |||||||||||||||||||||||||||||||
| 510 | } # _checker ########################################################### | ||||||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||||||
| 512 | =head2 set_values | ||||||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||||||
| 514 | $go->set_values(%hash); | ||||||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||||||
| 516 | =cut | ||||||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||||||
| 518 | 0 | ▶ | sub set_values { | ||||||||||||||||||||||||||||||
|
Subroutine
set_values: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 519 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 520 | 0 | my %hash = @_; | |||||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||||||
| 522 | 0 | foreach my $k (keys %hash) { | |||||||||||||||||||||||||||||||
| 523 | # XXX I need to think about whether this has exceptional cases | ||||||||||||||||||||||||||||||||
| 524 | 0 | my $v = $hash{$k}; | |||||||||||||||||||||||||||||||
| 525 | 0 | my $ref = ref($v); | |||||||||||||||||||||||||||||||
| 526 | 0 | ▶ | $self->store($k, $ref | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 527 | ? $ref eq 'HASH' | ||||||||||||||||||||||||||||||||
| 528 | ? %$v | ||||||||||||||||||||||||||||||||
| 529 | : $ref eq 'ARRAY' | ||||||||||||||||||||||||||||||||
| 530 | ? @$v | ||||||||||||||||||||||||||||||||
| 531 | : $v | ||||||||||||||||||||||||||||||||
| 532 | : $v); | ||||||||||||||||||||||||||||||||
| 533 | } | ||||||||||||||||||||||||||||||||
| 534 | } # end subroutine set_values definition | ||||||||||||||||||||||||||||||||
| 535 | ######################################################################## | ||||||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||||||
| 537 | =head2 object | ||||||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||||||
| 539 | Default/current result-storage object. Subclasses may wish to | ||||||||||||||||||||||||||||||||
| 540 | override this. | ||||||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||||||
| 542 | my $obj = $go->object; | ||||||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||||||
| 544 | =cut | ||||||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||||||
| 546 | 37 | ▶ | sub object { | ||||||||||||||||||||||||||||||
|
Subroutine
object: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 547 | 37 | my $self = shift; | |||||||||||||||||||||||||||||||
| 548 | 37 | ▶ | return $self->{object} if($self->{object}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||
| 550 | 37 | return $self->make_object; | |||||||||||||||||||||||||||||||
| 551 | } # end subroutine object definition | ||||||||||||||||||||||||||||||||
| 552 | ######################################################################## | ||||||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||||||
| 554 | =head2 make_object | ||||||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||||||
| 556 | Constructs an empty (with defaults) data object from the set options. | ||||||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||||||
| 558 | my $obj = $self->make_object; | ||||||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||||||
| 560 | =cut | ||||||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||||||
| 562 | 37 | ▶ | sub make_object { | ||||||||||||||||||||||||||||||
|
Subroutine
make_object: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 563 | 37 | my $self = shift; | |||||||||||||||||||||||||||||||
| 564 | 37 | my $obj = Getopt::Base::Accessors->new($self->{opt_data}); | |||||||||||||||||||||||||||||||
| 565 | # XXX should find a nicer way to pass these around | ||||||||||||||||||||||||||||||||
| 566 | 37 | $self->{_defaults} = delete $obj->{__defaults}; | |||||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||||||
| 568 | # XXX ugly, but we need to honor isa on default values | ||||||||||||||||||||||||||||||||
| 569 | 37 | foreach my $k (keys %$obj) { | |||||||||||||||||||||||||||||||
| 570 | 16 | my $checker = $self->_checker($k); | |||||||||||||||||||||||||||||||
| 571 | 16 | $obj->{$k} = $checker->($obj->{$k}); | |||||||||||||||||||||||||||||||
| 572 | } | ||||||||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||||||
| 574 | 37 | return $obj; | |||||||||||||||||||||||||||||||
| 575 | } # make_object ######################################################## | ||||||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||||||
| 578 | =head2 _find_option | ||||||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||||||
| 580 | Fetches the option data for the canonical match (de-aliased) of $opt. | ||||||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||||||
| 582 | my $d = $self->_find_option($opt); | ||||||||||||||||||||||||||||||||
| 583 | |||||||||||||||||||||||||||||||||
| 584 | =cut | ||||||||||||||||||||||||||||||||
| 585 | |||||||||||||||||||||||||||||||||
| 586 | 28 | ▶ | sub _find_option { | ||||||||||||||||||||||||||||||
|
Subroutine
_find_option: called CC=6 SCAR=18.1
| |||||||||||||||||||||||||||||||||
| 587 | 28 | my $self = shift; | |||||||||||||||||||||||||||||||
| 588 | 28 | my ($opt) = @_; | |||||||||||||||||||||||||||||||
| 589 | |||||||||||||||||||||||||||||||||
| 590 | 28 | my $key = $opt; | |||||||||||||||||||||||||||||||
| 591 | 28 | $key =~ s/^--//; $key =~ s/-/_/g; | |||||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||||||
| 593 | # exact match | ||||||||||||||||||||||||||||||||
| 594 | 28 | ▶ | if(my $d = $self->{opt_data}{$key}) { return($d); } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||||||
| 596 | 40 | my @hit = grep({$_ =~ m/^$key/} | |||||||||||||||||||||||||||||||
| 597 | 11 | keys %{$self->{aliases}}, | |||||||||||||||||||||||||||||||
| 598 | 11 | keys %{$self->{opt_data}} | |||||||||||||||||||||||||||||||
| 599 | ); | ||||||||||||||||||||||||||||||||
| 600 | 11 | ▶ | croak("option '$opt' is invalid") unless(@hit); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 601 | 11 | ▶ | croak("option '$opt' is not long enough to be unique") if(@hit > 1); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 602 | |||||||||||||||||||||||||||||||||
| 603 | 11 | ▶ | my $canon = $self->{aliases}{$hit[0]} || $hit[0]; | ||||||||||||||||||||||||||||||
|
Condition66%2/3
$self->{'aliases'}{$hit[0]} || $hit[0]
MC/DC0%0/2
$self->{'aliases'}{$hit[0]} || $hit[0]
$self->{'aliases'}{$hit[0]}
$hit[0]
Truth table66%2/3
A || B
A= $self->{'aliases'}{$hit[0]}B= $hit[0]
| |||||||||||||||||||||||||||||||||
| 604 | 11 | ▶ | my $d = $self->{opt_data}{$canon} or | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 605 | croak("alias '$hit[0]' has no canonical form ($canon)"); | ||||||||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||||||
| 607 | 11 | return($d); | |||||||||||||||||||||||||||||||
| 608 | } # end subroutine _find_option definition | ||||||||||||||||||||||||||||||||
| 609 | ######################################################################## | ||||||||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||||||
| 611 | =head2 _unbundle | ||||||||||||||||||||||||||||||||
| 612 | |||||||||||||||||||||||||||||||||
| 613 | my @d = $self->_unbundle($blah); | ||||||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||||||
| 615 | =cut | ||||||||||||||||||||||||||||||||
| 616 | |||||||||||||||||||||||||||||||||
| 617 | 6 | ▶ | sub _unbundle { | ||||||||||||||||||||||||||||||
|
Subroutine
_unbundle: called CC=6 SCAR=19.3
| |||||||||||||||||||||||||||||||||
| 618 | 6 | my $self = shift; | |||||||||||||||||||||||||||||||
| 619 | 6 | my $bun = shift; | |||||||||||||||||||||||||||||||
| 620 | 6 | $bun =~ s/^-//; | |||||||||||||||||||||||||||||||
| 621 | |||||||||||||||||||||||||||||||||
| 622 | 6 | my @d; | |||||||||||||||||||||||||||||||
| 623 | 6 | foreach my $c (split(//, $bun)) { | |||||||||||||||||||||||||||||||
| 624 | 6 | ▶ | my $canon = $self->{short}{$c} or | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 625 | croak("short option '$c' is not defined"); | ||||||||||||||||||||||||||||||||
| 626 | 6 | ▶ | my $data = $self->{opt_data}{$canon} or | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 627 | croak("short option '$c' points to non-existent '$canon'"); | ||||||||||||||||||||||||||||||||
| 628 | 6 | push(@d, $data); | |||||||||||||||||||||||||||||||
| 629 | } | ||||||||||||||||||||||||||||||||
| 630 | |||||||||||||||||||||||||||||||||
| 631 | 6 | foreach my $i (0..($#d-1)) { | |||||||||||||||||||||||||||||||
| 632 | croak("option '$d[$i]->{name}' is not a bundle-able flag") | ||||||||||||||||||||||||||||||||
| 633 | 0 | ▶ | unless($d[$i]->{type} eq 'boolean'); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 634 | } | ||||||||||||||||||||||||||||||||
| 635 | 6 | return(@d); | |||||||||||||||||||||||||||||||
| 636 | } # end subroutine _unbundle definition | ||||||||||||||||||||||||||||||||
| 637 | ######################################################################## | ||||||||||||||||||||||||||||||||
| 638 | |||||||||||||||||||||||||||||||||
| 639 | { | ||||||||||||||||||||||||||||||||
| 640 | package Getopt::Base::Accessors; | ||||||||||||||||||||||||||||||||
| 641 | |||||||||||||||||||||||||||||||||
| 642 | =head1 Accessor Class | ||||||||||||||||||||||||||||||||
| 643 | |||||||||||||||||||||||||||||||||
| 644 | This is the default object for holding results. It will contain | ||||||||||||||||||||||||||||||||
| 645 | accessors for all of the defined options. | ||||||||||||||||||||||||||||||||
| 646 | |||||||||||||||||||||||||||||||||
| 647 | =head2 new | ||||||||||||||||||||||||||||||||
| 648 | |||||||||||||||||||||||||||||||||
| 649 | my $o = Getopt::Base::Accessors->new($opt_data); | ||||||||||||||||||||||||||||||||
| 650 | |||||||||||||||||||||||||||||||||
| 651 | =cut | ||||||||||||||||||||||||||||||||
| 652 | |||||||||||||||||||||||||||||||||
| 653 | 37 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=13 SCAR=25.7
| |||||||||||||||||||||||||||||||||
| 654 | 37 | my $class = shift; | |||||||||||||||||||||||||||||||
| 655 | 37 | my $opt_data = shift; | |||||||||||||||||||||||||||||||
| 656 | |||||||||||||||||||||||||||||||||
| 657 | 37 | my $self = {}; | |||||||||||||||||||||||||||||||
| 658 | |||||||||||||||||||||||||||||||||
| 659 | 37 | $class .= "::$self"; | |||||||||||||||||||||||||||||||
| 660 | |||||||||||||||||||||||||||||||||
| 661 | 37 | bless($self, $class); | |||||||||||||||||||||||||||||||
| 662 | |||||||||||||||||||||||||||||||||
| 663 | 37 | foreach my $k (keys %$opt_data) { | |||||||||||||||||||||||||||||||
| 664 | # warn "$k\n"; | ||||||||||||||||||||||||||||||||
| 665 | 50 | my $o = $opt_data->{$k}; | |||||||||||||||||||||||||||||||
| 666 | 50 | ▶ | next if(($o->{type} ||'' eq 'boolean') and $o->{opposes}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition60%3/5
and $o->{"opposes"}
$o->{"type"} or
MC/DC50%1/2
and $o->{"opposes"}
$o->{"type"}
$o->{"opposes"}
Truth table66%2/3
A and B
A= $o->{"type"}B= $o->{"opposes"}
| |||||||||||||||||||||||||||||||||
| 667 | 38 | my $sub; | |||||||||||||||||||||||||||||||
| 668 | 38 | ▶ | if(my $r = $o->{form}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 669 | # warn "form for $k : $r"; | ||||||||||||||||||||||||||||||||
| 670 | 3 | my $def = $o->{default}; | |||||||||||||||||||||||||||||||
| 671 | 3 | ▶ | if($r eq 'HASH') { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 672 | 1 | ▶ | $self->{$k} = {$def ? %$def : ()}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 673 | 1 | $sub = eval("sub {\%{shift->{$k}}}"); | |||||||||||||||||||||||||||||||
| 674 | } | ||||||||||||||||||||||||||||||||
| 675 | elsif($r eq 'ARRAY') { | ||||||||||||||||||||||||||||||||
| 676 | 2 | ▶ | $self->{$k} = [$def ? @$def : ()]; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 677 | 2 | $sub = eval("sub {\@{shift->{$k}}}"); | |||||||||||||||||||||||||||||||
| 678 | } | ||||||||||||||||||||||||||||||||
| 679 | else { | ||||||||||||||||||||||||||||||||
| 680 | 0 | Carp::croak("unknown ref type '$r'"); | |||||||||||||||||||||||||||||||
| 681 | } | ||||||||||||||||||||||||||||||||
| 682 | } | ||||||||||||||||||||||||||||||||
| 683 | else { | ||||||||||||||||||||||||||||||||
| 684 | 35 | $sub = eval("sub {shift->{$k}}"); | |||||||||||||||||||||||||||||||
| 685 | 35 | ▶ | if(exists $o->{default}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 686 | 15 | my $def = $o->{default}; | |||||||||||||||||||||||||||||||
| 687 | 15 | ▶ | if((ref($def)||'') eq 'CODE') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
ref $def || ''
MC/DC100%1/1
ref $def || ''
ref $def
Truth table100%2/2
A
A= ref $def
| |||||||||||||||||||||||||||||||||
| 688 | # lazy | ||||||||||||||||||||||||||||||||
| 689 | 2 | push(@{$self->{__defaults}}, [$k, $def]); | |||||||||||||||||||||||||||||||
| 690 | } | ||||||||||||||||||||||||||||||||
| 691 | else { | ||||||||||||||||||||||||||||||||
| 692 | 13 | $self->{$k} = $def | |||||||||||||||||||||||||||||||
| 693 | } | ||||||||||||||||||||||||||||||||
| 694 | } | ||||||||||||||||||||||||||||||||
| 695 | } | ||||||||||||||||||||||||||||||||
| 696 | { | ||||||||||||||||||||||||||||||||
| 697 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 698 | 38 | *{$class . '::' . $k} = $sub; | |||||||||||||||||||||||||||||||
| 699 | } | ||||||||||||||||||||||||||||||||
| 700 | } | ||||||||||||||||||||||||||||||||
| 701 | # and we need to cleanup this object class | ||||||||||||||||||||||||||||||||
| 702 | 37 | ▶ | my $destroy = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 703 | 2 | ▶ | my $st = do { no strict 'refs'; \%{$class . '::'}}; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 704 | 37 | delete($st->{$_}) for(keys %$st); | |||||||||||||||||||||||||||||||
| 705 | 37 | return; | |||||||||||||||||||||||||||||||
| 706 | 37 | }; | |||||||||||||||||||||||||||||||
| 707 | 2 | ▶ | { no strict 'refs'; *{$class . '::' . 'DESTROY'} = $destroy; } | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 708 | |||||||||||||||||||||||||||||||||
| 709 | 37 | return $self; | |||||||||||||||||||||||||||||||
| 710 | } # end subroutine new definition | ||||||||||||||||||||||||||||||||
| 711 | ######################################################################## | ||||||||||||||||||||||||||||||||
| 712 | |||||||||||||||||||||||||||||||||
| 713 | 2 | }; | |||||||||||||||||||||||||||||||
| 714 | |||||||||||||||||||||||||||||||||
| 715 | |||||||||||||||||||||||||||||||||
| 716 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||
| 717 | |||||||||||||||||||||||||||||||||
| 718 | Eric Wilhelm @ <ewilhelm at cpan dot org> | ||||||||||||||||||||||||||||||||
| 719 | |||||||||||||||||||||||||||||||||
| 720 | http://scratchcomputing.com/ | ||||||||||||||||||||||||||||||||
| 721 | |||||||||||||||||||||||||||||||||
| 722 | =head1 BUGS | ||||||||||||||||||||||||||||||||
| 723 | |||||||||||||||||||||||||||||||||
| 724 | If you found this module on CPAN, please report any bugs or feature | ||||||||||||||||||||||||||||||||
| 725 | requests through the web interface at L<http://rt.cpan.org>. I will be | ||||||||||||||||||||||||||||||||
| 726 | notified, and then you'll automatically be notified of progress on your | ||||||||||||||||||||||||||||||||
| 727 | bug as I make changes. | ||||||||||||||||||||||||||||||||
| 728 | |||||||||||||||||||||||||||||||||
| 729 | If you pulled this development version from my /svn/, please contact me | ||||||||||||||||||||||||||||||||
| 730 | directly. | ||||||||||||||||||||||||||||||||
| 731 | |||||||||||||||||||||||||||||||||
| 732 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||||||
| 733 | |||||||||||||||||||||||||||||||||
| 734 | Copyright (C) 2009 Eric L. Wilhelm, All Rights Reserved. | ||||||||||||||||||||||||||||||||
| 735 | |||||||||||||||||||||||||||||||||
| 736 | =head1 NO WARRANTY | ||||||||||||||||||||||||||||||||
| 737 | |||||||||||||||||||||||||||||||||
| 738 | Absolutely, positively NO WARRANTY, neither express or implied, is | ||||||||||||||||||||||||||||||||
| 739 | offered with this software. You use this software at your own risk. In | ||||||||||||||||||||||||||||||||
| 740 | case of loss, no person or entity owes you anything whatsoever. You | ||||||||||||||||||||||||||||||||
| 741 | have been warned. | ||||||||||||||||||||||||||||||||
| 742 | |||||||||||||||||||||||||||||||||
| 743 | =head1 LICENSE | ||||||||||||||||||||||||||||||||
| 744 | |||||||||||||||||||||||||||||||||
| 745 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||||||
| 746 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||
| 747 | |||||||||||||||||||||||||||||||||
| 748 | =cut | ||||||||||||||||||||||||||||||||
| 749 | |||||||||||||||||||||||||||||||||
| 750 | # vi:ts=2:sw=2:et:sta | ||||||||||||||||||||||||||||||||
| 751 | 2 | 1; | |||||||||||||||||||||||||||||||