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