| 1 | package Getopt::Kingpin; | ||||||||||||||||||||||||||||
| 2 | 28 | ▶ | use 5.008001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 28 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 28 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 28 | ▶ | use Object::Simple -base; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 28 | ▶ | use Getopt::Kingpin::Flags; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 28 | ▶ | use Getopt::Kingpin::Args; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 28 | ▶ | use Getopt::Kingpin::Commands; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 28 | ▶ | use File::Basename; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 28 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 28 | our $VERSION = "0.09"; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | use overload ( | ||||||||||||||||||||||||||||
| 15 | 6 | ▶ | '""' => sub {$_[0]->name}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 28 | fallback => 1, | |||||||||||||||||||||||||||
| 17 | 28 | ▶ | ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | has flags => sub { | ||||||||||||||||||||||||||||
| 20 | 170 | ▶ | my $flags = Getopt::Kingpin::Flags->new; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 170 | $flags->add( | |||||||||||||||||||||||||||
| 22 | name => 'help', | ||||||||||||||||||||||||||||
| 23 | description => 'Show context-sensitive help.', | ||||||||||||||||||||||||||||
| 24 | )->bool(); | ||||||||||||||||||||||||||||
| 25 | 170 | return $flags; | |||||||||||||||||||||||||||
| 26 | 28 | }; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | has args => sub { | ||||||||||||||||||||||||||||
| 29 | 133 | ▶ | my $args = Getopt::Kingpin::Args->new; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 133 | return $args; | |||||||||||||||||||||||||||
| 31 | 28 | }; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | has commands => sub { | ||||||||||||||||||||||||||||
| 34 | 113 | ▶ | my $commands = Getopt::Kingpin::Commands->new; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 113 | return $commands; | |||||||||||||||||||||||||||
| 36 | 28 | }; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | has _version => sub { | ||||||||||||||||||||||||||||
| 39 | 0 | ▶ | return ""; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 40 | 28 | }; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | has parent => sub { | ||||||||||||||||||||||||||||
| 43 | return | ||||||||||||||||||||||||||||
| 44 | 128 | ▶ | }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | has name => sub { | ||||||||||||||||||||||||||||
| 47 | 47 | ▶ | return basename($0); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 48 | 28 | }; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | has description => sub { | ||||||||||||||||||||||||||||
| 51 | 23 | ▶ | return ""; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 52 | 28 | }; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | has terminate => sub { | ||||||||||||||||||||||||||||
| 55 | return sub { | ||||||||||||||||||||||||||||
| 56 | 0 | ▶ | my $ret = defined $_[1] ? $_[1] : 0; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 0 | exit $ret; | |||||||||||||||||||||||||||
| 58 | 0 | ▶ | }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 59 | 28 | }; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | sub new { | ||||||||||||||||||||||||||||
| 62 | 220 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | 220 | my @args = @_; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 220 | my $self; | |||||||||||||||||||||||||||
| 66 | 220 | ▶ | if (@args == 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 1 | $self = $class->SUPER::new( | |||||||||||||||||||||||||||
| 68 | name => $args[0], | ||||||||||||||||||||||||||||
| 69 | description => $args[1], | ||||||||||||||||||||||||||||
| 70 | ); | ||||||||||||||||||||||||||||
| 71 | } else { | ||||||||||||||||||||||||||||
| 72 | 219 | $self = $class->SUPER::new(@args); | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 220 | return $self; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | sub flag { | ||||||||||||||||||||||||||||
| 79 | 132 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
flag: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 80 | 132 | my ($name, $description) = @_; | |||||||||||||||||||||||||||
| 81 | 132 | my $ret = $self->flags->add( | |||||||||||||||||||||||||||
| 82 | name => $name, | ||||||||||||||||||||||||||||
| 83 | description => $description, | ||||||||||||||||||||||||||||
| 84 | ); | ||||||||||||||||||||||||||||
| 85 | 131 | return $ret; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | sub arg { | ||||||||||||||||||||||||||||
| 89 | 100 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
arg: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 90 | 100 | my ($name, $description) = @_; | |||||||||||||||||||||||||||
| 91 | 100 | my $ret = $self->args->add( | |||||||||||||||||||||||||||
| 92 | name => $name, | ||||||||||||||||||||||||||||
| 93 | description => $description, | ||||||||||||||||||||||||||||
| 94 | ); | ||||||||||||||||||||||||||||
| 95 | 100 | return $ret; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | sub command { | ||||||||||||||||||||||||||||
| 99 | 53 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
command: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 100 | 53 | my ($name, $description) = @_; | |||||||||||||||||||||||||||
| 101 | 53 | ▶ | if ($self->commands->count == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 33 | $self->commands->add( | |||||||||||||||||||||||||||
| 103 | name => "help", | ||||||||||||||||||||||||||||
| 104 | description => "Show help.", | ||||||||||||||||||||||||||||
| 105 | ); | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | 53 | my $ret = $self->commands->add( | |||||||||||||||||||||||||||
| 108 | name => $name, | ||||||||||||||||||||||||||||
| 109 | description => $description, | ||||||||||||||||||||||||||||
| 110 | parent => $self, | ||||||||||||||||||||||||||||
| 111 | ); | ||||||||||||||||||||||||||||
| 112 | 53 | return $ret; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | sub parse { | ||||||||||||||||||||||||||||
| 116 | 128 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 117 | 128 | my @argv = @_; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 128 | ▶ | if (scalar @argv == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 127 | @argv = @ARGV; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 128 | my ($ret, $exit_code) = $self->_parse(@argv); | |||||||||||||||||||||||||||
| 124 | 127 | ▶ | if (defined $exit_code) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 64 | return $self->terminate->($ret, $exit_code); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | 63 | return $ret; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | sub _parse { | ||||||||||||||||||||||||||||
| 131 | 152 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_parse: called CC=58 SCAR=40.6
| |||||||||||||||||||||||||||||
| 132 | 152 | my @argv = @_; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 152 | ▶ | if (defined $self->parent) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 24 | $self->flags->unshift($self->parent->flags->values); | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | my $required_but_not_found = { | ||||||||||||||||||||||||||||
| 139 | 152 | map {$_->name => $_} grep {$_->_required} $self->flags->values, | |||||||||||||||||||||||||||
| 140 | }; | ||||||||||||||||||||||||||||
| 141 | 152 | my $arg_index = 0; | |||||||||||||||||||||||||||
| 142 | 152 | my $arg_only = 0; | |||||||||||||||||||||||||||
| 143 | 152 | ▶ | while (scalar @argv > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 190 | my $arg = shift @argv; | |||||||||||||||||||||||||||
| 145 | 190 | ▶ | if ($arg eq "--") { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 146 | 3 | $arg_only = 1; | |||||||||||||||||||||||||||
| 147 | ▶ | } elsif ($arg_only == 0 and $arg =~ /^--(no-)?(\S+?)(=(\S+))?$/) { | |||||||||||||||||||||||||||
|
Condition100%3/3
$arg_only == 0 and $arg =~ /^--(no-)?(\S+?)(=(\S+))?$/
MC/DC100%2/2
$arg_only == 0 and $arg =~ /^--(no-)?(\S+?)(=(\S+))?$/
$arg_only == 0
$arg =~ /^--(no-)?(\S+?)(=(\S+))?$/
Truth table100%3/3
A and B
A= $arg_only == 0B= $arg =~ /^--(no-)?(\S+?)(=(\S+))?$/
| |||||||||||||||||||||||||||||
| 148 | 88 | my $no = $1; | |||||||||||||||||||||||||||
| 149 | 88 | my $name = $2; | |||||||||||||||||||||||||||
| 150 | 88 | my $equal = $3; | |||||||||||||||||||||||||||
| 151 | 88 | my $val = $4; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 88 | ▶ | delete $required_but_not_found->{$name} if exists $required_but_not_found->{$name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 88 | my $v = $self->flags->get($name); | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 88 | ▶ | if (not defined $v) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | 1 | printf STDERR "%s: error: unknown long flag '--%s', try --help\n", $self->name, $name; | |||||||||||||||||||||||||||
| 158 | 1 | return undef, 1; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 87 | my $value; | |||||||||||||||||||||||||||
| 162 | 87 | ▶ | if ($v->type eq "Bool") { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 163 | 47 | ▶ | $value = defined $no ? 0 : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | } elsif (defined $equal) { | ||||||||||||||||||||||||||||
| 165 | 12 | $value = $val; | |||||||||||||||||||||||||||
| 166 | } else { | ||||||||||||||||||||||||||||
| 167 | 28 | $value = shift @argv; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 87 | my ($dummy, $exit) = $v->set_value($value); | |||||||||||||||||||||||||||
| 171 | 86 | ▶ | if (defined $exit) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 3 | return undef, $exit; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | ▶ | } elsif ($arg_only == 0 and $arg =~ /^-(\S+)$/) { | |||||||||||||||||||||||||||
|
Condition100%3/3
$arg_only == 0 and $arg =~ /^-(\S+)$/
MC/DC100%2/2
$arg_only == 0 and $arg =~ /^-(\S+)$/
$arg_only == 0
$arg =~ /^-(\S+)$/
Truth table100%3/3
A and B
A= $arg_only == 0B= $arg =~ /^-(\S+)$/
| |||||||||||||||||||||||||||||
| 175 | 16 | my $short_name = $1; | |||||||||||||||||||||||||||
| 176 | 16 | ▶ | while (length $short_name > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | 18 | my ($s, $remain) = split //, $short_name, 2; | |||||||||||||||||||||||||||
| 178 | 18 | my $name; | |||||||||||||||||||||||||||
| 179 | 18 | foreach my $f ($self->flags->values) { | |||||||||||||||||||||||||||
| 180 | 46 | ▶ | if (defined $f->short_name and $f->short_name eq $s) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $f->short_name and $f->short_name eq $s
MC/DC100%2/2
defined $f->short_name and $f->short_name eq $s
defined $f->short_name
$f->short_name eq $s
Truth table100%3/3
A and B
A= defined $f->short_nameB= $f->short_name eq $s
| |||||||||||||||||||||||||||||
| 181 | 16 | $name = $f->name; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | 18 | ▶ | if (not defined $name) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | 2 | printf STDERR "%s: error: unknown short flag '-%s', try --help\n", $self->name, $s; | |||||||||||||||||||||||||||
| 186 | 2 | return undef, 1; | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | 16 | ▶ | delete $required_but_not_found->{$name} if exists $required_but_not_found->{$name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | 16 | my $v = $self->flags->get($name); | |||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 16 | my $value; | |||||||||||||||||||||||||||
| 192 | 16 | ▶ | if ($v->type eq "Bool") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | 9 | $value = 1; | |||||||||||||||||||||||||||
| 194 | } else { | ||||||||||||||||||||||||||||
| 195 | 7 | ▶ | if (length $remain > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | 3 | $value = $remain; | |||||||||||||||||||||||||||
| 197 | 3 | $remain = ""; | |||||||||||||||||||||||||||
| 198 | } else { | ||||||||||||||||||||||||||||
| 199 | 4 | $value = shift @argv; | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 16 | my ($dummy, $exit) = $v->set_value($value); | |||||||||||||||||||||||||||
| 204 | 16 | ▶ | if (defined $exit) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 205 | 1 | return undef, $exit; | |||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | 15 | $short_name = $remain; | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | } else { | ||||||||||||||||||||||||||||
| 210 | 83 | ▶ | if ($arg_index == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | 67 | my $cmd = $self->commands->get($arg); | |||||||||||||||||||||||||||
| 212 | 67 | ▶ | if (defined $cmd) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 213 | 31 | ▶ | if ($cmd->name eq "help") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 214 | 7 | $self->flags->get("help")->set_value(1) | |||||||||||||||||||||||||||
| 215 | } else { | ||||||||||||||||||||||||||||
| 216 | 24 | my @argv_for_command = @argv; | |||||||||||||||||||||||||||
| 217 | 24 | @argv = (); | |||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 24 | ▶ | if ($self->flags->get("help")) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 220 | 13 | push @argv_for_command, "--help"; | |||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||
| 222 | 24 | return $cmd->_parse(@argv_for_command); | |||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | 59 | ▶ | if (not ($arg_index == 0 and $arg eq "help")) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$arg_index == 0 and $arg eq "help"
MC/DC100%2/2
$arg_index == 0 and $arg eq "help"
$arg_index == 0
$arg eq "help"
Truth table100%3/3
A and B
A= $arg_index == 0B= $arg eq "help"
| |||||||||||||||||||||||||||||
| 228 | 52 | ▶ | if ($arg_index < $self->args->count) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | 51 | my ($dummy, $exit) = $self->args->get_by_index($arg_index)->set_value($arg); | |||||||||||||||||||||||||||
| 230 | 51 | ▶ | if (defined $exit) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 231 | 5 | return undef, $exit; | |||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||
| 233 | 46 | ▶ | if (not $self->args->get_by_index($arg_index)->is_cumulative) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 234 | 34 | $arg_index++; | |||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | } else { | ||||||||||||||||||||||||||||
| 237 | 1 | printf STDERR "%s: error: unexpected %s, try --help\n", $self->name, $arg; | |||||||||||||||||||||||||||
| 238 | 1 | return undef, 1; | |||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | 114 | ▶ | if ($self->flags->get("help")) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 245 | 40 | $self->help; | |||||||||||||||||||||||||||
| 246 | 40 | return undef, 0; | |||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | 74 | ▶ | if ($self->flags->get("version")) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 250 | 1 | printf STDERR "%s\n", $self->_version; | |||||||||||||||||||||||||||
| 251 | 1 | return undef, 0; | |||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | 73 | foreach my $f ($self->flags->values) { | |||||||||||||||||||||||||||
| 255 | 135 | ▶ | if (defined $f->value) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 256 | 45 | next; | |||||||||||||||||||||||||||
| 257 | } elsif (defined $f->_envar) { | ||||||||||||||||||||||||||||
| 258 | 2 | my ($dummy, $exit) = $f->set_value($f->_envar); | |||||||||||||||||||||||||||
| 259 | 2 | ▶ | if (defined $exit) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 260 | 1 | return undef, $exit; | |||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||
| 262 | } elsif (defined $f->_default) { | ||||||||||||||||||||||||||||
| 263 | 84 | ▶ | if ($f->type =~ /List$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 264 | 2 | foreach my $default (@{$f->_default}) { | |||||||||||||||||||||||||||
| 265 | 3 | my ($dummy, $exit) = $f->set_value($default); | |||||||||||||||||||||||||||
| 266 | 3 | ▶ | if (defined $exit) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 267 | 1 | return undef, $exit; | |||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||
| 270 | } else { | ||||||||||||||||||||||||||||
| 271 | 82 | my ($dummy, $exit) = $f->set_value($f->_default); | |||||||||||||||||||||||||||
| 272 | 82 | ▶ | if (defined $exit) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 273 | 1 | return undef, $exit; | |||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||
| 276 | } elsif ($f->type =~ /List$/) { | ||||||||||||||||||||||||||||
| 277 | 2 | $f->value([]); | |||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||
| 280 | 70 | ▶ | for (my $i = 0; $i < $self->args->count; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 281 | 51 | my $arg = $self->args->get_by_index($i); | |||||||||||||||||||||||||||
| 282 | 51 | ▶ | if (defined $arg->value) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 283 | 36 | next; | |||||||||||||||||||||||||||
| 284 | } elsif (defined $arg->_envar) { | ||||||||||||||||||||||||||||
| 285 | 3 | my ($dummy, $exit) = $arg->set_value($arg->_envar); | |||||||||||||||||||||||||||
| 286 | 3 | ▶ | if (defined $exit) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 287 | 1 | return undef, $exit; | |||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||
| 289 | } elsif (defined $arg->_default) { | ||||||||||||||||||||||||||||
| 290 | 6 | ▶ | if ($arg->type =~ /List$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 291 | 2 | foreach my $default (@{$arg->_default}) { | |||||||||||||||||||||||||||
| 292 | 3 | my ($dummy, $exit) = $arg->set_value($default); | |||||||||||||||||||||||||||
| 293 | 3 | ▶ | if (defined $exit) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 294 | 1 | return undef, $exit; | |||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||
| 297 | } else { | ||||||||||||||||||||||||||||
| 298 | 4 | my ($dummy, $exit) = $arg->set_value($arg->_default); | |||||||||||||||||||||||||||
| 299 | 4 | ▶ | if (defined $exit) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 300 | 1 | return undef, $exit; | |||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||
| 303 | } elsif ($arg->type =~ /List$/) { | ||||||||||||||||||||||||||||
| 304 | 2 | $arg->value([]); | |||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | 67 | foreach my $r (values %$required_but_not_found) { | |||||||||||||||||||||||||||
| 309 | 2 | printf STDERR "%s: error: required flag --%s not provided, try --help\n", $self->name, $r->name; | |||||||||||||||||||||||||||
| 310 | 2 | return undef, 1; | |||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||
| 312 | 65 | ▶ | for (my $i = 0; $i < $self->args->count; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 313 | 48 | my $arg = $self->args->get_by_index($i); | |||||||||||||||||||||||||||
| 314 | 48 | ▶ | if ($arg->_required and not $arg->_defined) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$arg->_required and not $arg->_defined
MC/DC100%2/2
$arg->_required and not $arg->_defined
$arg->_required
not $arg->_defined
Truth table100%3/3
A and not(B)
A= $arg->_requiredB= not $arg->_defined
| |||||||||||||||||||||||||||||
| 315 | 2 | printf STDERR "%s: error: required arg '%s' not provided, try --help\n", $self->name, $arg->name; | |||||||||||||||||||||||||||
| 316 | 2 | return undef, 1; | |||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | 63 | return $self; | |||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | sub version { | ||||||||||||||||||||||||||||
| 324 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
version: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 325 | 2 | my ($version) = @_; | |||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | 2 | my $f = $self->flags->add( | |||||||||||||||||||||||||||
| 328 | name => 'version', | ||||||||||||||||||||||||||||
| 329 | description => 'Show application version.', | ||||||||||||||||||||||||||||
| 330 | )->bool(); | ||||||||||||||||||||||||||||
| 331 | 2 | $self->_version($version); | |||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | sub help_short { | ||||||||||||||||||||||||||||
| 335 | 23 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
help_short: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 336 | 23 | my @help = ($self->name); | |||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | 23 | push @help, "[<flags>]"; | |||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | 23 | ▶ | if ($self->commands->count > 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 341 | 6 | push @help, "<command>"; | |||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | 6 | my $has_args = 0; | |||||||||||||||||||||||||||
| 344 | 6 | foreach my $cmd ($self->commands->get_all) { | |||||||||||||||||||||||||||
| 345 | 17 | ▶ | if ($cmd->args->count > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 346 | 2 | $has_args = 1; | |||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | 6 | push @help, "[<args> ...]"; | |||||||||||||||||||||||||||
| 351 | } else { | ||||||||||||||||||||||||||||
| 352 | 17 | foreach my $arg ($self->args->get_all) { | |||||||||||||||||||||||||||
| 353 | 8 | push @help, sprintf "<%s>", $arg->name; | |||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | 23 | return join " ", @help; | |||||||||||||||||||||||||||
| 358 | } | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | sub help { | ||||||||||||||||||||||||||||
| 361 | 23 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
help: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 362 | 23 | printf "usage: %s\n", $self->help_short; | |||||||||||||||||||||||||||
| 363 | 23 | printf "\n"; | |||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | 23 | ▶ | if ($self->description ne "") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 366 | 1 | printf "%s\n", $self->description; | |||||||||||||||||||||||||||
| 367 | 1 | printf "\n"; | |||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | 23 | printf "%s\n", $self->flags->help; | |||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | 23 | ▶ | if ($self->commands->count > 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 373 | 6 | printf "%s\n", $self->commands->help; | |||||||||||||||||||||||||||
| 374 | } else { | ||||||||||||||||||||||||||||
| 375 | 17 | ▶ | if ($self->args->count > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 376 | 5 | printf "%s\n", $self->args->help; | |||||||||||||||||||||||||||
| 377 | } | ||||||||||||||||||||||||||||
| 378 | } | ||||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | 28 | 1; | |||||||||||||||||||||||||||
| 383 | __END__ | ||||||||||||||||||||||||||||