| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package MooseX::Getopt::Kingpin; |
|
2
|
2
|
|
|
2
|
|
1930
|
use Moose::Role; |
|
|
2
|
|
|
|
|
536765
|
|
|
|
2
|
|
|
|
|
10
|
|
|
3
|
|
|
|
|
|
|
|
|
4
|
2
|
|
|
2
|
|
12091
|
use Safe::Isa; |
|
|
2
|
|
|
|
|
906
|
|
|
|
2
|
|
|
|
|
685
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.1.2'; |
|
7
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 NAME |
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
MooseX::Getopt::Kingpin - A Moose role for processing command lines options via Getopt::Kingpin |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
### In your class |
|
15
|
|
|
|
|
|
|
package MyClass { |
|
16
|
|
|
|
|
|
|
use Moose; |
|
17
|
|
|
|
|
|
|
with 'MooseX::Getopt::Kingpin'; |
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
my $lines_default = 10; |
|
20
|
|
|
|
|
|
|
has 'lines' => ( |
|
21
|
|
|
|
|
|
|
is => 'ro', |
|
22
|
|
|
|
|
|
|
isa => 'Int', |
|
23
|
|
|
|
|
|
|
default => $lines_default, |
|
24
|
|
|
|
|
|
|
documentation => sub ($kingpin) { |
|
25
|
|
|
|
|
|
|
$kingpin->flag('lines', 'print first N lines') |
|
26
|
|
|
|
|
|
|
->default($lines_default) |
|
27
|
|
|
|
|
|
|
->short('n') |
|
28
|
|
|
|
|
|
|
->int(); |
|
29
|
|
|
|
|
|
|
}, |
|
30
|
|
|
|
|
|
|
); |
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
has 'input_file' => ( |
|
33
|
|
|
|
|
|
|
is => 'ro', |
|
34
|
|
|
|
|
|
|
isa => 'Path::Tiny', |
|
35
|
|
|
|
|
|
|
required => 1, |
|
36
|
|
|
|
|
|
|
documentation => sub ($kingpin) { |
|
37
|
|
|
|
|
|
|
$kingpin->arg('input_file', 'input_file') |
|
38
|
|
|
|
|
|
|
->required |
|
39
|
|
|
|
|
|
|
->existing_file(); |
|
40
|
|
|
|
|
|
|
}, |
|
41
|
|
|
|
|
|
|
); |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
has 'other_attr' => (is => 'ro', isa => 'Str'); |
|
44
|
|
|
|
|
|
|
}; |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
my $kingpin = Getopt::Kingpin->new(); |
|
47
|
|
|
|
|
|
|
my $other_flag = $kingpin->flag('other_flag', 'this flag do something ...')->bool(); |
|
48
|
|
|
|
|
|
|
$kingpin->version($MyClass::VERSION); |
|
49
|
|
|
|
|
|
|
MyClass->new_with_options( |
|
50
|
|
|
|
|
|
|
$kingpin, |
|
51
|
|
|
|
|
|
|
other_attr => 'xxx' |
|
52
|
|
|
|
|
|
|
); |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
if $other_flag { |
|
55
|
|
|
|
|
|
|
... |
|
56
|
|
|
|
|
|
|
} |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
This is a role which provides an alternate constructor for creating objects using parameters passed in from the command line. |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
Thi role use L<Getopt::Kingpin> as command line processor, MOP and documentation trick. |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head1 METHODS |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=head2 new_with_options($kingpin, %options) |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
C<$kingpin> instance of L<Getopt::Kingpin> is required |
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
C<%options> - classic Moose options, override options set via kingpin |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=cut |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub new_with_options { |
|
75
|
4
|
|
|
4
|
1
|
18250
|
my ($class, $kingpin, %options) = @_; |
|
76
|
|
|
|
|
|
|
|
|
77
|
4
|
100
|
|
|
|
17
|
die 'First parameter ins\'t Getopt::Kingpin instance' if !$kingpin->$_isa('Getopt::Kingpin'); |
|
78
|
|
|
|
|
|
|
|
|
79
|
3
|
|
|
|
|
59
|
my %kingpin_opts = generate_kingpin_options_from_moose_documentation($class, $kingpin), |
|
80
|
|
|
|
|
|
|
$kingpin->parse(); |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
return $class->new( |
|
83
|
2
|
|
|
|
|
2764
|
map {$_ => $kingpin_opts{$_}->value()} keys %kingpin_opts, |
|
|
4
|
|
|
|
|
107
|
|
|
84
|
|
|
|
|
|
|
%options |
|
85
|
|
|
|
|
|
|
); |
|
86
|
|
|
|
|
|
|
} |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub generate_kingpin_options_from_moose_documentation { |
|
89
|
3
|
|
|
3
|
0
|
7
|
my ($class, $kingpin) = @_; |
|
90
|
|
|
|
|
|
|
|
|
91
|
3
|
|
|
|
|
5
|
my %options; |
|
92
|
3
|
|
|
|
|
14
|
foreach my $attr (sort { $a->name cmp $b->name } $class->meta->get_all_attributes()) { |
|
|
8
|
|
|
|
|
214
|
|
|
93
|
9
|
100
|
|
|
|
4086
|
if (ref $attr->documentation eq 'CODE') { |
|
94
|
6
|
|
|
|
|
190
|
$options{ $attr->name } = $attr->documentation->($kingpin); |
|
95
|
|
|
|
|
|
|
} |
|
96
|
|
|
|
|
|
|
} |
|
97
|
|
|
|
|
|
|
|
|
98
|
3
|
|
|
|
|
41
|
return %options; |
|
99
|
|
|
|
|
|
|
} |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=over |
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=item * |
|
106
|
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
L<MooseX::Getopt> |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=back |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head1 contributing |
|
112
|
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
for dependency use [cpanfile](cpanfile)... |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
for resolve dependency use [carton](https://metacpan.org/pod/Carton) (or carton - is more experimental) |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
carton install |
|
118
|
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
for run test use C<minil test> |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
carton exec minil test |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
if you don't have perl environment, is best way use docker |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
docker run -it -v $PWD:/tmp/work -w /tmp/work avastsoftware/perl-extended carton install |
|
127
|
|
|
|
|
|
|
docker run -it -v $PWD:/tmp/work -w /tmp/work avastsoftware/perl-extended carton exec minil test |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=head2 warning |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
docker run default as root, all files which will be make in docker will be have root rights |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
one solution is change rights in docker |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
docker run -it -v $PWD:/tmp/work -w /tmp/work avastsoftware/perl-extended bash -c "carton install; chmod -R 0777 ." |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
or after docker command (but you must have root rights) |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=head1 LICENSE |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
Copyright (C) Avast Software. |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
|
144
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head1 AUTHOR |
|
147
|
|
|
|
|
|
|
Jan Seidl E<lt>seidl@avast.comE<gt> |
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=cut |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
1; |