blib/lib/Easy/Options.pm untested

filter: Statementstmt 0.0% 0 / 270 Branchbran 0.0% 0 / 120 Conditioncond 0.0% 0 / 107 MC/DCmcdc 0.0% 0 / 60 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 575 CC 61 SCAR 65.6
CC
61
Cov
0%
usage
1260.0
easy_options
342.0
optargs_missing
72.0
CRAP 706.7
1 package Easy::Options;
2 use strict;
3  
4 use Easy::Log qw(:all);
5  
6 use Getopt::Long;
7  
8 use Exporter;
9 our ( %EXPORT_TAGS, @ISA, @EXPORT_OK, @EXPORT, $VERSION );
10 @ISA = qw( Exporter );
11  
12 $VERSION = '0.01_01';
13  
14 %EXPORT_TAGS = (
15 usage => [ qw( easy_options get_options optargs_missing usage) ],
16 );
17  
18 $EXPORT_TAGS{all} = [ map {@{$_}} values %EXPORT_TAGS ];
19 @EXPORT_OK = @{$EXPORT_TAGS{'all'}};
20 @EXPORT = ();
21  
22 use File::Spec;
23 my @pathinfo = (File::Spec->splitpath( File::Spec->rel2abs( $0 )));
24 $log->write({prefix=>undef},$sll, '@pathinfo: ', \@pathinfo );
25  
26  
27 sub get_options {
28 # this sub is for backwards compatibility with the version of this routine that formerly lived in Log::Easy (which is now re-named Easy::Log)
29 my $optargs_orig = shift;
30 my $optargs = { %$optargs_orig };
31 my $getoptconfig = ref $_[0] eq 'HASH' ? (shift) : {};
32 my $easyconfig = ref $_[0] eq 'HASH' ? (shift) : {};
33
34 return easy_options( %$easyconfig,
35 optargs => $optargs,
36 getoptconfig => $getoptconfig,
37 easyconfig => $easyconfig,
38 );
39 }
40  
41 sub easy_options {
42 my ( $self, %args ) = @_;
43 $log->write($dll, '%args: ', \%args );
44 $log->write($all, '@ARGV: ', \@ARGV );
45 my %options;
46
47 my $optargs_orig = $args{optargs} || \%args;
48 $log->write($dll, '$optargs_orig: ', $optargs_orig );
49 my $optargs = $log->clone( $optargs_orig ); # a deep copy of the original arguments to avoid messing with them if we need to refer back to the original values
50 $log->write($dll, '$optargs: ', $optargs );
51
52 # $options{options_meta}{canonical_opts} = [ map { $_ =~ /^([^|]+)/ ? $1 : ( die "problem with canonical option list: $_" ); } sort keys %$optargs ];
53 # $log->write($all, '$options{options_meta}{canonical_opts}: ', $options{options_meta}{canonical_opts} );
54 $options{options_meta}{canonical_opts} = { map { $_ =~ /^([^|]+)/ ? {($1, $_)} : ( die "problem with canonical option list: $_" ); } sort keys %$optargs } ;
55 $log->write($all, '$options{options_meta}{canonical_opts}: ', $options{options_meta}{canonical_opts} );
56
57 $options{options_meta}{match_string} = join('|', keys %{$options{options_meta}{canonical_opts}} );
58 #$options{options_meta}{match_string} = join('|', keys %$optargs);
59 $options{options_meta}{match_string} =~ s/\|.\|/\|/g;
60 $options{options_meta}{match_string} =~ s/\|+/\|/g;
61 $log->write($all, '$options{options_meta}{match_string}: ', $options{options_meta}{match_string} );
62
63
64 $options{options_meta}{match_regex} = qr/$options{options_meta}{match_string}/i;
65 $log->write($dl4, '$options{options_meta}{match_regex}: ', $options{options_meta}{match_regex} );
66
67 my $getoptconfig = $args{getoptconfig}; # settings affecting Getopt::Long
68
69 my $target = $args{target} || \%options;
70 my $default = $args{default} || $target;
71 my $easyconfig = $args{easyconfig} || $optargs_orig;
72
73 # give some easy usage/help options
74 if ( not $args{nohelp} or exists $args{help} and $args{help} ) {
75 $optargs->{help} ||= $optargs->{usage} ||= ['' , sub { usage( $optargs, \%options, $getoptconfig, $easyconfig ) }, 'print help message and exit'];
76 $optargs->{usage} ||= $optargs->{help} ||= ['' , sub { usage( $optargs, \%options, $getoptconfig, $easyconfig ) }, 'print help message and exit'];
77 }
78 my $no_usage = $easyconfig->{no_usage} || scalar (grep { /no_usage/ } @_) ? 1 : 0;
79 $log->write($sll, '$no_usage: ', $no_usage );
80 my $no_missing = $easyconfig->{no_missing} || scalar (grep { /no_missing/ } @_) ? 1 : 0;
81 $log->write($sll, '$no_missing: ', $no_missing );
82 my $p = new Getopt::Long::Parser config => [ map { ($getoptconfig->{$_} ? $_ : ($_ =~ /^no_/ ? $_ : "no_$_")); } keys %$getoptconfig ];
83 my %GetOptions = map { my $optname = $_;
84 $log->write($dll, '$optname: ', $optname );
85 my $progopt = $optargs->{$_}[0];
86 $log->write($dll, '$progopt: ', $progopt );
87 my $argspec = '';
88 my $optspec = '';
89 my $type;
90 if ( $progopt =~ /^([:=]+)([ifs][@%]?)$/ ) {
91 my $spec = $1;
92 $type = $2;
93 $argspec = substr($spec, 0 , 1);
94 $optspec = substr($spec, 1 , 1) || $argspec;
95 $argspec .= $type;
96 $optspec .= $type;
97 } else {
98 $argspec = $progopt;
99 $optspec = $progopt;
100 }
101 #("$_$optargs->{$_}[0]" => ref $optargs->{$_}[1] ? $optargs->{$_}[1] : \$optargs->{$_}[1])
102 $log->write($dll, '$optspec: ', $optspec );
103 $log->write($dll, '$argspec: ', $argspec );
104 my @opt = ( "$optname$optspec" => ref $optargs->{$_}[1] ? $optargs->{$_}[1] : \$optargs->{$_}[1] );
105 $log->write($dll, '@opt: ', \@opt );
106 @opt;
107 } keys %$optargs;
108
109 $log->write($dll, '%GetOptions: ', \%GetOptions);
110 #local $SIG{__WARN__} = sub { &failed_options(@_) };# may want to add some additional arguments to pass to failed_options
111 $log->write($dll, '@ARGV: ', \@ARGV );
112 my $opt = $p->getoptions( %GetOptions );
113 $log->write($dll, '%GetOptions: ', \%GetOptions);
114 $log->write($dll, '@ARGV: ', \@ARGV );
115 $log->write($dll, '$opt: ', $opt );
116 $log->write($dl7, '$optargs: ', $optargs );
117 $log->write($dl7, '$opt: ', $opt);
118 # check that all required options have been provided
119 my @missing = $no_missing ? () : optargs_missing( $optargs );
120 $log->write($sll, "\@missing: ($no_missing)", \@missing );
121 #return () if (scalar @missing and $no_usage);
122
123 foreach my $key ( keys %$optargs ) {
124 $log->write($dll, '$key: ', $key );
125 my $value = defined $optargs->{$key}[1] ? $optargs->{$key}[1] : $default->{$key};
126 $log->write($dll, '$value: ', $value );
127 my @aliases = ();# = ( $key );
128 if ( $key =~ /\|/ ) {
129 push @aliases, split /\|/, $key;
130 } else {
131 push @aliases, $key;
132 }
133 $log->write($dll, '@aliases: ', \@aliases );
134 foreach my $alias ( @aliases ) {
135 if ( ref $value eq 'SCALAR' ) {
136 $options{$alias} = $$value;
137 } elsif ( ref $value eq 'CODE' ) {
138 #$options{$alias} = $value;
139 } elsif ( ref $value eq 'ARRAY' ) {
140 my $final_value = [];
141 foreach my $item ( @$value ) {
142 if ( ref $item eq 'ARRAY' ) {
143 push @$final_value, @$item;
144 }
145 else {
146 push @$final_value, $item;
147 }
148 }
149 $options{$alias} = $final_value;
150 } else {
151 $options{$alias} = $value;
152 }
153 $target->{$alias} = $options{$alias};
154 }
155 }
156
157 # when this routine is separated into its own package (Getargs::Long ???), the %options should
158 # probably be a tied hash with a canonical set of key names and a 'hidden' set of aliases as
159 # specified by the $key of %optargs (eg my %o = ( 'foo|bar|baz' => [ '!', undef, 'some flag'] );
160 # would have a canonical name 'foo' (returned by keys %o) and aliases 'bar' and 'baz'
161
162 if ( $options{help} ) {
163 usage( $optargs, \%options, undef, undef, { brief_info => ' [OPTIONS] ["SQL STRING"|"SQL_FILENAME" [ ...] ] '} );
164 }
165
166 return usage( $optargs, \%options, $getoptconfig, $easyconfig ) if (scalar @missing and not $no_usage);
167 $log->write($dl7, '%options: ', \%options);
168 $log->write($lll, '%options: ', \%options);
169 return %options;
170 }
171  
172 sub failed_options {
173 $log->write({EXIT => 1, prefix => ''}, EXIT, @_);
174 }
175  
176 sub optargs_missing {
177 my $optargs = shift;
178 $log->write($dl7, '$optargs: ', $optargs );
179 my $options = shift || {};
180 $log->write($dl7, '$options: ', $options );
181 my @missing;
182 my %options;
183 my @aliases = ();
184 my %aliases = ();
185 foreach my $key ( keys %$optargs ) {
186 next if $key =~ /^(usage|help)$/;
187 $log->write($dl7, '$key: ', $key );
188 my @a;
189 my $value = $optargs->{$key}[1];
190 ref $value eq 'SCALAR' and $value = $$value;
191 $log->write($dl7, '$value: ', $value );
192 if ( $key =~ /\|/ ) {
193 push @a, split /\|/, $key;
194 } else {
195 push @a, $key;
196 }
197 push @aliases, @a;
198 $aliases{$key} = \@a;
199 foreach my $alias ( @a ) {
200 $log->write($dl7, '$alias: ', $alias );
201 $options{$alias} = defined $options->{$alias} ? $options->{$alias} : $value;
202 $log->write($dl7, qq'\$options{$alias}: ', $options{$alias} );
203 }
204 }
205 $log->write($dl7, '%options: ', \%options );
206 foreach my $opt (keys %$optargs ) {
207 $log->write($dl7, '$opt: ', $opt );
208 my $optspec = $optargs->{$opt}[0];
209 $log->write($dl7, '$optspec: ', $optspec );
210 next if not $optspec;
211 my $a = $aliases{$opt};
212 $log->write($dl7, '$a: ', $a );
213 my $alias = $a->[0];
214 $log->write($dl7, '$alias: ', $alias );
215 my $value = $options{$alias};
216 $log->write($dl7, '$value: ', $value );
217 my $optval = (ref $value eq 'ARRAY' ? join("", @$value) : $value );
218 $log->write($dl7, '$optval: ', $optval );
219 if ( $optspec =~ /^\=/ ) {
220 $log->write(D_SPEW, 'REQUIRED: ', $opt );
221 if ( not defined $optval or ($optval ne '0' and not $optval) ) {
222 $log->write(D_SPEW, 'REQUIRED AND MISSING: ', $opt );
223 push @missing, $opt;
224 }
225 }
226 }
227 $log->write($dl7, '%options: ', \%options );
228 $log->write($dll, '@missing: ', \@missing );
229 return @missing;
230 }
231 use Text::Wrap;
232 $Text::Wrap::columns = 70;
233 $Text::Wrap::huge = 'overflow';
234 my %already_decoded;
235 sub usage {
236 # I really need to clean up how this gets its arguments, prubably aught to have the @_ be HASHable { optargs => \%optargs, ... } instead of trying to rely on this ordering crap
237 my $optargs_orig = shift;
238 my $optargs = { %$optargs_orig };
239 $log->write($dll, '$optargs: ', $optargs );
240
241 my $options = ref $_[0] eq 'HASH' ? (shift) : {};
242 $log->write($dll, '$options: ', $options );
243 my $optconfig = ref $_[0] eq 'HASH' ? (shift) : {};
244 $log->write($dll, '$optconfig: ', $optconfig );
245 my $config = ref $_[0] eq 'HASH' ? (shift) : {};
246 $log->write($dll, '$config: ', $config );
247 # give some easy usage/help options
248 if ( not $config->{nohelp} or exists $config->{help} and $config->{help} ) {
249 $optargs->{help} ||= $optargs->{usage} ||= ['' , sub { usage( $optargs, $options, $optconfig, $config ) }, 'print help message and exit'];
250 $optargs->{usage} ||= $optargs->{help} ||= ['' , sub { usage( $optargs, $options, $optconfig, $config ) }, 'print help message and exit'];
251 }
252 # if ( not $config->{nohelp} or exists $config->{help} and $config->{help} ) {
253 # $optargs->{help} ||= $optargs->{usage} ||= ['' , sub { usage( $optargs ) }, 'print help message and exit'];
254 # $optargs->{usage} ||= $optargs->{help} ||= ['' , sub { usage( $optargs ) }, 'print help message and exit'];
255 # }
256 my $no_missing = scalar (grep { /no_missing/ } map { defined $_ ? $_ : (); } @_) ? 1 : 0;
257 my $usage_args = ref $_[0] eq 'HASH' ? (shift) : $_[0] ? $_[0] : {};
258 $log->write($dll, '$usage_args: ', $usage_args );
259 ref $usage_args eq 'HASH' or $usage_args = { $usage_args => (scalar @_ ? @_ : 1 )};
260 my %options = $options ? %$options : map { ($_ => $optargs->{$_}[1]) } keys %$optargs;
261 $log->write($dll, '$optargs: ', $optargs );
262 my @missing = optargs_missing( $optargs, $options );
263 $log->write($dll, '@missing: ', \@missing);
264 my @required = map { my $a = $optargs->{$_}[0];
265 $a =~ /^\=/ ? $_ : ();
266 } sort { $a cmp $b } keys %$optargs;
267
268 my @optional = map { my $a = $optargs->{$_}[0];
269 $a =~ /^\=/ ? () : $_;
270 } sort { $a cmp $b } keys %$optargs;
271
272 my $missing = scalar @missing ? "MISSING REQUIRED ARGUMENT(S): ( " . join(', ', map { ($optargs->{$_} ? "--$_" : $_); } sort @missing ) . " ) ... \n" : '';
273 my $base_name = $pathinfo[2];
274 $log->write($dll, '$base_name: ', $base_name );
275 my %type_spec = qw( f FLOAT
276 i INTEGER
277 s STRING
278 );
279 my $indent = ' | ';
280 my $sep = ( ' ' x ((length $indent) - 4) ) . ('-' x ( $Text::Wrap::columns - ((length $indent) + 2)));
281 $sep = ' ';
282 $usage_args->{brief_info} ||= $config->{brief_info} || '';
283 $log->write(CLEAN,(#'_' x ($Text::Wrap::columns + 3),
284 "\n",
285 ($config->{usage_message} ? $config->{usage_message} : () ),
286 "\n",
287 $no_missing ? () : ($missing,"\n"),
288 space("usage: $base_name $usage_args->{brief_info}", ), "\n",
289 $sep,#"\n ",
290 join("\n" . ' ', #(' ' x (length "usage: $base_name")),
291 map {
292 my $val = ( defined $options{$_}
293 ? $options{$_}
294 : $optargs->{$_}[1]
295 );
296 ref $val eq 'SCALAR' and $val = $$val;
297 ref $val eq 'ARRAY' and $val = join(', ', @$val);
298 ref $val eq 'HASH' and $val = join(', ', map { "$_ => $val->{$_}" } keys %$val);
299
300 my $label = $_;
301 $label =~ /\|/ and $label = '(' . $label . ')';
302 my $required = $optargs->{$_}[0] =~ /^\=/ ? 1 : 0;
303 #my $boolean = $optargs->{$_}[0] =~ /(^$)|(\+)/ ? 1 : 0;
304 $optargs->{$_}[0] =~ /([fis])/;
305 my $type = $1;
306 my $boolean = $type ? 0 : 1;
307 $type = $boolean ? 'BOOLEAN' : $type_spec{$type};
308 my $desc = $optargs->{$_}[2] || $_; #'NO DESCRIPTION PROVIDED';
309 my @desc = split ('\s+', $desc );
310 my $show_req = ($required ? "REQUIRED($type)" : "OPTIONAL($type)");# . "\n";
311 my @wrap = wrap( $indent, $indent, @desc);
312 if( scalar @wrap == 1 ) {
313 @wrap = split("\n", $wrap[0]);
314 }
315 $log->write($dll, '@wrap: ', \@wrap );
316 $desc = "\n" . join( "\n", map { space($_, $Text::Wrap::columns ) . ' |'; } @wrap ) . "\n" . (' ' x length $indent ) . ('-' x ($Text::Wrap::columns - length $indent)) . $sep;
317 if ( 'CODE' eq ref $val
318 and $_ !~ /(usage|help)/
319 and not $already_decoded{$val}++
320 ) {
321 $val = &$val( $_, $options{$_} );
322 }
323 if ( defined $val and ( $val or $val eq '0' ) ) {
324 $val = $boolean ? $val : "'$val'";
325 }
326 my $onoff = ($boolean and $val) ? '#ON#' : '#OFF#';
327 my @opt = ( $required
328 ? ( space($show_req, 25), ' ', ( '--', space($_, 25),($val ? ' ': '***'), space($val ? ($boolean ? $onoff : $val) : ($boolean ? $onoff : uc "<$_>***"), 15)) , ' ', $desc )
329 : ( space($show_req, 25), ' [ ', ( '--', space($_, 25), space($val ? ($boolean ? $onoff : $val) : ($boolean ? $onoff : uc "<$_>"), 15)) , ' ] ', $desc )
330 );
331 join('', @opt);
332 } #( sort
333 ( # sort here to make required options come first, and sort alphabetically in each sub-category: (required, not-required)
334 ( sort @required ),
335 ( sort @optional ),
336 )
337 #)
338 ),
339 "\n",
340 #'_' x ($Text::Wrap::columns + 3),
341 )
342 );
343 %already_decoded = ();
344 exists $config->{exit} or exit -1;
345 $config->{exit} and exit -1;
346 }
347  
348 END {
349 }
350  
351 1;
352 __END__