line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package YA::CLI; |
2
|
|
|
|
|
|
|
our $VERSION = '0.004'; |
3
|
3
|
|
|
3
|
|
283707
|
use Moo; |
|
3
|
|
|
|
|
11418
|
|
|
3
|
|
|
|
|
17
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
# ABSTRACT: Yet another CLI framework |
6
|
|
|
|
|
|
|
|
7
|
3
|
|
|
3
|
|
2157
|
use Carp qw(croak); |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
149
|
|
8
|
3
|
|
|
3
|
|
2430
|
use Getopt::Long; |
|
3
|
|
|
|
|
35135
|
|
|
3
|
|
|
|
|
11
|
|
9
|
3
|
|
|
3
|
|
459
|
use List::Util qw(first); |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
333
|
|
10
|
3
|
|
|
3
|
|
1429
|
use Module::Pluggable::Object; |
|
3
|
|
|
|
|
19010
|
|
|
3
|
|
|
|
|
1770
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub BUILD_ARGS { |
14
|
0
|
|
|
0
|
0
|
0
|
croak "Please use run()"; |
15
|
|
|
|
|
|
|
} |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub default_handler { |
18
|
5
|
|
|
5
|
1
|
20
|
return 'main'; |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub default_search_path { |
22
|
15
|
|
|
15
|
1
|
74
|
return shift; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub cli_options { |
26
|
15
|
|
|
15
|
1
|
24
|
return; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub _init { |
30
|
15
|
|
|
15
|
|
31
|
my ($class, $args) = @_; |
31
|
|
|
|
|
|
|
|
32
|
15
|
|
50
|
|
|
39
|
$args //= \@ARGV; |
33
|
|
|
|
|
|
|
|
34
|
15
|
|
|
|
|
40
|
my %cli_args = $class->_get_opts($args); |
35
|
|
|
|
|
|
|
|
36
|
15
|
|
|
|
|
31
|
my ($action, $subaction); |
37
|
15
|
100
|
100
|
|
|
103
|
if (@$args && $args->[0] !~ /^--/) { |
38
|
10
|
|
|
|
|
20
|
$action = shift @$args; |
39
|
|
|
|
|
|
|
} |
40
|
15
|
100
|
100
|
|
|
79
|
if ($action && @$args && $args->[0] !~ /^--/) { |
|
|
|
100
|
|
|
|
|
41
|
4
|
|
|
|
|
7
|
$subaction = shift @$args; |
42
|
|
|
|
|
|
|
} |
43
|
15
|
|
66
|
|
|
46
|
$action //= $class->default_handler; |
44
|
|
|
|
|
|
|
|
45
|
15
|
|
|
|
|
43
|
my $handler = $class->_get_action_handler($action, $subaction); |
46
|
|
|
|
|
|
|
|
47
|
15
|
100
|
|
|
|
53
|
if (!$handler) { |
48
|
3
|
|
|
|
|
1011
|
require YA::CLI::ErrorHandler; |
49
|
3
|
|
|
|
|
53
|
$handler = 'YA::CLI::ErrorHandler'; |
50
|
3
|
100
|
|
|
|
11
|
if (!defined $subaction) { |
51
|
2
|
|
|
|
|
10
|
return $handler->as_help(1, "$action command does not exist!"); |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
else { |
54
|
1
|
|
|
|
|
11
|
return $handler->as_help(1, "$action $subaction command does not exist!"); |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
12
|
100
|
|
|
|
32
|
return $handler->as_manpage() if $cli_args{man}; |
59
|
10
|
100
|
|
|
|
31
|
return $handler->as_help() if $cli_args{help}; |
60
|
|
|
|
|
|
|
|
61
|
7
|
|
|
|
|
23
|
return $handler->new_from_args($args); |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub run { |
65
|
15
|
|
|
15
|
1
|
52495
|
my ($class, $args) = @_; |
66
|
15
|
|
|
|
|
43
|
my $handler = $class->_init($args); |
67
|
15
|
|
|
|
|
1919
|
return $handler->run(); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub _get_opts { |
71
|
15
|
|
|
15
|
|
25
|
my ($class, $args) = @_; |
72
|
|
|
|
|
|
|
|
73
|
15
|
|
|
|
|
34
|
my @cli_options = qw( |
74
|
|
|
|
|
|
|
help|h |
75
|
|
|
|
|
|
|
man|m |
76
|
|
|
|
|
|
|
); |
77
|
|
|
|
|
|
|
|
78
|
15
|
|
|
|
|
40
|
push(@cli_options, $class->cli_options); |
79
|
|
|
|
|
|
|
|
80
|
15
|
|
|
|
|
71
|
my $p = Getopt::Long::Parser->new( |
81
|
|
|
|
|
|
|
config => [ |
82
|
|
|
|
|
|
|
qw( |
83
|
|
|
|
|
|
|
pass_through |
84
|
|
|
|
|
|
|
no_auto_abbrev |
85
|
|
|
|
|
|
|
) |
86
|
|
|
|
|
|
|
] |
87
|
|
|
|
|
|
|
); |
88
|
|
|
|
|
|
|
|
89
|
15
|
|
|
|
|
1794
|
my %cli_args; |
90
|
15
|
|
|
|
|
48
|
$p->getoptionsfromarray($args, \%cli_args, @cli_options); |
91
|
15
|
|
|
|
|
4703
|
return %cli_args; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
my @PLUGINS; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub _get_action_handler { |
97
|
15
|
|
|
15
|
|
24
|
my $class = shift; |
98
|
15
|
|
|
|
|
26
|
my $action = shift; |
99
|
15
|
|
|
|
|
23
|
my $subaction = shift; |
100
|
|
|
|
|
|
|
|
101
|
15
|
|
|
|
|
38
|
my $finder = Module::Pluggable::Object->new( |
102
|
|
|
|
|
|
|
search_path => $class->default_search_path, |
103
|
|
|
|
|
|
|
require => 1, |
104
|
|
|
|
|
|
|
); |
105
|
|
|
|
|
|
|
|
106
|
15
|
100
|
|
|
|
154
|
@PLUGINS = $finder->plugins if !@PLUGINS; |
107
|
15
|
|
|
30
|
|
21048
|
return first { $_->has_action($action, $subaction) } @PLUGINS; |
|
30
|
|
|
|
|
113
|
|
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
__END__ |