blib/lib/MOP4Import/Base/CLI.pm untested

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