| 1 | #!/usr/bin/env perl | ||||||||||||||||||||
| 2 | package MOP4Import::Base::CLI; | ||||||||||||||||||||
| 3 | 19 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 19 | ▶ | use warnings qw(FATAL all NONFATAL misc); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 19 | ▶ | use mro qw/c3/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 19 | ▶ | use File::Basename (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 19 | ▶ | use Data::Dumper (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 19 | ▶ | use attributes (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 19 | ▶ | use constant DEBUG => $ENV{DEBUG_MOP4IMPORT}; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 19 | ▶ | use Module::Runtime (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 19 | ▶ | use MOP4Import::Base::Configure -as_base, qw/FieldSpec/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | #======================================== | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 19 | *run = *cli_run; *run = *cli_run; | |||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | sub cli_run :method { | ||||||||||||||||||||
| 23 | 28 | ▶ | my ($class, $arglist, $opt_alias) = @_; | ||||||||||||||||||
|
Subroutine
cli_run: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | { | ||||||||||||||||||||
| 26 | 28 | my $modFn = Module::Runtime::module_notional_filename($class); | |||||||||||||||||||
| 27 | 28 | ▶ | $INC{$modFn} //= 1; | ||||||||||||||||||
|
Condition100%2/2
$INC{$modFn} //= 1
MC/DC100%1/1
$INC{$modFn} //= 1
$INC{$modFn}
Truth table100%2/2
A
A= $INC{$modFn}
| |||||||||||||||||||||
| 28 | } | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 28 | my MY $self = $class->new($class->cli_parse_opts($arglist, undef, $opt_alias)); | |||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 26 | ▶ | unless (@$arglist) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 33 | # Invoke help command if no arguments are given. | ||||||||||||||||||||
| 34 | 2 | $self->cmd_help; | |||||||||||||||||||
| 35 | 0 | return; | |||||||||||||||||||
| 36 | } | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 24 | my $cmd = shift @$arglist; | |||||||||||||||||||
| 39 | 24 | ▶ | if (my $sub = $self->can("cmd_$cmd")) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 40 | # Invoke official command. | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 3 | $self->cli_precmd($cmd); | |||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | 3 | $sub->($self, @$arglist); | |||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | } elsif ($self->can($cmd)) { | ||||||||||||||||||||
| 47 | # Invoke unofficial internal methods. Development aid. | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | 20 | $self->cli_invoke($cmd, @$arglist); | |||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | } else { | ||||||||||||||||||||
| 52 | # Last resort. You can implement your own subcommand interpretations here. | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 1 | $self->cli_unknown_subcommand($cmd, $arglist); | |||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | #======================================== | ||||||||||||||||||||
| 59 | # Hooks and default implementations | ||||||||||||||||||||
| 60 | #======================================== | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | # | ||||||||||||||||||||
| 63 | # Each class can override parse_opts method. | ||||||||||||||||||||
| 64 | # | ||||||||||||||||||||
| 65 | 19 | *parse_opts = *cli_parse_opts; *parse_opts = *cli_parse_opts; | |||||||||||||||||||
| 66 | sub cli_parse_opts :method { | ||||||||||||||||||||
| 67 | 108 | ▶ | my ($pack, $list, $result, $opt_alias) = @_; | ||||||||||||||||||
|
Subroutine
cli_parse_opts: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 108 | MOP4Import::Util::parse_opts($pack, $list, $result, $opt_alias); | |||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | ▶ | sub cli_precmd :method {} # hook called just before cmd_zzz | |||||||||||||||||||
|
Subroutine
cli_precmd: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | sub cli_invoke { | ||||||||||||||||||||
| 75 | 1 | ▶ | (my MY $self, my ($method, @args)) = @_; | ||||||||||||||||||
|
Subroutine
cli_invoke: called CC=6 SCAR=24.3
Pod
undocumented
| |||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 1 | $self->cli_precmd($method); | |||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | 1 | my @res = $self->$method(@args); | |||||||||||||||||||
| 80 | 1 | ▶ | $self->cli_output(\@res) if @res; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 1 | ▶ | if ($method =~ /^has_/) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 83 | # If method name starts with 'has_' and result is empty, | ||||||||||||||||||||
| 84 | # exit with 1. | ||||||||||||||||||||
| 85 | 0 | ▶ | exit(@res ? 0 : 1); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | } elsif ($method =~ /^is_/) { | ||||||||||||||||||||
| 88 | # If method name starts with 'is_' and first result is false, | ||||||||||||||||||||
| 89 | # exit with 1. | ||||||||||||||||||||
| 90 | 0 | ▶ | exit($res[0] ? 0 : 1); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 91 | } | ||||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | sub cli_output :method { | ||||||||||||||||||||
| 95 | 1 | ▶ | (my MY $self, my $res) = @_; | ||||||||||||||||||
|
Subroutine
cli_output: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 96 | 1 | print join("\n", map {MOP4Import::Util::terse_dump($_)} @$res), "\n"; | |||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | sub cli_unknown_subcommand :method { | ||||||||||||||||||||
| 100 | 1 | ▶ | (my MY $self, my ($cmd, $arglist)) = @_; | ||||||||||||||||||
|
Subroutine
cli_unknown_subcommand: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 1 | $self->cmd_help("Error: No such subcommand '$cmd'\n"); | |||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | #======================================== | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | sub onconfigure_help :method { | ||||||||||||||||||||
| 108 | 2 | ▶ | (my MY $self, my $val) = @_; | ||||||||||||||||||
|
Subroutine
onconfigure_help: called CC=1 SCAR=0.4
Pod
undocumented
| |||||||||||||||||||||
| 109 | 2 | $self->cmd_help; | |||||||||||||||||||
| 110 | 0 | exit; | |||||||||||||||||||
| 111 | } | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | sub cli_inspector { | ||||||||||||||||||||
| 114 | 7 | ▶ | require MOP4Import::Util::Inspector; | ||||||||||||||||||
|
Subroutine
cli_inspector: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 115 | 7 | 'MOP4Import::Util::Inspector'; | |||||||||||||||||||
| 116 | } | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | sub cmd_help :method { | ||||||||||||||||||||
| 119 | 6 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
cmd_help: called CC=4 SCAR=14.3
Pod
undocumented
| |||||||||||||||||||||
| 120 | 6 | ▶ | my $pack = ref $self || $self; | ||||||||||||||||||
|
Condition33%1/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table33%1/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | # Invoke precmd (mainly for binmode handling) | ||||||||||||||||||||
| 123 | 6 | $self->cli_precmd(); | |||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 6 | my @msg = (join("\n", @_, <<END)); | |||||||||||||||||||
| 126 | 6 | Usage: @{[File::Basename::basename($0)]} [--opt=value].. <Command> ARGS... | |||||||||||||||||||
| 127 | END | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | 6 | my $insp = $self->cli_inspector; | |||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | 6 | ▶ | if (my @cmds = $insp->describe_commands_of($self)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 132 | 6 | push @msg, "\nCommands\n", @cmds; | |||||||||||||||||||
| 133 | } | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | 6 | ▶ | if (my @opts = $insp->describe_options_of($self)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 136 | 6 | push @msg, "\n", @opts; | |||||||||||||||||||
| 137 | } | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | 6 | die join("", @msg); | |||||||||||||||||||
| 140 | } | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | 19 | ▶ | MY->cli_run(\@ARGV) unless caller; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | 19 | 1; | |||||||||||||||||||