line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package YA::CLI::ActionRole; |
2
|
|
|
|
|
|
|
our $VERSION = '0.003'; |
3
|
1
|
|
|
1
|
|
13474
|
use Moo::Role; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
6
|
|
4
|
1
|
|
|
1
|
|
492
|
use namespace::autoclean; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
5
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
# ABSTRACT: Action handler role |
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
109
|
use YA::CLI::Usage; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
34
|
|
9
|
1
|
|
|
1
|
|
31
|
use Getopt::Long; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
12
|
|
10
|
1
|
|
|
1
|
|
192
|
use List::Util qw(any); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
605
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
requires qw( |
13
|
|
|
|
|
|
|
action |
14
|
|
|
|
|
|
|
run |
15
|
|
|
|
|
|
|
); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
has _cli_args => ( |
18
|
|
|
|
|
|
|
is => 'ro', |
19
|
|
|
|
|
|
|
predicate => '_has_cli_args', |
20
|
|
|
|
|
|
|
writer => '_set_cli_args', |
21
|
|
|
|
|
|
|
init_args => undef, |
22
|
|
|
|
|
|
|
default => sub { {} }, |
23
|
|
|
|
|
|
|
); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub cli_options { |
26
|
3
|
|
|
3
|
1
|
9
|
return; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub usage_pod { |
30
|
2
|
|
|
2
|
1
|
5
|
return; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub BUILD { |
34
|
5
|
|
|
5
|
0
|
24
|
my ($self, $args) = @_; |
35
|
|
|
|
|
|
|
|
36
|
5
|
|
|
|
|
65
|
foreach (keys %$args) { |
37
|
2
|
100
|
|
|
|
22
|
delete $args->{$_} if $self->can($_); |
38
|
|
|
|
|
|
|
} |
39
|
5
|
100
|
|
|
|
36
|
$self->_set_cli_args($args) if %$args; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub new_from_args { |
43
|
5
|
|
|
5
|
1
|
9
|
my ($class, $args) = @_; |
44
|
5
|
|
|
|
|
10
|
return $class->new($class->get_opts($args)); |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub get_opts { |
48
|
5
|
|
|
5
|
1
|
8
|
my ($class, $args) = @_; |
49
|
|
|
|
|
|
|
|
50
|
5
|
|
|
|
|
23
|
my $p = Getopt::Long::Parser->new( |
51
|
|
|
|
|
|
|
config => [qw(no_auto_abbrev) ] |
52
|
|
|
|
|
|
|
); |
53
|
|
|
|
|
|
|
|
54
|
5
|
|
|
|
|
477
|
my %cli_args; |
55
|
5
|
|
|
|
|
14
|
$p->getoptionsfromarray($args, \%cli_args, $class->cli_options); |
56
|
5
|
|
|
|
|
1039
|
return %cli_args; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub has_action { |
60
|
16
|
|
|
16
|
1
|
35
|
my ($self, $action) = @_; |
61
|
16
|
|
|
16
|
|
56
|
return any { $action eq $_ } $self->action; |
|
16
|
|
|
|
|
133
|
|
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub as_help { |
65
|
7
|
|
|
7
|
1
|
33
|
my ($self, $rc, $message) = @_; |
66
|
|
|
|
|
|
|
|
67
|
7
|
100
|
100
|
|
|
27
|
return YA::CLI::Usage->new( |
68
|
|
|
|
|
|
|
rc => ($rc // 0), |
69
|
|
|
|
|
|
|
$message ? (message => $message) : (), |
70
|
|
|
|
|
|
|
$self->_get_podfile_for_usage, |
71
|
|
|
|
|
|
|
); |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub as_manpage { |
75
|
2
|
|
|
2
|
1
|
4
|
my ($self) = @_; |
76
|
|
|
|
|
|
|
|
77
|
2
|
|
|
|
|
5
|
return YA::CLI::Usage->new( |
78
|
|
|
|
|
|
|
rc => 0, |
79
|
|
|
|
|
|
|
verbose => 2, |
80
|
|
|
|
|
|
|
$self->_get_podfile_for_usage, |
81
|
|
|
|
|
|
|
); |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub _get_podfile_for_usage { |
86
|
9
|
|
|
9
|
|
13
|
my $self = shift; |
87
|
9
|
|
|
|
|
16
|
my $podfile; |
88
|
9
|
100
|
|
|
|
20
|
if (my $pod = $self->usage_pod) { |
89
|
1
|
50
|
|
|
|
13
|
$podfile = $pod == 1? $self : $pod; |
90
|
|
|
|
|
|
|
} |
91
|
9
|
100
|
|
|
|
208
|
return $podfile ? ( pod_file => $podfile ) : (); |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
1; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
__END__ |