line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Options::Generator; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
23077
|
use 5.006; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
37
|
|
4
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
36
|
|
5
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
25
|
|
6
|
1
|
|
|
1
|
|
6
|
use Carp; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
645
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 NAME |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
Options::Generator - Build options for the command line from a perl data structure |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 VERSION |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
Version 0.01 |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=cut |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=head1 SYNOPSIS |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
use Options::Generator; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
my $og = Options::Generator->new; |
27
|
|
|
|
|
|
|
print $og->generate($data); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
or more detailed... |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
my $og = Options::Generater->new({ |
33
|
|
|
|
|
|
|
outer_prefix => [ '-', '--' ], |
34
|
|
|
|
|
|
|
outer_assign => ' ', # default |
35
|
|
|
|
|
|
|
outer_separate => ' ', # default |
36
|
|
|
|
|
|
|
inner_assign => '=', # default |
37
|
|
|
|
|
|
|
inner_separate => ',', # default |
38
|
|
|
|
|
|
|
... |
39
|
|
|
|
|
|
|
}); |
40
|
|
|
|
|
|
|
my $data = { |
41
|
|
|
|
|
|
|
foo => 'bar', |
42
|
|
|
|
|
|
|
o => undef, |
43
|
|
|
|
|
|
|
s => 'silly', |
44
|
|
|
|
|
|
|
options => [ 'one', 'two', { this => that }], |
45
|
|
|
|
|
|
|
blah => undef, |
46
|
|
|
|
|
|
|
}; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
prints: |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
--foo bar --options one,two,this=that, --blah -o -s silly |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=head1 SUBROUTINES/METHODS |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=head2 Options:Generator->new($args); |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
Accepts a hashref of definitions |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=over 4 |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=item * outer_prefix |
64
|
|
|
|
|
|
|
The prefix character to add to the outer options, defaults to a single hyphen if not specified. This is the only option that can accept an array reference for options that are single length. The first element will be assigned to single length options, the second element will be assigned to options greater that a single character. |
65
|
|
|
|
|
|
|
outer_prefx => '--' # --foo |
66
|
|
|
|
|
|
|
outer_prefix => '-' # -foo |
67
|
|
|
|
|
|
|
outer_prefix => [ '-', '--'] # -f --foo --bar -s -c |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=item * outer_assign |
70
|
|
|
|
|
|
|
The character to assign a value to the option, defaults to space if not specified. |
71
|
|
|
|
|
|
|
--foo bar |
72
|
|
|
|
|
|
|
outer_assign => '=' # --foo=bar |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=item * outer_separate |
75
|
|
|
|
|
|
|
The character to separate outer most options. Defaults to space if not specified. |
76
|
|
|
|
|
|
|
--foo bar |
77
|
|
|
|
|
|
|
outer_separate => ',' # --foo=bar,--boo=baz |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=item * inner_prefx |
80
|
|
|
|
|
|
|
The prefix character to add to the inner options (if applicable). No prefix by default |
81
|
|
|
|
|
|
|
--foo bar,baz,this=that |
82
|
|
|
|
|
|
|
inner_prefix => '+' # --foo +bar,+baz,+this=that |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=item * inner_assign |
85
|
|
|
|
|
|
|
The character to assign values to the inner options. Defaults to equals sign. |
86
|
|
|
|
|
|
|
--foo this=that,boo=baz |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=item * inner_separate |
89
|
|
|
|
|
|
|
The character to separate inner options. Defaults to comma |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
Examples of outputs with defaults |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
--foo |
95
|
|
|
|
|
|
|
--foo --bar |
96
|
|
|
|
|
|
|
--foo -b -z --bar |
97
|
|
|
|
|
|
|
--foo bar=baz,this=that -o -s --options -f blah |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=cut |
102
|
|
|
|
|
|
|
sub new { |
103
|
0
|
|
|
0
|
1
|
|
my $class = shift; |
104
|
0
|
|
|
|
|
|
my $self = {}; |
105
|
0
|
0
|
|
|
|
|
$self = shift if $_[0]; |
106
|
|
|
|
|
|
|
|
107
|
0
|
0
|
|
|
|
|
$self->{inner_prefix} = defined $self->{inner_prefix} ? $self->{inner_prefix} : ''; |
108
|
0
|
0
|
|
|
|
|
$self->{outer_prefix} = defined $self->{outer_prefix} ? $self->{outer_prefix} : '-'; |
109
|
0
|
0
|
|
|
|
|
$self->{inner_assign} = defined $self->{inner_assign} ? $self->{inner_assign} : '='; |
110
|
0
|
0
|
|
|
|
|
$self->{outer_assign} = defined $self->{outer_assign} ? $self->{outer_assign} : ' '; |
111
|
0
|
0
|
|
|
|
|
$self->{inner_separate} = defined $self->{inner_separate} ? $self->{inner_separate} : ','; |
112
|
0
|
0
|
|
|
|
|
$self->{outer_separate} = defined $self->{outer_separate} ? $self->{outer_separate} : ' '; |
113
|
|
|
|
|
|
|
|
114
|
0
|
|
|
|
|
|
bless $self,$class; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=back |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=head2 $og->generate($data) |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
Returns a string of your options. Supply your perl data structure as a hash ref. |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
my $data = { |
126
|
|
|
|
|
|
|
foo => 'bar', |
127
|
|
|
|
|
|
|
bar => 'baz', |
128
|
|
|
|
|
|
|
inner => [ 'this', 'that', { one => 'two'} ], |
129
|
|
|
|
|
|
|
a => b |
130
|
|
|
|
|
|
|
c => undef, |
131
|
|
|
|
|
|
|
}; |
132
|
|
|
|
|
|
|
print $og->generate($hash); |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=cut |
138
|
|
|
|
|
|
|
sub generate { |
139
|
0
|
|
|
0
|
1
|
|
my ($self,$hash) = @_; |
140
|
0
|
0
|
|
|
|
|
croak "Need hashref" unless ref $hash eq 'HASH'; |
141
|
|
|
|
|
|
|
|
142
|
0
|
|
|
|
|
|
my $build; |
143
|
|
|
|
|
|
|
my @outers; |
144
|
0
|
|
|
|
|
|
for my $key (keys %{ $hash }) { |
|
0
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
|
146
|
0
|
|
|
|
|
|
my $outer; |
147
|
0
|
0
|
|
|
|
|
if (ref $self->{outer_prefix} eq 'ARRAY') { |
148
|
|
|
|
|
|
|
|
149
|
0
|
0
|
|
|
|
|
my $outer_prefix = ($key =~ /^\w{1}$/) ? @{ $self->{outer_prefix}}[0] : @{ $self->{outer_prefix}}[1]; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
|
151
|
0
|
|
|
|
|
|
$outer .= $outer_prefix . $key; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
else { |
154
|
0
|
|
|
|
|
|
$outer .= $self->{outer_prefix} . $key; |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
|
157
|
0
|
|
|
|
|
|
my @inners; |
158
|
|
|
|
|
|
|
|
159
|
0
|
0
|
|
|
|
|
croak "Use array ref for inner options" if (ref $hash->{$key} eq 'HASH'); |
160
|
|
|
|
|
|
|
|
161
|
0
|
0
|
|
|
|
|
if (ref $hash->{$key} eq 'ARRAY') { |
162
|
|
|
|
|
|
|
|
163
|
0
|
|
|
|
|
|
for my $each (@{ $hash->{$key}} ) { |
|
0
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
|
165
|
0
|
0
|
|
|
|
|
if (ref $each eq 'HASH') { |
166
|
|
|
|
|
|
|
|
167
|
0
|
|
|
|
|
|
for my $inner (keys %{ $each }) { |
|
0
|
|
|
|
|
|
|
168
|
0
|
|
|
|
|
|
my $build .= $self->{inner_prefix} . $inner . $self->{inner_assign} . $each->{$inner}; |
169
|
0
|
|
|
|
|
|
push (@inners,$build); |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
else { |
173
|
0
|
|
|
|
|
|
push (@inners, $self->{inner_prefix} . $each); |
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
else { |
178
|
0
|
|
|
|
|
|
push (@inners,$hash->{$key}); |
179
|
|
|
|
|
|
|
} |
180
|
0
|
|
|
|
|
|
push(@outers,$outer . $self->{outer_assign} . join($self->{inner_separate}, @inners)); |
181
|
|
|
|
|
|
|
} |
182
|
0
|
|
|
|
|
|
my $out = join( $self->{outer_separate}, @outers); |
183
|
0
|
|
|
|
|
|
return $out; |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=head1 AUTHOR |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
Michael Kroher, C<< >> |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=head1 BUGS |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
Wrote this module for kvm-qemu generation stuff (hence the defaults). |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
198
|
|
|
|
|
|
|
the web interface at L. I will be notified, and then you'll |
199
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=head1 SUPPORT |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
perldoc Options::Generator |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
You can also look for information at: |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
=over 4 |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker (report bugs here) |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
L |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
L |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
=item * CPAN Ratings |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
L |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=item * Search CPAN |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
L |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
=back |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
Copyright 2011 Michael Kroher. |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
242
|
|
|
|
|
|
|
under the terms of either: the GNU General Public License as published |
243
|
|
|
|
|
|
|
by the Free Software Foundation; or the Artistic License. |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
See http://dev.perl.org/licenses/ for more information. |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
=cut |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
1; |
251
|
|
|
|
|
|
|
|