line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MooX::Commander; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
4078
|
use Moo; |
|
1
|
|
|
|
|
10025
|
|
|
1
|
|
|
|
|
4
|
|
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
1426
|
use Class::Load qw/load_class/; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
use String::CamelSnakeKebab qw/upper_camel_case/; |
7
|
|
|
|
|
|
|
use Syntax::Keyword::Junction qw/any/; |
8
|
|
|
|
|
|
|
use Path::Tiny; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = "0.01"; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
has base_class => (is => 'rw'); |
13
|
|
|
|
|
|
|
has class_prefix => (is => 'rw', default => sub { "Cmd" }); |
14
|
|
|
|
|
|
|
has version => (is => 'lazy'); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub _build_version { |
17
|
|
|
|
|
|
|
my $self = shift; |
18
|
|
|
|
|
|
|
load_class($self->base_class); |
19
|
|
|
|
|
|
|
my $program = path($0); |
20
|
|
|
|
|
|
|
my $version = $self->base_class . "::VERSION"; |
21
|
|
|
|
|
|
|
no strict 'refs'; |
22
|
|
|
|
|
|
|
print $program->basename . " $${version}\n"; |
23
|
|
|
|
|
|
|
exit; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub dispatch { |
27
|
|
|
|
|
|
|
my ($self, %params) = @_; |
28
|
|
|
|
|
|
|
my $argv = [@{ $params{argv} }]; # make a copy of the array |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
(print($self->version->(), "\n") && exit 1) |
31
|
|
|
|
|
|
|
if $argv->[0] |
32
|
|
|
|
|
|
|
&& $argv->[0] eq any(qw/-v --version/); |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
unshift @$argv, 'help' if $argv->[0] && $argv->[0] eq any(qw/-h --help/); |
35
|
|
|
|
|
|
|
unshift @$argv, 'help' unless $argv->[0]; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
my @args; |
38
|
|
|
|
|
|
|
while (my $arg = shift @$argv) { |
39
|
|
|
|
|
|
|
last if $arg =~ /^--?/; |
40
|
|
|
|
|
|
|
push @args, $arg; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
my $action = upper_camel_case shift @args; |
44
|
|
|
|
|
|
|
my $class = $self->base_class . "::" . $self->class_prefix . "::" . $action; |
45
|
|
|
|
|
|
|
eval { load_class($class) }; |
46
|
|
|
|
|
|
|
if ($@) { |
47
|
|
|
|
|
|
|
eval { |
48
|
|
|
|
|
|
|
my $start = $self->base_class . "::" . $self->class_prefix; |
49
|
|
|
|
|
|
|
my $class = "${start}::Help"; |
50
|
|
|
|
|
|
|
eval { load_class($class) }; |
51
|
|
|
|
|
|
|
if ($@) { |
52
|
|
|
|
|
|
|
print "subcommand not found\n"; |
53
|
|
|
|
|
|
|
exit 1; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
$class->new(argv => $params{argv})->usage; |
57
|
|
|
|
|
|
|
}; |
58
|
|
|
|
|
|
|
}; |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
$class->new(argv => $params{argv})->go(@args); |
61
|
|
|
|
|
|
|
die $@ if $@; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
1; |
65
|
|
|
|
|
|
|
__END__ |