| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Amon2::Plugin::CLI; |
|
2
|
6
|
|
|
6
|
|
6673
|
use strict; |
|
|
6
|
|
|
|
|
12
|
|
|
|
6
|
|
|
|
|
155
|
|
|
3
|
6
|
|
|
6
|
|
30
|
use warnings; |
|
|
6
|
|
|
|
|
13
|
|
|
|
6
|
|
|
|
|
148
|
|
|
4
|
|
|
|
|
|
|
|
|
5
|
6
|
|
|
6
|
|
1337
|
use Plack::Util; |
|
|
6
|
|
|
|
|
12021
|
|
|
|
6
|
|
|
|
|
160
|
|
|
6
|
6
|
|
|
6
|
|
735
|
use Amon2::Util qw/add_method/; |
|
|
6
|
|
|
|
|
25575
|
|
|
|
6
|
|
|
|
|
4619
|
|
|
7
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
my $CLI_OPT_KEY = '.'. __PACKAGE__; |
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub init { |
|
11
|
5
|
|
|
5
|
1
|
55
|
my ($self, $c, $code_conf) = @_; |
|
12
|
|
|
|
|
|
|
|
|
13
|
5
|
50
|
|
|
|
31
|
if ($code_conf->{cli_opt_key}) { |
|
14
|
0
|
|
|
|
|
0
|
$CLI_OPT_KEY = $code_conf->{cli_opt_key}; |
|
15
|
|
|
|
|
|
|
} |
|
16
|
|
|
|
|
|
|
|
|
17
|
5
|
|
50
|
|
|
35
|
my $run_method = $code_conf->{run_method} || 'run'; |
|
18
|
|
|
|
|
|
|
|
|
19
|
5
|
|
50
|
|
|
61
|
my $getopt = $code_conf->{getopt} || [qw/:config posix_default no_ignore_case gnu_compat/]; |
|
20
|
5
|
|
|
|
|
5818
|
require Getopt::Long; |
|
21
|
5
|
|
|
|
|
57999
|
Getopt::Long->import(@{$getopt}); |
|
|
5
|
|
|
|
|
24
|
|
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
add_method($c => $run_method, sub { |
|
24
|
5
|
|
|
5
|
|
5496
|
my ($c, $arg) = @_; |
|
25
|
|
|
|
|
|
|
|
|
26
|
5
|
|
|
|
|
12
|
eval { |
|
27
|
5
|
100
|
|
|
|
20
|
if (ref $arg eq 'CODE') { |
|
28
|
2
|
|
|
|
|
6
|
$arg->($c); |
|
29
|
|
|
|
|
|
|
} |
|
30
|
|
|
|
|
|
|
else { |
|
31
|
3
|
|
|
|
|
6
|
my $klass = $arg; |
|
32
|
3
|
|
|
|
|
18
|
my $runner = Plack::Util::load_class($klass, $code_conf->{base}); |
|
33
|
3
|
|
50
|
|
|
1747
|
my $method = $code_conf->{method} || 'main'; |
|
34
|
3
|
|
|
|
|
21
|
$runner->$method($c); |
|
35
|
|
|
|
|
|
|
} |
|
36
|
|
|
|
|
|
|
}; |
|
37
|
5
|
100
|
|
|
|
209
|
if (my $e = $@) { |
|
38
|
1
|
50
|
|
|
|
4
|
if ($code_conf->{on_error}) { |
|
39
|
0
|
|
|
|
|
0
|
$code_conf->{on_error}->($c, $e); |
|
40
|
|
|
|
|
|
|
} |
|
41
|
|
|
|
|
|
|
else { |
|
42
|
1
|
|
|
|
|
5
|
_croak("$0\t$e"); |
|
43
|
|
|
|
|
|
|
} |
|
44
|
|
|
|
|
|
|
} |
|
45
|
5
|
|
|
|
|
1086
|
}); |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
add_method($c => 'show_usage', sub { |
|
49
|
1
|
|
|
1
|
|
1052
|
my ($self, %args) = @_; |
|
50
|
|
|
|
|
|
|
|
|
51
|
1
|
|
|
|
|
903
|
require Pod::Usage; |
|
52
|
1
|
|
|
|
|
70111
|
Pod::Usage::pod2usage(%args); |
|
53
|
5
|
|
|
|
|
72
|
}); |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
add_method($c => 'parse_opt', sub { |
|
56
|
1
|
|
|
1
|
|
8
|
my ($c, %options) = @_; |
|
57
|
|
|
|
|
|
|
|
|
58
|
1
|
|
|
|
|
3
|
my @cli_args = @ARGV; # save @ARGV |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
Getopt::Long::GetOptionsFromArray( |
|
61
|
|
|
|
|
|
|
\@cli_args, |
|
62
|
|
|
|
|
|
|
%options, |
|
63
|
|
|
|
|
|
|
'h' => sub { |
|
64
|
0
|
|
|
0
|
|
0
|
$c->show_usage(-exitval => 1); |
|
65
|
|
|
|
|
|
|
}, |
|
66
|
|
|
|
|
|
|
'help' => sub { |
|
67
|
0
|
|
|
0
|
|
0
|
$c->show_usage(-exitval => 1, -verbose => 2); |
|
68
|
|
|
|
|
|
|
}, |
|
69
|
1
|
50
|
|
|
|
11
|
) or $c->show_usage(-exitval => 2); |
|
70
|
|
|
|
|
|
|
|
|
71
|
1
|
|
|
|
|
350
|
return $c; |
|
72
|
5
|
|
|
|
|
56
|
}); |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
add_method($c => 'setopt', sub { |
|
75
|
1
|
|
|
1
|
|
2
|
my ($c, $opt) = @_; |
|
76
|
|
|
|
|
|
|
|
|
77
|
1
|
50
|
|
|
|
5
|
_croak('$opt is not HASH') unless ref $opt eq 'HASH'; |
|
78
|
|
|
|
|
|
|
|
|
79
|
1
|
|
|
|
|
7
|
$c->{$CLI_OPT_KEY} = $opt; |
|
80
|
|
|
|
|
|
|
|
|
81
|
1
|
|
|
|
|
3
|
return $c; |
|
82
|
5
|
|
|
|
|
60
|
}); |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
add_method($c => 'getopt', sub { |
|
85
|
1
|
|
|
1
|
|
2
|
my ($c, $opt_key) = @_; |
|
86
|
|
|
|
|
|
|
|
|
87
|
1
|
50
|
33
|
|
|
8
|
if (!defined $opt_key || $opt_key eq '') { |
|
88
|
0
|
|
|
|
|
0
|
return $c->{$CLI_OPT_KEY}; |
|
89
|
|
|
|
|
|
|
} |
|
90
|
|
|
|
|
|
|
|
|
91
|
1
|
|
|
|
|
62
|
return $c->{$CLI_OPT_KEY}{$opt_key}; |
|
92
|
5
|
|
|
|
|
53
|
}); |
|
93
|
|
|
|
|
|
|
} |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub _croak { |
|
96
|
1
|
|
|
1
|
|
2
|
my ($msg) = @_; |
|
97
|
|
|
|
|
|
|
|
|
98
|
1
|
|
|
|
|
6
|
require Carp; |
|
99
|
1
|
|
|
|
|
225
|
Carp::croak($msg); |
|
100
|
|
|
|
|
|
|
} |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
1; |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
__END__ |