| 1 | package YATT::Lite::Util::CmdLine; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 75 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 75 | ▶ | use warnings qw(FATAL all NONFATAL misc); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 75 | ▶ | BEGIN {require Exporter; *import = \&Exporter::import} | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 75 | our @EXPORT = qw(parse_opts parse_params); | |||||||||||||||||||||||||||||||||||||||||||||
| 8 | 75 | our @EXPORT_OK = (@EXPORT, qw(run process_result)); | |||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | # posix style option. | ||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 38 | ▶ | sub parse_opts { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_opts: called CC=12 SCAR=24.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 38 | my ($pack, $list, $result, $alias) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 13 | 38 | my $wantarray = wantarray; | |||||||||||||||||||||||||||||||||||||||||||||
| 14 | 38 | ▶ | unless (defined $result) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 37 | ▶ | $result = $wantarray ? [] : {}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 38 | ▶ | while (@$list and my ($n, $v) = $list->[0] | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@$list and () = $list->[0] =~ /^--$ | ^(?:--? ([\w:\-\.]+) (?: =(.*))?)$/sx
MC/DC100%2/2
@$list and () = $list->[0] =~ /^--$ | ^(?:--? ([\w:\-\.]+) (?: =(.*))?)$/sx
@$list
() = $list->[0] =~ /^--$ | ^(?:--? ([\w:\-\.]+) (?: =(.*))?)$/sx
Truth table100%3/3
A and B
A= @$listB= () = $list->[0] =~ /^--$ | ^(?:--? ([\w:\-\.]+) (?: =(.*))?)$/sx
| |||||||||||||||||||||||||||||||||||||||||||||||
| 18 | =~ m{^--$ | ^(?:--? ([\w:\-\.]+) (?: =(.*))?)$}xs) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 21 | shift @$list; | |||||||||||||||||||||||||||||||||||||||||||||
| 20 | 21 | ▶ | last unless defined $n; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 20 | ▶ | $n = $alias->{$n} if $alias and $alias->{$n}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$alias and $alias->{$n}
MC/DC100%2/2
$alias and $alias->{$n}
$alias
$alias->{$n}
Truth table100%3/3
A and B
A= $aliasB= $alias->{$n}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 20 | ▶ | $v = 1 unless defined $v; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 20 | ▶ | if (ref $result eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 3 | $result->{$n} = $v; | |||||||||||||||||||||||||||||||||||||||||||||
| 25 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 17 | push @$result, $n, $v; | |||||||||||||||||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 38 | ▶ | $wantarray && ref $result ne 'HASH' ? @$result : $result; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$wantarray and ref $result ne "HASH"
MC/DC50%1/2
$wantarray and ref $result ne "HASH"
$wantarray
ref $result ne "HASH"
Truth table66%2/3
A and B
A= $wantarrayB= ref $result ne "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||
| 32 | # 'Make' style parameter. | ||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 22 | ▶ | sub parse_params { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_params: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 22 | my ($pack, $list, $hash) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 35 | 22 | my $explicit; | |||||||||||||||||||||||||||||||||||||||||||||
| 36 | 22 | ▶ | unless (defined $hash) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 3 | $hash = {} | |||||||||||||||||||||||||||||||||||||||||||||
| 38 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 19 | $explicit++; | |||||||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 22 | ▶ | for (; @$list and $list->[0] =~ /^([^=]+)=(.*)/s; shift @$list) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@$list and $list->[0] =~ /^([^=]+)=(.*)/s
MC/DC100%2/2
@$list and $list->[0] =~ /^([^=]+)=(.*)/s
@$list
$list->[0] =~ /^([^=]+)=(.*)/s
Truth table100%3/3
A and B
A= @$listB= $list->[0] =~ /^([^=]+)=(.*)/s
| |||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 13 | $hash->{$1} = $2; | |||||||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 22 | ▶ | if (not $explicit and wantarray) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $explicit and wantarray
MC/DC50%1/2
not $explicit and wantarray
not $explicit
wantarray
Truth table66%2/3
not(A) and B
A= not $explicitB= wantarray
| |||||||||||||||||||||||||||||||||||||||||||||||
| 45 | # return empty list if hash is empty | ||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 3 | ▶ | %$hash ? $hash : (); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 47 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 19 | $hash | |||||||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 3 | ▶ | sub run { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run: called CC=6 SCAR=19.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 3 | my ($pack, $list, $alias) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 3 | my @opts = parse_opts($pack, $list, $alias); | |||||||||||||||||||||||||||||||||||||||||||||
| 56 | 3 | my $app = do { | |||||||||||||||||||||||||||||||||||||||||||||
| 57 | 3 | ▶ | if (ref $pack) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 1 | $pack->configure(@opts); | |||||||||||||||||||||||||||||||||||||||||||||
| 59 | 1 | $pack; | |||||||||||||||||||||||||||||||||||||||||||||
| 60 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 2 | $pack->new(@opts); | |||||||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 63 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 3 | ▶ | my $cmd = shift @$list || 'help'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift @$list || 'help'
MC/DC0%0/1
shift @$list || 'help'
shift @$list
Truth table50%1/2
A
A= shift @$list
| |||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 3 | $app->configure(parse_opts($pack, \@_, $alias)); | |||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 3 | ▶ | if (my $sub = $app->can("cmd_$cmd")) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 3 | $sub->($app, @$list); | |||||||||||||||||||||||||||||||||||||||||||||
| 69 | } elsif ($sub = $app->can($cmd)) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | process_result($sub->($app, @$list)); | |||||||||||||||||||||||||||||||||||||||||||||
| 71 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | die "$0: Unknown subcommand '$cmd'\n" | |||||||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 3 | ▶ | if (my $sub = $app->can('DESTROY')) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | $sub->($app); | |||||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | sub process_result { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
process_result: not called CC=13 SCAR=52.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | my (@res) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | if (not @res | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/6
not @res or @res == 1 and not $res[0]
@res == 1 and not $res[0]
MC/DC0%0/3
not @res or @res == 1 and not $res[0]
not @res
@res == 1
not $res[0]
Truth table0%0/4
not(A) or B and not(C)
A= not @resB= @res == 1C= not $res[0]
| |||||||||||||||||||||||||||||||||||||||||||||||
| 82 | or @res == 1 and not $res[0]) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | exit 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 84 | ▶ | } elsif (@res == 1 and defined $res[0] and $res[0] eq 1) { | |||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/6
@res == 1 and defined $res[0] and $res[0] eq 1
@res == 1 and defined $res[0]
MC/DC0%0/3
@res == 1 and defined $res[0] and $res[0] eq 1
@res == 1
defined $res[0]
$res[0] eq 1
Truth table0%0/4
A and B and C
A= @res == 1B= defined $res[0]C= $res[0] eq 1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 85 | # nop | ||||||||||||||||||||||||||||||||||||||||||||||
| 86 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | require YATT::Lite::Util; | |||||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | foreach my $res (@res) { | |||||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | unless (defined $res) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | print "(undef)\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 91 | } elsif (not ref $res) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | print $res, "\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 93 | } elsif (my $sub = $res->can('get_columns')) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | my @kv = $sub->($res); | |||||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | my $cnt; | |||||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | ▶ | while (my ($k, $v) = splice @kv, 0, 2) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | ▶ | print "\t" if $cnt++; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | print "$k=", YATT::Lite::Util::terse_dump($v); | |||||||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | print "\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 101 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | print YATT::Lite::Util::terse_dump($res), "\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 75 | 1; | |||||||||||||||||||||||||||||||||||||||||||||