line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package YA::CLI; |
2
|
|
|
|
|
|
|
our $VERSION = '0.002'; |
3
|
2
|
|
|
2
|
|
141304
|
use Moo; |
|
2
|
|
|
|
|
9779
|
|
|
2
|
|
|
|
|
9
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
# ABSTRACT: Yet another CLI framework |
6
|
|
|
|
|
|
|
|
7
|
2
|
|
|
2
|
|
1541
|
use Carp qw(croak); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
72
|
|
8
|
2
|
|
|
2
|
|
1165
|
use Getopt::Long; |
|
2
|
|
|
|
|
18995
|
|
|
2
|
|
|
|
|
8
|
|
9
|
2
|
|
|
2
|
|
252
|
use List::Util qw(first); |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
170
|
|
10
|
2
|
|
|
2
|
|
764
|
use Module::Pluggable::Object; |
|
2
|
|
|
|
|
10291
|
|
|
2
|
|
|
|
|
813
|
|
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
|
13
|
return 'main'; |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub default_search_path { |
22
|
10
|
|
|
10
|
1
|
36
|
return shift; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub cli_options { |
26
|
10
|
|
|
10
|
1
|
12
|
return; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub _init { |
30
|
10
|
|
|
10
|
|
17
|
my ($class, $args) = @_; |
31
|
|
|
|
|
|
|
|
32
|
10
|
|
50
|
|
|
20
|
$args //= \@ARGV; |
33
|
|
|
|
|
|
|
|
34
|
10
|
|
|
|
|
21
|
my %cli_args = $class->_get_opts($args); |
35
|
|
|
|
|
|
|
|
36
|
10
|
|
|
|
|
15
|
my $action; |
37
|
10
|
100
|
100
|
|
|
37
|
if (@$args && $args->[0] !~ /^--/) { |
38
|
5
|
|
|
|
|
9
|
$action = shift @$args; |
39
|
|
|
|
|
|
|
} |
40
|
10
|
|
66
|
|
|
30
|
$action //= $class->default_handler; |
41
|
|
|
|
|
|
|
|
42
|
10
|
|
|
|
|
21
|
my $handler = $class->_get_action_handler($action); |
43
|
|
|
|
|
|
|
|
44
|
10
|
100
|
|
|
|
27
|
if (!$handler) { |
45
|
2
|
|
|
|
|
375
|
require YA::CLI::ErrorHandler; |
46
|
2
|
|
|
|
|
18
|
$handler = 'YA::CLI::ErrorHandler'; |
47
|
2
|
|
|
|
|
6
|
return $handler->as_help(1, "$action command does not exist!"); |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
8
|
100
|
|
|
|
20
|
return $handler->as_manpage() if $cli_args{man}; |
51
|
6
|
100
|
|
|
|
12
|
return $handler->as_help() if $cli_args{help}; |
52
|
|
|
|
|
|
|
|
53
|
4
|
|
|
|
|
9
|
return $handler->new_from_args($args); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub run { |
57
|
10
|
|
|
10
|
1
|
25469
|
my ($class, $args) = @_; |
58
|
10
|
|
|
|
|
25
|
my $handler = $class->_init($args); |
59
|
10
|
|
|
|
|
1057
|
return $handler->run(); |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub _get_opts { |
63
|
10
|
|
|
10
|
|
13
|
my ($class, $args) = @_; |
64
|
|
|
|
|
|
|
|
65
|
10
|
|
|
|
|
14
|
my @cli_options = qw( |
66
|
|
|
|
|
|
|
help|h |
67
|
|
|
|
|
|
|
man|m |
68
|
|
|
|
|
|
|
); |
69
|
|
|
|
|
|
|
|
70
|
10
|
|
|
|
|
19
|
push(@cli_options, $class->cli_options); |
71
|
|
|
|
|
|
|
|
72
|
10
|
|
|
|
|
34
|
my $p = Getopt::Long::Parser->new( |
73
|
|
|
|
|
|
|
config => [ |
74
|
|
|
|
|
|
|
qw( |
75
|
|
|
|
|
|
|
pass_through |
76
|
|
|
|
|
|
|
no_auto_abbrev |
77
|
|
|
|
|
|
|
) |
78
|
|
|
|
|
|
|
] |
79
|
|
|
|
|
|
|
); |
80
|
|
|
|
|
|
|
|
81
|
10
|
|
|
|
|
945
|
my %cli_args; |
82
|
10
|
|
|
|
|
26
|
$p->getoptionsfromarray($args, \%cli_args, @cli_options); |
83
|
10
|
|
|
|
|
2196
|
return %cli_args; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
my @PLUGINS; |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub _get_action_handler { |
89
|
10
|
|
|
10
|
|
16
|
my ($class, $action) = @_; |
90
|
|
|
|
|
|
|
|
91
|
10
|
|
|
|
|
19
|
my $finder = Module::Pluggable::Object->new( |
92
|
|
|
|
|
|
|
search_path => $class->default_search_path, |
93
|
|
|
|
|
|
|
require => 1, |
94
|
|
|
|
|
|
|
); |
95
|
|
|
|
|
|
|
|
96
|
10
|
100
|
|
|
|
81
|
@PLUGINS = $finder->plugins if !@PLUGINS; |
97
|
10
|
|
|
14
|
|
4289
|
return first { $_->has_action($action) } @PLUGINS; |
|
14
|
|
|
|
|
46
|
|
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
__END__ |