blib/lib/Options/Generator.pm

filter: Statementstmt 26.0% 13 / 50 Branchbran 0.0% 0 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 66.6% 4 / 6 Podpod 100% 2 / 2 totaltotal 22.6% 19 / 84 CC 17 SCAR 43.4
CC
17
Cov
17%
generate
110.0
new
72.0
BEGIN
1.0
CRAP 76.4
↑ Summary
1 package Options::Generator;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 2 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 0 ▶sub new {
Subroutine new: not called CC=8 SCAR=42.8
Pod documented
103 0 my $class = shift;
104 0 my $self = {};
105 0 ▶ $self = shift if $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] 0 0 0
106  
107 0 ▶ $self->{inner_prefix} = defined $self->{inner_prefix} ? $self->{inner_prefix} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'inner_prefix'} ? : 0 0 0
108 0 ▶ $self->{outer_prefix} = defined $self->{outer_prefix} ? $self->{outer_prefix} : '-';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'outer_prefix'} ? : 0 0 0
109 0 ▶ $self->{inner_assign} = defined $self->{inner_assign} ? $self->{inner_assign} : '=';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'inner_assign'} ? : 0 0 0
110 0 ▶ $self->{outer_assign} = defined $self->{outer_assign} ? $self->{outer_assign} : ' ';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'outer_assign'} ? : 0 0 0
111 0 ▶ $self->{inner_separate} = defined $self->{inner_separate} ? $self->{inner_separate} : ',';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'inner_separate'} ? : 0 0 0
112 0 ▶ $self->{outer_separate} = defined $self->{outer_separate} ? $self->{outer_separate} : ' ';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'outer_separate'} ? : 0 0 0
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 0 ▶sub generate {
Subroutine generate: not called CC=10 SCAR=47.0
Pod documented
139 0 my ($self,$hash) = @_;
140 0 ▶ croak "Need hashref" unless ref $hash eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hash eq "HASH" 0 0 0
141  
142 0 my $build;
143 my @outers;
144 0 for my $key (keys %{ $hash }) {
145  
146 0 my $outer;
147 0 ▶ if (ref $self->{outer_prefix} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'outer_prefix'} eq 'ARRAY') { } 0 0 0
148  
149 0 ▶ my $outer_prefix = ($key =~ /^\w{1}$/) ? @{ $self->{outer_prefix}}[0] : @{ $self->{outer_prefix}}[1];
Branch: 1 branch
BranchTrueFalseTotal
$key =~ /^\w{1}$/ ? : 0 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 ▶ croak "Use array ref for inner options" if (ref $hash->{$key} eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
if ref $hash->{$key} eq "HASH" 0 0 0
160  
161 0 ▶ if (ref $hash->{$key} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $hash->{$key} eq 'ARRAY') { } 0 0 0
162  
163 0 for my $each (@{ $hash->{$key}} ) {
164  
165 0 ▶ if (ref $each eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $each eq 'HASH') { } 0 0 0
166  
167 0 for my $inner (keys %{ $each }) {
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<< <infrared at cpan.org> >>
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<bug-commandline-generator at rt.cpan.org>, or through
198 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Options-Generator>. 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<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Options-Generator>
218  
219 =item * AnnoCPAN: Annotated CPAN documentation
220  
221 L<http://annocpan.org/dist/Options-Generator>
222  
223 =item * CPAN Ratings
224  
225 L<http://cpanratings.perl.org/d/Options-Generator>
226  
227 =item * Search CPAN
228  
229 L<http://search.cpan.org/dist/Options-Generator/>
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 2 1;
251  
↑ Summary