line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
2
|
|
|
2
|
|
15
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
71
|
|
2
|
2
|
|
|
2
|
|
13
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
132
|
|
3
|
|
|
|
|
|
|
package Getopt::Long::Descriptive::Usage 0.110; |
4
|
|
|
|
|
|
|
# ABSTRACT: the usage description for GLD |
5
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
14
|
use List::Util qw(max); |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
3250
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
#pod =head1 SYNOPSIS |
9
|
|
|
|
|
|
|
#pod |
10
|
|
|
|
|
|
|
#pod use Getopt::Long::Descriptive; |
11
|
|
|
|
|
|
|
#pod my ($opt, $usage) = describe_options( ... ); |
12
|
|
|
|
|
|
|
#pod |
13
|
|
|
|
|
|
|
#pod $usage->text; # complete usage message |
14
|
|
|
|
|
|
|
#pod |
15
|
|
|
|
|
|
|
#pod $usage->die; # die with usage message |
16
|
|
|
|
|
|
|
#pod |
17
|
|
|
|
|
|
|
#pod =head1 DESCRIPTION |
18
|
|
|
|
|
|
|
#pod |
19
|
|
|
|
|
|
|
#pod This document only describes the methods of the Usage object. For information |
20
|
|
|
|
|
|
|
#pod on how to use L, consult its documentation. |
21
|
|
|
|
|
|
|
#pod |
22
|
|
|
|
|
|
|
#pod =head1 METHODS |
23
|
|
|
|
|
|
|
#pod |
24
|
|
|
|
|
|
|
#pod =head2 new |
25
|
|
|
|
|
|
|
#pod |
26
|
|
|
|
|
|
|
#pod my $usage = Getopt::Long::Descriptive::Usage->new(\%arg); |
27
|
|
|
|
|
|
|
#pod |
28
|
|
|
|
|
|
|
#pod You B don't need to call this. GLD will do it for you. |
29
|
|
|
|
|
|
|
#pod |
30
|
|
|
|
|
|
|
#pod Valid arguments are: |
31
|
|
|
|
|
|
|
#pod |
32
|
|
|
|
|
|
|
#pod options - an arrayref of options |
33
|
|
|
|
|
|
|
#pod leader_text - the text that leads the usage; this may go away! |
34
|
|
|
|
|
|
|
#pod |
35
|
|
|
|
|
|
|
#pod =cut |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub new { |
38
|
29
|
|
|
29
|
1
|
73
|
my ($class, $arg) = @_; |
39
|
|
|
|
|
|
|
|
40
|
29
|
|
|
|
|
65
|
my @to_copy = qw(leader_text options show_defaults); |
41
|
|
|
|
|
|
|
|
42
|
29
|
|
|
|
|
41
|
my %copy; |
43
|
29
|
|
|
|
|
123
|
@copy{ @to_copy } = @$arg{ @to_copy }; |
44
|
|
|
|
|
|
|
|
45
|
29
|
|
|
|
|
110
|
bless \%copy => $class; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
#pod =head2 text |
49
|
|
|
|
|
|
|
#pod |
50
|
|
|
|
|
|
|
#pod This returns the full text of the usage message. |
51
|
|
|
|
|
|
|
#pod |
52
|
|
|
|
|
|
|
#pod =cut |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub text { |
55
|
15
|
|
|
15
|
1
|
135
|
my ($self) = @_; |
56
|
|
|
|
|
|
|
|
57
|
15
|
|
|
|
|
38
|
return join qq{\n}, $self->leader_text, $self->option_text; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
#pod =head2 leader_text |
61
|
|
|
|
|
|
|
#pod |
62
|
|
|
|
|
|
|
#pod This returns the text that comes at the beginning of the usage message. |
63
|
|
|
|
|
|
|
#pod |
64
|
|
|
|
|
|
|
#pod =cut |
65
|
|
|
|
|
|
|
|
66
|
15
|
|
|
15
|
1
|
100
|
sub leader_text { $_[0]->{leader_text} } |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
#pod =head2 option_text |
69
|
|
|
|
|
|
|
#pod |
70
|
|
|
|
|
|
|
#pod This returns the text describing the available options. |
71
|
|
|
|
|
|
|
#pod |
72
|
|
|
|
|
|
|
#pod =cut |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub option_text { |
75
|
15
|
|
|
15
|
1
|
31
|
my ($self) = @_; |
76
|
|
|
|
|
|
|
|
77
|
15
|
|
|
|
|
25
|
my $string = q{}; |
78
|
|
|
|
|
|
|
|
79
|
15
|
50
|
|
|
|
22
|
my @options = @{ $self->{options} || [] }; |
|
15
|
|
|
|
|
59
|
|
80
|
15
|
|
|
|
|
34
|
my @specs = map { $_->{spec} } grep { $_->{desc} ne 'spacer' } @options; |
|
39
|
|
|
|
|
78
|
|
|
50
|
|
|
|
|
108
|
|
81
|
15
|
|
100
|
|
|
34
|
my $length = (max(map { _option_length($_) } @specs) || 0); |
82
|
15
|
|
|
|
|
39
|
my $spec_fmt = "\t%-${length}s"; |
83
|
|
|
|
|
|
|
|
84
|
15
|
|
|
|
|
40
|
while (@options) { |
85
|
50
|
|
|
|
|
75
|
my $opt = shift @options; |
86
|
50
|
|
|
|
|
126
|
my $spec = $opt->{spec}; |
87
|
50
|
|
|
|
|
79
|
my $desc = $opt->{desc}; |
88
|
|
|
|
|
|
|
|
89
|
50
|
100
|
|
|
|
97
|
if ($desc eq 'spacer') { |
90
|
11
|
100
|
|
|
|
27
|
if (ref $opt->{spec}) { |
91
|
2
|
|
|
|
|
3
|
$string .= "${ $opt->{spec} }\n"; |
|
2
|
|
|
|
|
7
|
|
92
|
2
|
|
|
|
|
6
|
next; |
93
|
|
|
|
|
|
|
} else { |
94
|
9
|
|
|
|
|
21
|
my @lines = $self->_split_description($length, $opt->{spec}); |
95
|
|
|
|
|
|
|
|
96
|
9
|
100
|
|
|
|
38
|
$string .= length($_) ? sprintf("$spec_fmt\n", $_) : "\n" for @lines; |
97
|
9
|
|
|
|
|
25
|
next; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
39
|
|
|
|
|
114
|
($spec, my $assign) = Getopt::Long::Descriptive->_strip_assignment($spec); |
102
|
|
|
|
|
|
|
|
103
|
39
|
|
|
|
|
86
|
my ($pre, $post) = _parse_assignment($assign); |
104
|
39
|
|
|
|
|
102
|
my @names = split /\|/, $spec; |
105
|
|
|
|
|
|
|
|
106
|
39
|
|
|
|
|
69
|
my $primary = shift @names; |
107
|
39
|
|
|
|
|
57
|
my $short; |
108
|
39
|
|
|
|
|
83
|
my ($i) = grep {; length $names[$_] == 1 } (0 .. $#names); |
|
15
|
|
|
|
|
40
|
|
109
|
39
|
100
|
|
|
|
82
|
if (defined $i) { |
110
|
15
|
|
|
|
|
31
|
$short = splice @names, $i, 1; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
39
|
50
|
|
|
|
94
|
$spec = length $primary > 1 ? "--$pre$primary$post" : "-$primary$post"; |
114
|
39
|
100
|
|
|
|
103
|
$spec .= " (or -$short)" if $short; |
115
|
|
|
|
|
|
|
|
116
|
39
|
|
|
|
|
87
|
my @desc = $self->_split_description($length, $desc); |
117
|
|
|
|
|
|
|
|
118
|
39
|
50
|
|
|
|
81
|
if (@names) { |
119
|
|
|
|
|
|
|
push @desc, |
120
|
0
|
0
|
|
|
|
0
|
"aka " . join q{, }, map { length > 1 ? "--$_" : "-$_" } @names; |
|
0
|
|
|
|
|
0
|
|
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
# add default value if it exists |
124
|
39
|
0
|
33
|
|
|
85
|
if (exists $opt->{constraint}->{default} and $self->{show_defaults}) { |
125
|
0
|
|
|
|
|
0
|
my $dflt = $opt->{constraint}->{default}; |
126
|
0
|
0
|
|
|
|
0
|
$dflt = ! defined $dflt ? '(undef)' |
|
|
0
|
|
|
|
|
|
127
|
|
|
|
|
|
|
: ! length $dflt ? '(empty string)' |
128
|
|
|
|
|
|
|
: $dflt; |
129
|
0
|
|
|
|
|
0
|
push @desc, "(default value: $dflt)"; |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
39
|
|
|
|
|
153
|
$string .= sprintf "$spec_fmt %s\n", $spec, shift @desc; |
133
|
39
|
|
|
|
|
144
|
for my $line (@desc) { |
134
|
2
|
|
|
|
|
4
|
$string .= "\t"; |
135
|
2
|
|
|
|
|
7
|
$string .= q{ } x ( $length + 2 ); |
136
|
2
|
|
|
|
|
21
|
$string .= "$line\n"; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
15
|
|
|
|
|
104
|
return $string; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
sub _option_length { |
144
|
39
|
|
|
39
|
|
74
|
my ($fullspec) = @_; |
145
|
|
|
|
|
|
|
|
146
|
39
|
|
|
|
|
100
|
my ($spec, $argspec) = Getopt::Long::Descriptive->_strip_assignment($fullspec); |
147
|
|
|
|
|
|
|
|
148
|
39
|
|
|
|
|
97
|
my ($pre, $post) = _parse_assignment($argspec); |
149
|
39
|
|
|
|
|
105
|
my @names = split /\|/, $spec; |
150
|
|
|
|
|
|
|
|
151
|
39
|
|
|
|
|
72
|
my $primary = shift @names; |
152
|
39
|
100
|
66
|
|
|
116
|
my $short = (@names && length $names[0] eq 1) |
153
|
|
|
|
|
|
|
? shift @names |
154
|
|
|
|
|
|
|
: undef; |
155
|
|
|
|
|
|
|
|
156
|
39
|
50
|
|
|
|
123
|
$spec = length $primary > 1 ? "--$pre$primary$post" : "-$primary$post"; |
157
|
39
|
100
|
|
|
|
85
|
$spec .= " (or -$short)" if $short; |
158
|
|
|
|
|
|
|
|
159
|
39
|
|
|
|
|
120
|
return length $spec; |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
sub _max_line_length { |
163
|
48
|
|
|
48
|
|
96
|
return $Getopt::Long::Descriptive::TERM_WIDTH - 2; |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
sub _split_description { |
167
|
48
|
|
|
48
|
|
93
|
my ($self, $length, $desc) = @_; |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
# 8 for a tab, 2 for the space between option & desc, 2 more for gutter |
170
|
48
|
|
|
|
|
84
|
my $max_length = $self->_max_line_length - ( $length + 8 + 2 ); |
171
|
|
|
|
|
|
|
|
172
|
48
|
100
|
|
|
|
127
|
return $desc if length $desc <= $max_length; |
173
|
|
|
|
|
|
|
|
174
|
3
|
|
|
|
|
6
|
my @lines; |
175
|
3
|
|
|
|
|
8
|
while (length $desc > $max_length) { |
176
|
5
|
|
|
|
|
11
|
my $idx = rindex( substr( $desc, 0, $max_length ), q{ }, ); |
177
|
5
|
100
|
|
|
|
13
|
last unless $idx >= 0; |
178
|
4
|
|
|
|
|
11
|
push @lines, substr($desc, 0, $idx); |
179
|
4
|
|
|
|
|
11
|
substr($desc, 0, $idx + 1) = q{}; |
180
|
|
|
|
|
|
|
} |
181
|
3
|
|
|
|
|
7
|
push @lines, $desc; |
182
|
|
|
|
|
|
|
|
183
|
3
|
|
|
|
|
9
|
return @lines; |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
sub _parse_assignment { |
187
|
96
|
|
|
96
|
|
2028
|
my ($assign_spec) = @_; |
188
|
|
|
|
|
|
|
|
189
|
96
|
|
|
|
|
148
|
my $result = 'STR'; |
190
|
96
|
|
|
|
|
132
|
my $desttype; |
191
|
96
|
100
|
|
|
|
206
|
if (length($assign_spec) < 2) { |
192
|
|
|
|
|
|
|
# empty, ! or + |
193
|
62
|
100
|
|
|
|
125
|
return ('[no-]', '') if $assign_spec eq '!'; |
194
|
58
|
|
|
|
|
128
|
return ('', ''); |
195
|
|
|
|
|
|
|
} |
196
|
|
|
|
|
|
|
|
197
|
34
|
|
|
|
|
73
|
my $optional = substr($assign_spec, 0, 1) eq ':'; |
198
|
34
|
|
|
|
|
57
|
my $argument = substr $assign_spec, 1, 2; |
199
|
|
|
|
|
|
|
|
200
|
34
|
100
|
100
|
|
|
195
|
if ($argument =~ m/^[io]/ or $assign_spec =~ m/^:[+0-9]/) { |
|
|
100
|
|
|
|
|
|
201
|
10
|
|
|
|
|
16
|
$result = 'INT'; |
202
|
|
|
|
|
|
|
} elsif ($argument =~ m/^f/) { |
203
|
4
|
|
|
|
|
9
|
$result = 'NUM'; |
204
|
|
|
|
|
|
|
} |
205
|
|
|
|
|
|
|
|
206
|
34
|
100
|
|
|
|
93
|
if (length($assign_spec) > 2) { |
207
|
16
|
|
|
|
|
29
|
$desttype = substr($assign_spec, 2, 1); |
208
|
16
|
100
|
|
|
|
37
|
if ($desttype eq '@') { |
|
|
50
|
|
|
|
|
|
209
|
|
|
|
|
|
|
# Imply it can be repeated |
210
|
8
|
|
|
|
|
14
|
$result .= '...'; |
211
|
|
|
|
|
|
|
} elsif ($desttype eq '%') { |
212
|
8
|
|
|
|
|
18
|
$result = "KEY=${result}..."; |
213
|
|
|
|
|
|
|
} |
214
|
|
|
|
|
|
|
} |
215
|
|
|
|
|
|
|
|
216
|
34
|
100
|
|
|
|
71
|
if ($optional) { |
217
|
10
|
|
|
|
|
45
|
return ("", "[=$result]"); |
218
|
|
|
|
|
|
|
} |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
# with leading space so it can just blindly be appended. |
221
|
24
|
|
|
|
|
91
|
return ("", " $result"); |
222
|
|
|
|
|
|
|
} |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
#pod =head2 warn |
225
|
|
|
|
|
|
|
#pod |
226
|
|
|
|
|
|
|
#pod This warns with the usage message. |
227
|
|
|
|
|
|
|
#pod |
228
|
|
|
|
|
|
|
#pod =cut |
229
|
|
|
|
|
|
|
|
230
|
0
|
|
|
0
|
1
|
0
|
sub warn { warn shift->text } |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
#pod =head2 die |
233
|
|
|
|
|
|
|
#pod |
234
|
|
|
|
|
|
|
#pod This throws the usage message as an exception. |
235
|
|
|
|
|
|
|
#pod |
236
|
|
|
|
|
|
|
#pod $usage_obj->die(\%arg); |
237
|
|
|
|
|
|
|
#pod |
238
|
|
|
|
|
|
|
#pod Some arguments can be provided |
239
|
|
|
|
|
|
|
#pod |
240
|
|
|
|
|
|
|
#pod pre_text - text to be prepended to the usage message |
241
|
|
|
|
|
|
|
#pod post_text - text to be appended to the usage message |
242
|
|
|
|
|
|
|
#pod |
243
|
|
|
|
|
|
|
#pod The C and C arguments are concatenated with the usage |
244
|
|
|
|
|
|
|
#pod message with no line breaks, so supply this if you need them. |
245
|
|
|
|
|
|
|
#pod |
246
|
|
|
|
|
|
|
#pod =cut |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
sub die { |
249
|
3
|
|
|
3
|
1
|
9
|
my $self = shift; |
250
|
3
|
|
50
|
|
|
21
|
my $arg = shift || {}; |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
die( |
253
|
9
|
|
|
|
|
78
|
join q{}, grep { defined } $arg->{pre_text}, $self->text, $arg->{post_text} |
254
|
3
|
|
|
|
|
12
|
); |
255
|
|
|
|
|
|
|
} |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
use overload ( |
258
|
|
|
|
|
|
|
q{""} => "text", |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
# This is only needed because Usage used to be a blessed coderef that worked |
261
|
|
|
|
|
|
|
# this way. Later we can toss a warning in here. -- rjbs, 2009-08-19 |
262
|
|
|
|
|
|
|
'&{}' => sub { |
263
|
2
|
|
|
2
|
|
6
|
my ($self) = @_; |
264
|
2
|
|
|
|
|
319
|
Carp::cluck("use of __PACKAGE__ objects as a code ref is deprecated"); |
265
|
2
|
50
|
|
2
|
|
156
|
return sub { return $_[0] ? $self->text : $self->warn; }; |
|
2
|
|
|
|
|
10
|
|
266
|
|
|
|
|
|
|
} |
267
|
2
|
|
|
2
|
|
26
|
); |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
21
|
|
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
1; |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
__END__ |