blib/lib/Getopt/Class.pm untested

filter: Statementstmt 0.0% 0 / 604 Branchbran 0.0% 0 / 320 Conditioncond 0.0% 0 / 326 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 70 Podpod 0.0% 0 / 40 totaltotal 0.0% 0 / 1360 CC 161 SCAR 64.7
CC
161
Cov
0%
init
2450.0
AUTOLOAD
552.0
postprocess
420.0
CRAP 643.9
1 ##----------------------------------------------------------------------------
2 ## Getopt::Long with Class - ~/lib/m
3 ## Version v1.1.5
4 ## Copyright(c) 2025 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2020/04/25
7 ## Modified 2026/07/04
8 ## All rights reserved
9 ##
10 ## This program is free software; you can redistribute it and/or modify it
11 ## under the same terms as Perl itself.
12 ##----------------------------------------------------------------------------
13 package Getopt::Class;
14 BEGIN
15 {
16 use strict;
17 use warnings;
18 use parent qw( Module::Generic );
19 use vars qw( $VERSION );
20 use Clone;
21 use DateTime::Lite;
22 use DateTime::Format::Lite;
23 use Getopt::Long;
24 use Module::Generic::Array;
25 use Module::Generic::File qw( file );
26 use Module::Generic::Scalar;
27 use Scalar::Util;
28 our $VERSION = 'v1.1.5';
29 };
30  
31 use strict;
32 use warnings;
33  
34 sub init
35 {
36 my $self = shift( @_ );
37 my $param = shift( @_ ) || return( $self->error( "No hash parameter was provided." ) );
38 return( $self->error( "Hash of parameters provided ($param) is not an hash reference." ) ) if( !$self->_is_hash( $param ) );
39 $self->SUPER::init( $param ) || return( $self->pass_error );
40 $self->{configured} = 0;
41 $self->{classes} = {};
42 $self->{missing} = [];
43 $self->{colour_open} = '<';
44 $self->{colour_close} = '>';
45 my $dict = $param->{dictionary} || return( $self->error( "No dictionary was provided to initiate Getopt::Long" ) );
46 return( $self->error( "Dictionary provided is not a hash reference." ) ) if( !$self->_is_hash( $dict ) );
47 $self->dictionary( $dict );
48
49 # Set the aliases hash reference used to contain each of the option aliases,e ach pointing to the same dictionary definition
50 $self->{aliases} = {};
51
52 # Tie'ing will make sure that values set for a key or its aliases are populated to other aliases
53 # Getopt::Long already does it, but this takes care of synchronising values for all aliases AFTER Getopt::Long has processed the options
54 # So that if the user change an option value using an alias:, e.g.:
55 # last_name => { type => 'string', alias => [qw( surname )] }
56 # last_name and surname would have the same value set thanks to Getopt::Long
57 # --last-name = 'Einstein';
58 # But if, after, the user does something like:
59 # $opts->{surname} = 'Doe';
60 # $opts->{last_name} would still be 'Einstein'
61 # Getopt::Class::Alias ensures the values for aliases and original key are the same seamlessly
62 # The way tie works means we must tie en empty hash, because we cannot tie an already populated hash sadly enough
63 my %options = ();
64 my $tie = tie( %options, 'Getopt::Class::Alias',
65 {
66 dict => $dict,
67 aliases => $self->{aliases},
68 # debug => $self->{debug}
69 }) || return( $self->error( "Unable to get a Getopt::Class::Alias tie object: ", Getopt::Class::Alias->error ) );
70
71 $self->{configure_options} = [qw( no_ignore_case no_auto_abbrev auto_version auto_help )];
72 my $opts = \%options;
73 my $params = [];
74  
75 foreach my $k ( sort( keys( %$dict ) ) )
76 {
77 my $k2_dash = $k;
78 $k2_dash =~ tr/_/-/;
79 my $k2_under = $k;
80 $k2_under =~ tr/-/_/;
81
82 my $def = $dict->{ $k };
83 next if( $def->{__no_value_assign} );
84  
85 # Do some pre-processing work for booleans
86 if( $def->{type} eq 'boolean' && !exists( $def->{mirror} ) )
87 {
88 my $mirror_opt;
89 # If this is a boolean, add their counterpart, if necessary
90 if( substr( $k, 0, 5 ) eq 'with_' &&
91 !exists( $dict->{ 'without_' . substr( $k, 5 ) } ) )
92 {
93 $mirror_opt = 'without_' . substr( $k, 5 );
94 }
95 elsif( substr( $k, 0, 8 ) eq 'without_' &&
96 !exists( $dict->{ 'with_' . substr( $k, 8 ) } ) )
97 {
98 $mirror_opt = 'with_' . substr( $k, 8 );
99 }
100 elsif( substr( $k, 0, 7 ) eq 'enable_' &&
101 !exists( $dict->{ 'disable_' . substr( $k, 7 ) } ) )
102 {
103 $mirror_opt = 'disable_' . substr( $k, 7 );
104 }
105 elsif( substr( $k, 0, 8 ) eq 'disable_' &&
106 !exists( $dict->{ 'enable_' . substr( $k, 8 ) } ) )
107 {
108 $mirror_opt = 'enable_' . substr( $k, 8 );
109 }
110
111 if( defined( $mirror_opt ) )
112 {
113 my $false = 0;
114 my $val = exists( $def->{default} )
115 # ? ( Scalar::Util::reftype( $def->{default} // '' ) eq 'SCALAR' || ref( $def->{default} // '' ) eq 'CODE' )
116 ? ( $self->_is_scalar( $def->{default} ) || $self->_is_code( $def->{default} ) || ref( $def->{default} ) )
117 ? $def->{default}
118 : \$def->{default}
119 : exists( $def->{code} )
120 ? $def->{code}
121 : \$false;
122 my $copy = Clone::clone( $def );
123 $dict->{ $mirror_opt } = $copy;
124 $def->{mirror} = { name => $mirror_opt, toggle => sub
125 {
126 my( $value ) = @_;
127 $opts->{ $mirror_opt } = int( !$value );
128 }};
129 $def->{mirror}->{default} = delete( $def->{default} ) if( exists( $def->{default} ) && defined( $def->{default} ) );
130 # A code is used for this boolean, so we create an anon sub that call this sub just like Getopt::Long would
131 if( ref( $val ) eq 'CODE' )
132 {
133 $copy->{mirror} = { name => $k, toggle => sub
134 {
135 my( $value ) = @_;
136 $val->( $k, int( !$value ) );
137 }};
138 }
139 # Otherwise, we create a sub that set the mirror value
140 else
141 {
142 $copy->{mirror} = { name => $k, toggle => sub
143 {
144 my( $value ) = @_;
145 $opts->{ $k } = int( !$value );
146 }};
147 }
148 $copy->{mirror}->{default} = int( !$def->{mirror}->{default} ) if( exists( $def->{mirror}->{default} ) );
149 # We remove it, because they would be assigned by Getopt::Long even if not triggered and this would bother us.
150 delete( $def->{default} );
151 delete( $copy->{default} );
152 $def->{default} = sub
153 {
154 my( $option, $value ) = @_;
155 return if( $def->{mirror}->{is_set} );
156 $def->{mirror}->{value} = $value;
157 $def->{mirror}->{is_set}++;
158 $def->{mirror}->{toggle}->( $value );
159 };
160 $copy->{default} = sub
161 {
162 my( $option, $value ) = @_;
163 return if( $copy->{mirror}->{is_set} );
164 $copy->{mirror}->{value} = $value;
165 $copy->{mirror}->{is_set}++;
166 $copy->{mirror}->{toggle}->( $value );
167 };
168 $def->{__no_value_assign} = 1;
169 }
170 }
171 }
172
173 # Build the options parameters
174 foreach my $k ( sort( keys( %$dict ) ) )
175 {
176 my $k2_dash = $k;
177 $k2_dash =~ tr/_/-/;
178 my $k2_under = $k;
179 $k2_under =~ tr/-/_/;
180
181 my $def = $dict->{ $k };
182
183 my $opt_name = [ $k2_under ];
184 # If the dictionary element is given with dash, e.g. some-thing, we replace it with some_thing, which is our standard
185 # and we set some-thing as an alias
186 if( CORE::index( $k, '-' ) != -1 && $k eq $k2_dash )
187 {
188 $dict->{ $k2_under } = CORE::delete( $dict->{ $k } );
189 $k = $k2_under;
190 }
191 # Add the dash option as an alias if it is not the same as the underscore one, such as when this is just one word, e.g. version
192 CORE::push( @$opt_name, $k2_dash ) if( $k2_dash ne $k2_under );
193  
194 if( !ref( $def->{alias} ) && CORE::length( $def->{alias} ) )
195 {
196 $def->{alias} = [$def->{alias}];
197 }
198 # Add the given aliases, if any
199 if( $self->_is_array( $def->{alias} ) )
200 {
201 push( @$opt_name, @{$def->{alias}} ) if( scalar( @{$def->{alias}} ) );
202 # push( @$opt_name, $k2_under ) if( !scalar( grep( /^$k2_under$/, @{$def->{alias}} ) ) );
203 }
204 # Now, also add the original key-something and key_something to the alias, so we can find them from one of the aliases
205 # When we do exec, we'll be able to find all the aliases
206 $def->{alias} = [] if( !CORE::exists( $def->{alias} ) );
207 CORE::push( @{$def->{alias}}, $k2_dash ) if( !scalar( grep( /^$k2_dash$/, @{$def->{alias}} ) ) );
208 CORE::push( @{$def->{alias}}, $k2_under ) if( !scalar( grep( /^$k2_under$/, @{$def->{alias}} ) ) );
209 $def->{alias} = Module::Generic::Array->new( $def->{alias} );
210
211 my $opt = join( '|', @$opt_name );
212 if( defined( $def->{default} ) && ( ref( $def->{default} ) || length( $def->{default} ) ) )
213 {
214 if( $def->{type} eq 'file' )
215 {
216 if( !ref( $def->{default} ) || $self->_can_overload( $def->{default} => '""' ) )
217 {
218 $opts->{ $k2_under } = "$def->{default}";
219 }
220 }
221 else
222 {
223 $opts->{ $k2_under } = $def->{default};
224 }
225 }
226 else
227 {
228 $opts->{ $k2_under } = '';
229 }
230 my $suff = '';
231 if( $def->{type} eq 'string' || $def->{type} eq 'scalar' )
232 {
233 $suff = '=s';
234 }
235 elsif( $def->{type} eq 'string-hash' )
236 {
237 $suff = '=s%';
238 }
239 elsif( $def->{type} eq 'array' ||
240 $def->{type} eq 'file-array' ||
241 $def->{type} eq 'uri-array' )
242 {
243 $suff = '=s@';
244 $opts->{ $k2_under } = [] unless( length( $def->{default} ) );
245 $def->{min} = 1 if( !exists( $def->{min} ) && !exists( $def->{max} ) );
246 }
247 elsif( $def->{type} eq 'boolean' )
248 {
249 $suff = '!';
250 if( exists( $def->{code} ) &&
251 ref( $def->{code} ) eq 'CODE' &&
252 # Will not override if a code ref is already assigned
253 ref( $opts->{ $k2_under } // '' ) ne 'CODE' )
254 {
255 $opts->{ $k2_under } = $def->{code};
256 }
257 }
258 elsif( $def->{type} eq 'hash' )
259 {
260 $suff = '=s%';
261 $opts->{ $k2_under } = {} unless( length( $def->{default} ) );
262 }
263 elsif( $def->{type} eq 'code' && ref( $def->{code} ) eq 'CODE' )
264 {
265 $opts->{ $k2_under } = $def->{code};
266 }
267 elsif( $def->{type} eq 'integer' )
268 {
269 $suff = '=i';
270 }
271 elsif( $def->{type} eq 'decimal' || $def->{type} eq 'float' || $def->{type} eq 'number' )
272 {
273 $suff .= '=f';
274 }
275 elsif( $def->{type} eq 'date' || $def->{type} eq 'datetime' )
276 {
277 $suff = '=s';
278 }
279 elsif( $def->{type} eq 'code' )
280 {
281 return( $self->error( "Type is code, but there is no property code for this option \"$k\"." ) ) if( !CORE::exists( $def->{code} ) );
282 return( $self->error( "Type is code, but the property code is not a code reference for this option \"$k\"." ) ) if( ref( $def->{code} ) ne 'CODE' );
283 $opts->{ $k2_under } = $def->{code};
284 }
285 elsif( $def->{type} eq 'file' )
286 {
287 $suff = '=s';
288 }
289 elsif( $def->{type} eq 'uri' )
290 {
291 $suff = '=s';
292 }
293
294 if( $def->{min} )
295 {
296 # If there is no max, it would be for example s{1,}
297 # 2nd formatter is %s because it could be blank. %d would translate to 0 when blank.
298 no warnings 'uninitialized';
299 $suff .= sprintf('{%d,%s}', @$def{ qw( min max ) } );
300 }
301
302 if( $def->{re} && ref( $def->{re} ) ne 'Regexp' )
303 {
304 return( $self->error( "Regular expression provided for property \"$k\" ($def->{re}) is not a proper regular expression. I was expecting something like qr// and of type 'Regexp'." ) );
305 }
306 push( @$params, $opt . $suff );
307 }
308 $self->options( $opts );
309 $self->parameters( $params );
310 $self->{getopt} = Getopt::Long::Parser->new;
311 return( $self );
312 }
313  
314 sub check_class_data
315 {
316 my $self = shift( @_ );
317 my $class = shift( @_ ) || return( $self->error( "No class was provided to return its definition" ) );
318 return( $self->error( "Class provided '$class' is not a string." ) ) if( ref( $class ) );
319 my $p = {};
320 $p = shift( @_ ) if( scalar( @_ ) && $self->_is_hash( $_[0] ) );
321 my $dict = $self->class( $class ) || return;
322 my $v = $self->get_class_values( $class ) || return;
323 my $errors =
324 {
325 missing => {},
326 regexp => {},
327 };
328 foreach my $f ( sort( keys( %$dict ) ) )
329 {
330 my $def = $dict->{ $f };
331 my $n = $def->{name} ? $def->{name} : $f;
332 $def->{error} ||= "does not match requirements";
333 if( !!$p->{required} && $def->{required} )
334 {
335 if( ( $def->{type} =~ /^(?:boolean|decimal|integer|string)/ && !length( $v->{ $f } ) ) ||
336 ( ( $def->{type} eq 'hash' || $def->{type} eq 'string-hash' ) && !scalar( keys( %{$v->{ $f }} ) ) ) ||
337 ( $def->{type} eq 'array' && !scalar( @{$v->{ $f }} ) ) )
338 {
339 $errors->{missing}->{ $f } = "$f ($n) is missing";
340 next;
341 }
342 }
343 if( $def->{re} )
344 {
345 if( $def->{type} eq 'string' && length( $v->{ $f } ) && $v->{ $f } !~ /$def->{re}/ )
346 {
347 $errors->{regexp}->{ $f } = "$f ($n) " . $def->{error};
348 }
349 elsif( $def->{type} eq 'array' )
350 {
351 my $sub_err = [];
352 foreach my $this ( @{$v->{ $f }} )
353 {
354 if( $this !~ /$def->{re}/ )
355 {
356 push( @$sub_err, $this );
357 }
358 }
359 $errors->{regexp}->{ $f } = join( ', ', @$sub_err ) . ' ' . $def->{error};
360 }
361 }
362 elsif( $def->{type} eq 'decimal' && $v->{ $f } !~ /^\d+(\.\d{1,12})?$/ )
363 {
364 $errors->{regexp}->{ $f } = "$f ($n) " . $def->{error};
365 }
366 }
367 return( $errors );
368 }
369  
370 sub class
371 {
372 my $self = shift( @_ );
373 my $class = shift( @_ ) || return( $self->error( "No class was provided to return its definition" ) );
374 return( $self->error( "Class provided '$class' is not a string." ) ) if( ref( $class ) );
375 my $classes = $self->classes;
376 return( $self->error( "I was expecting an hash reference for the classes dictionaries but got '$classes' instead." ) ) if( !ref( $classes ) );
377 return( $self->error( "No class \"$class\" was found." ) ) if( scalar( keys( %$classes ) ) && !exists( $classes->{ $class } ) );
378 my $dict = $self->dictionary;
379 return( $self->error( "No dictionary data could be found!" ) ) if( !scalar( keys( %$dict ) ) );
380 foreach my $k ( sort( keys( %$dict ) ) )
381 {
382 my $def = $dict->{ $k };
383 next if( !exists( $def->{class} ) );
384 my $class_names = $def->{class};
385 my $k2 = $k;
386 $k2 =~ tr/-/_/;
387 foreach my $class ( @$class_names )
388 {
389 # Create the class if it doe snot exists yet
390 $classes->{ $class } = {} if( !exists( $classes->{ $class } ) );
391 my $this = $classes->{ $class };
392 # Then add the property and it definition hash
393 $this->{ $k2 } = $def;
394 # If there are any alias, we add them too
395 if( $def->{alias} && scalar( @{$def->{alias}} ) )
396 {
397 foreach my $f ( @{$def->{alias}} )
398 {
399 my $f2 = $f;
400 $f2 =~ tr/-/_/;
401 $this->{ $f } = $this->{ $f2 } = $def;
402 }
403 }
404 }
405 }
406 return( $self->error( "No class \"$class\" was found." ) ) if( !exists( $classes->{ $class } ) );
407 return( $classes->{ $class } );
408 }
409  
410 sub classes { return( shift->_set_get_hash( 'classes', @_ ) ); }
411  
412 sub class_properties
413 {
414 my $self = shift( @_ );
415 my $class = shift( @_ );
416 return( $self->error( "No class was provided to list its properties." ) ) if( !length( $class ) );
417 my $fields = [];
418 my $ref = $self->class( $class );
419 my $props = [ sort( keys( %$ref ) ) ];
420 return( Module::Generic::Array->new( $props ) );
421 }
422  
423 sub configure
424 {
425 my $self = shift( @_ );
426 return( $self ) if( $self->{configured} );
427 my $conf = [];
428 $conf = shift( @_ ) if( ref( $_[0] ) );
429 $conf = $self->configure_options if( !scalar( @$conf ) );
430 my $getopt = $self->getopt || return( $self->error( "No Getopt::Long::Parser object found." ) );
431 local $@;
432 # try-catch
433 eval
434 {
435 $getopt->configure( @$conf );
436 $self->{configured} = 1;
437 };
438 if( $@ )
439 {
440 return( $self->error( "An error occurred while configuration Getlong::Opt: $@" ) );
441 }
442 return( $self );
443 }
444  
445 sub configure_errors { return( shift->_set_get_array_as_object( 'configure_errors', @_ ) ); }
446  
447 sub configure_options { return( shift->_set_get_array_as_object( 'configure_options', @_ ) ); }
448  
449 sub dictionary { return( shift->_set_get_hash( 'dictionary', @_ ) ); }
450  
451 sub exec
452 {
453 my $self = shift( @_ );
454 $self->configure || return;
455 my $errors = [];
456 my $missing = [];
457 my $dict = $self->dictionary;
458 return( $self->error( "The data returned by dictionary() is not an hash reference." ) ) if( !$self->_is_hash( $dict ) );
459 return( $self->error( "Somehow, the dictionary hash is empty!" ) ) if( !scalar( keys( %$dict ) ) );
460 my $opts = $self->options;
461 return( $self->error( "The data returned by options() is not an hash reference." ) ) if( !$self->_is_hash( $opts ) );
462 return( $self->error( "Somehow, the options hash is empty!" ) ) if( !scalar( keys( %$opts ) ) );
463 my $params = $self->parameters;
464 return( $self->error( "Data returned by parameters() is not an array reference" ) ) if( !$self->_is_array( $params ) );
465 return( $self->error( "Somehow, the parameters array is empty!" ) ) if( !scalar( @$params ) );
466 my $getopt = $self->getopt || return( $self->error( "No Getopt::Long object found." ) );
467 my $required = $self->required;
468 return( $self->error( "Data returned by required() is not an array reference" ) ) if( !$self->_is_array( $required ) );
469
470 my $tie = tied( %$opts ) || return( $self->error( "Unable to get the tie object for the options value hash." ) );
471
472 local $Getopt::Long::SIG{ '__DIE__' } = sub
473 {
474 push( @$errors, join( '', @_ ) );
475 };
476 local $Getopt::Long::SIG{ '__WARN__' } = sub
477 {
478 push( @$errors, join( '', @_ ) );
479 };
480 $self->configure_errors( $errors );
481
482 $tie->enable(1);
483 $getopt->getoptions( $opts, @$params ) || do
484 {
485 my $usage = $self->usage;
486 return( $usage->() ) if( ref( $usage ) eq 'CODE' );
487 return;
488 };
489
490 foreach my $key ( @$required )
491 {
492 if( exists( $opts->{ $key } ) &&
493 ( !defined( $opts->{ $key } ) ||
494 !length( $opts->{ $key } ) ||
495 $opts->{ $key } =~ /^[[:blank:]]*$/ ||
496 ( ref( $opts->{ $key } ) eq 'SCALAR' &&
497 ( !length( ${$opts->{ $key }} ) || ${$opts->{ $key }} =~ /^[[:blank:]]*$/ )
498 ) ||
499 (
500 ref( $opts->{ $key } ) eq 'ARRAY' &&
501 !scalar( @{$opts->{ $key }} )
502 )
503 )
504 )
505 {
506 push( @$missing, $key );
507 }
508 }
509 $self->missing( $missing );
510
511 # Make sure we can access each of the options dictionary definition not just from the original key, but also from any of it aliases
512 my $aliases = $self->{aliases};
513 foreach my $k ( keys( %$dict ) )
514 {
515 my $def = $dict->{ $k };
516 $aliases->{ $k } = $def;
517 foreach my $a ( @{$def->{alias}} )
518 {
519 $aliases->{ $a } = $def;
520 }
521 }
522 $tie->enable(1);
523
524 $self->postprocess;
525
526 # return( $opts );
527 # e return a Getopt::Class::Values object, so we can call the option values hash key as method:
528 # $object->metadata / $object->metadata( $some_hash );
529 # instead of:
530 # $object->{metadata}
531 # return( $opts );
532 my $o = Getopt::Class::Values->new({
533 data => $opts,
534 dict => $dict,
535 aliases => $aliases,
536 debug => $self->{debug},
537 }) || return( $self->pass_error( Getopt::Class::Values->error ) );
538 return( $o );
539 }
540  
541 sub get_class_values
542 {
543 my $self = shift( @_ );
544 my $class = shift( @_ ) || return( $self->error( "No class was provided to return its definition" ) );
545 return( $self->error( "Class provided '$class' is not a string." ) ) if( ref( $class ) );
546 my $this_dict = $self->class( $class ) || return;
547 my $opts = $self->options;
548 return( $self->error( "The data returned by options() is not an hash reference." ) ) if( !$self->_is_hash( $opts ) );
549 return( $self->error( "Somehow, the options hash is empty!" ) ) if( !scalar( keys( %$opts ) ) );
550 my $v = {};
551 $v = shift( @_ ) if( scalar( @_ ) && $self->_is_hash( $_[0] ) );
552 foreach my $f ( sort( keys( %$this_dict ) ) )
553 {
554 my $ref = lc( Scalar::Util::reftype( $opts->{ $f } ) // '' );
555 if( $ref eq 'hash' )
556 {
557 $v->{ $f } = $opts->{ $f } if( scalar( keys( %{$opts->{ $f }} ) ) > 0 );
558 }
559 elsif( $ref eq 'array' )
560 {
561 $v->{ $f } = $opts->{ $f } if( scalar( @{$opts->{ $f }} ) > 0 );
562 }
563 elsif( !length( $ref ) )
564 {
565 $v->{ $f } = $opts->{ $f } if( length( $opts->{ $f } ) );
566 }
567 }
568 return( $v );
569 }
570  
571 sub getopt { return( shift->_set_get_object( 'getopt', 'Getopt::Long::Parser', @_ ) ); }
572  
573 sub missing { return( shift->_set_get_array_as_object( 'missing', @_ ) ); }
574  
575 sub options { return( shift->_set_get_hash( 'options', @_ ) ); }
576  
577 sub parameters { return( shift->_set_get_array_as_object( 'parameters', @_ ) ); }
578  
579 sub postprocess
580 {
581 my $self = shift( @_ );
582 my $dict = $self->dictionary;
583 my $opts = $self->options;
584 foreach my $k ( sort( keys( %$dict ) ) )
585 {
586 my $def = $dict->{ $k };
587 next if( !length( $opts->{ $k } ) && !$def->{default} );
588 return( $self->error( "Dictionary is malformed with entry $k value not being an hash reference." ) ) if( ref( $def ) ne 'HASH' );
589
590 if( ( $def->{type} eq 'date' || $def->{type} eq 'datetime' ) && length( $opts->{ $k } ) )
591 {
592 my $dt = $self->_parse_timestamp( $opts->{ $k } );
593 return( $self->pass_error ) if( !defined( $dt ) );
594 $opts->{ $k } = $dt if( $dt );
595 }
596 elsif( $def->{type} eq 'array' )
597 {
598 $opts->{ $k } = Module::Generic::Array->new( $opts->{ $k } );
599 }
600 elsif( $def->{type} eq 'hash' ||
601 $def->{type} eq 'string-hash' )
602 {
603 $opts->{ $k } = $self->_set_get_hash_as_object( $k, $opts->{ $k } );
604 }
605 elsif( $def->{type} eq 'boolean' )
606 {
607 if( exists( $def->{mirror} ) &&
608 exists( $def->{mirror}->{value} ) )
609 {
610 $opts->{ $k } = $def->{mirror}->{value};
611 }
612 $opts->{ $k } = ( $opts->{ $k } ? $self->true : $self->false );
613 }
614 elsif( $def->{type} eq 'string' || $def->{type} eq 'scalar' )
615 {
616 $opts->{ $k } = Module::Generic::Scalar->new( $opts->{ $k } );
617 }
618 elsif( $def->{type} eq 'integer' || $def->{type} eq 'number' || $def->{type} eq 'float' || $def->{type} eq 'decimal' || $def->{decimal} )
619 {
620 # Even though this is a number, this was set as a scalar reference, so we need to dereference it
621 if( $self->_is_scalar( $opts->{ $k } ) )
622 {
623 $opts->{ $k } = Module::Generic::Scalar->new( $opts->{ $k } );
624 }
625 else
626 {
627 $opts->{ $k } = $self->_set_get_number( $k, $opts->{ $k } );
628 }
629 }
630 elsif( $def->{type} eq 'file' && length( $opts->{ $k } // '' ) )
631 {
632 $opts->{ $k } = file( $opts->{ $k } );
633 }
634 elsif( $def->{type} eq 'file-array' )
635 {
636 my $arr = Module::Generic::Array->new;
637 foreach( @{$opts->{ $k }} )
638 {
639 push( @$arr, file( $_ ) );
640 }
641 $opts->{ $k } = $arr;
642 }
643 elsif( $def->{type} eq 'uri' )
644 {
645 my $uri_class = exists( $def->{package} ) ? $def->{package} : 'URI';
646 $opts->{ $k } = $uri_class->new( $opts->{ $k } );
647 }
648 elsif( $def->{type} eq 'uri-array' )
649 {
650 my $uri_class = exists( $def->{package} ) ? $def->{package} : 'URI';
651 my $arr = Module::Generic::Array->new;
652 foreach( @{$opts->{ $k }} )
653 {
654 push( @$arr, $uri_class->new( $_ ) );
655 }
656 $opts->{ $k } = $arr;
657 }
658 }
659 return( $self );
660 }
661  
662 sub required { return( shift->_set_get_array_as_object( 'required', @_ ) ); }
663  
664 sub usage { return( shift->_set_get_code( 'usage', @_ ) ); }
665  
666 # NOTE: Getopt::Class::Values package
667 package Getopt::Class::Values;
668 BEGIN
669 {
670 use strict;
671 use warnings;
672 use parent qw( Module::Generic );
673 };
674  
675 use strict;
676 use warnings;
677  
678 sub new
679 {
680 my $that = shift( @_ );
681 my %hash = ();
682 my $obj = tie( %hash, 'Getopt::Class::Repository' );
683 my $self = bless( \%hash => ( ref( $that ) || $that ) )->init( @_ );
684 $obj->enable( 1 );
685 return( $self );
686 }
687  
688 sub debug { return( shift->_set_get_number( 'debug', @_ ) ); }
689  
690 sub init
691 {
692 my $self = shift( @_ );
693 my $class = ref( $self ) || $self;
694 $self->{data} = {};
695 $self->{dict} = {};
696 $self->{aliases} = {};
697 # Can only set properties that exist
698 $self->{_init_strict} = 1;
699 $self->SUPER::init( @_ ) || return( $self->pass_error( $self->error ) );
700 return( $self->error( "No dictionary as provided." ) ) if( !$self->{dict} );
701 return( $self->error( "No dictionary as provided." ) ) if( !$self->{aliases} );
702 return( $self->error( "Dictionary provided (", ( $self->{dict} // 'undef' ), ") is not an hash reference." ) ) if( !$self->_is_hash( $self->{dict} ) );
703 return( $self->error( "Aliases provided is not an hash reference." ) ) if( !$self->_is_hash( $self->{aliases} ) );
704 scalar( keys( %{$self->{dict}} ) ) || return( $self->error( "No dictionary data was provided." ) );
705 return( $self->error( "Data provided is not an hash reference." ) ) if( !$self->_is_hash( $self->{data} ) );
706 my $call_offset = 0;
707 while( my @call_data = caller( $call_offset ) )
708 {
709 unless( $call_offset > 0 && $call_data[0] ne $class && (caller($call_offset-1))[0] eq $class )
710 {
711 $call_offset++;
712 next;
713 }
714 last if( $call_data[9] || ( $call_offset > 0 && (caller($call_offset-1))[0] ne $class ) );
715 $call_offset++;
716 }
717 my $bitmask = ( caller( $call_offset ) )[9];
718 my $offset = $warnings::Offsets{uninitialized};
719 my $should_display_warning = vec( $bitmask, $offset, 1 );
720 $self->{warnings} = $should_display_warning;
721 return( $self );
722 }
723  
724 sub verbose { return( shift->_set_get_number( 'verbose', @_ ) ); }
725  
726 # NOTE: AUTOLOAD
727 AUTOLOAD
728 {
729 my( $method ) = our $AUTOLOAD =~ /([^:]+)$/;
730 no overloading;
731 my $self = shift( @_ );
732 my $class = ref( $self ) || $self;
733 # Options dictionary
734 my $dict = $self->{dict};
735 # Values provided on command line
736 my $data = $self->{data};
737 # printf( STDERR "AUTOLOAD: \$data has %d items and property '$method' has value '%s'\n", scalar( keys( %$self ) ), $self->{ $method } );
738 # return if( !CORE::exists( $data->{ $method } ) );
739 return if( !CORE::exists( $self->{ $method } ) );
740 my $f = $method;
741 # Dictionary definition for this particular option field
742 my $def = $dict->{ $f };
743 if( !exists( $def->{type} ) ||
744 !defined( $def->{type} ) )
745 {
746 CORE::warn( "Property \"${f}\" has no defined type. Using scalar.\n" ) if( $self->{warnings} );
747 return( $self->_set_get_scalar( $f, @_ ) );
748 }
749 elsif( $def->{type} eq 'boolean' || ( $self->_is_object( $self->{ $f } ) && $self->{ $f }->isa( 'Module::Generic::Boolean' ) ) )
750 {
751 return( $self->_set_get_boolean( $f, @_ ) );
752 }
753 elsif( $def->{type} eq 'string' ||
754 $def->{type} eq 'scalar' ||
755 Scalar::Util::reftype( $self->{ $f } ) eq 'SCALAR' )
756 {
757 return( $self->_set_get_scalar_as_object( $f, @_ ) );
758 }
759 elsif( $def->{type} eq 'integer' ||
760 $def->{type} eq 'number' ||
761 $def->{type} eq 'numeric' ||
762 $def->{type} eq 'float' ||
763 $def->{type} eq 'decimal' )
764 {
765 return( $self->_set_get_number( $f, @_ ) );
766 }
767 elsif( $def->{type} eq 'date' ||
768 $def->{type} eq 'datetime' )
769 {
770 return( $self->_set_get_datetime( $f, @_ ) );
771 }
772 elsif( $def->{type} eq 'array' )
773 {
774 return( $self->_set_get_array_as_object( $f, @_ ) );
775 }
776 elsif( $def->{type} eq 'hash' ||
777 $def->{type} eq 'string-hash' )
778 {
779 return( $self->_set_get_hash_as_object( $f, @_ ) );
780 }
781 elsif( $def->{type} eq 'code' )
782 {
783 return( $self->_set_get_code( $f, @_ ) );
784 }
785 elsif( $def->{type} eq 'file' )
786 {
787 return( $self->_set_get_file( $f, @_ ) );
788 }
789 elsif( $def->{type} eq 'file-array' )
790 {
791 if( @_ )
792 {
793 my $arr = Module::Generic::Array->new;
794 foreach( @_ )
795 {
796 push( @$arr, file( $_ ) );
797 }
798 $self->{ $f } = $arr;
799 }
800 return( $self->_set_get_array_as_object( $f ) );
801 }
802 elsif( $def->{type} eq 'uri' )
803 {
804 my $uri_class = exists( $def->{package} ) ? $def->{package} : 'URI';
805 return( $self->_set_get_uri( { field => $f, class => $uri_class }, @_ ) );
806 }
807 elsif( $def->{type} eq 'uri-array' )
808 {
809 my $uri_class = exists( $def->{package} ) ? $def->{package} : 'URI';
810 if( @_ )
811 {
812 my $arr = Module::Generic::Array->new;
813 foreach( @_ )
814 {
815 push( @$arr, $uri_class->new( $_ ) );
816 }
817 $self->{ $f } = $arr;
818 }
819 return( $self->_set_get_uri( { field => $f, class => $uri_class } ) );
820 }
821 elsif( $def->{type} eq 'uri' )
822 {
823 return( $self->_set_get_uri( $f, @_ ) );
824 }
825 elsif( $def->{type} eq 'uri-array' )
826 {
827 if( @_ )
828 {
829 my $arr = Module::Generic::Array->new;
830 foreach( @_ )
831 {
832 push( @$arr, file( $_ ) );
833 }
834 $self->{ $f } = $arr;
835 }
836 return( $self->_set_get_array_as_object( $f ) );
837 }
838 else
839 {
840 CORE::warn( "I do not know what to do with this property \"$f\" type \"$def->{type}\". Using scalar.\n" ) if( $self->{warnings} );
841 return( $self->_set_get_scalar( $f, @_ ) );
842 }
843 };
844  
845 # NOTE: Getopt::Class::Repository package
846 package Getopt::Class::Repository;
847 BEGIN
848 {
849 use strict;
850 use warnings;
851 use Scalar::Util;
852 use constant VALUES_CLASS => 'Getopt::Class::Value';
853 };
854  
855 # tie( %self, 'Getopt::Class::Repository' );
856 # Used by Getopt::Class::Values to ensure that whether the data are accessed as methods or as hash keys,
857 # in either way it returns the option data
858 # Actually option data are stored in the Getopt::Class::Values object data property
859 sub TIEHASH
860 {
861 my $self = shift( @_ );
862 my $class = ref( $self ) || $self;
863 return( bless( { data => {} } => $class ) );
864 }
865  
866 sub CLEAR
867 {
868 my $self = shift( @_ );
869 my $data = $self->{data};
870 my $caller = caller;
871 %$data = ();
872 }
873  
874 sub DELETE
875 {
876 my $self = shift( @_ );
877 my $data = $self->{data};
878 my $key = shift( @_ );
879 if( caller eq VALUES_CLASS || !$self->{enable} )
880 {
881 CORE::delete( $self->{ $key } );
882 }
883 else
884 {
885 CORE::delete( $data->{ $key } );
886 }
887 }
888  
889 sub EXISTS
890 {
891 my $self = shift( @_ );
892 my $data = $self->{data};
893 my $key = shift( @_ );
894 if( caller eq VALUES_CLASS || !$self->{enable} )
895 {
896 CORE::exists( $self->{ $key } );
897 }
898 else
899 {
900 CORE::exists( $data->{ $key } );
901 }
902 }
903  
904 sub FETCH
905 {
906 my $self = shift( @_ );
907 my $data = $self->{data};
908 my $key = shift( @_ );
909 my $caller = caller;
910 # print( STDERR "FETCH($caller)[enable=$self->{enable}] <- '$key''\n" );
911 if( caller eq VALUES_CLASS || !$self->{enable} )
912 {
913 # print( STDERR "FETCH($caller)[enable=$self->{enable}] <- '$key' <- '$self->{$key}'\n" );
914 return( $self->{ $key } )
915 }
916 else
917 {
918 # print( STDERR "FETCH($caller)[enable=$self->{enable}] <- '$key' <- '$self->{$key}'\n" );
919 return( $data->{ $key } );
920 }
921 }
922  
923 sub FIRSTKEY
924 {
925 my $self = shift( @_ );
926 my $data = $self->{data};
927 my @keys = ();
928 if( caller eq VALUES_CLASS || !$self->{enable} )
929 {
930 @keys = keys( %$self );
931 }
932 else
933 {
934 @keys = keys( %$data );
935 }
936 $self->{ITERATOR} = \@keys;
937 return( shift( @keys ) );
938 }
939  
940 sub NEXTKEY
941 {
942 my $self = shift( @_ );
943 my $data = $self->{data};
944 my $keys = ref( $self->{ITERATOR} ) ? $self->{ITERATOR} : [];
945 return( shift( @$keys ) );
946 }
947  
948 sub SCALAR
949 {
950 my $self = shift( @_ );
951 my $data = $self->{data};
952 if( caller eq VALUES_CLASS || !$self->{enable} )
953 {
954 return( scalar( keys( %$self ) ) );
955 }
956 else
957 {
958 return( scalar( keys( %$data ) ) );
959 }
960 }
961  
962 sub STORE
963 {
964 my $self = shift( @_ );
965 my $data = $self->{data};
966 my( $key, $val ) = @_;
967 # print( STDERR "STORE($caller)[enable=$self->{enable}] -> '$key'\n" );
968 if( caller eq VALUES_CLASS || !$self->{enable} )
969 {
970 # print( STDERR "STORE($caller)[enable=$self->{enable}] -> '$key' -> '$val'\n" );
971 $self->{ $key } = $val;
972 }
973 else
974 {
975 # print( STDERR "STORE($caller)[enable=$self->{enable}] -> '$key' -> '$val'\n" );
976 $data->{ $key } = $val;
977 }
978 }
979  
980 sub enable
981 {
982 my $self = shift( @_ );
983 if( @_ )
984 {
985 $self->{enable} = shift( @_ );
986 }
987 return( $self->{enable} );
988 }
989  
990 # NOTE: Getopt::Class::Alias package
991 # This is an alternative to perl feature of refealiasing
992 # https://metacpan.org/pod/perlref#Assigning-to-References
993 package Getopt::Class::Alias;
994 BEGIN
995 {
996 use strict;
997 use warnings;
998 use parent -norequire, qw( Getopt::Class::Repository Module::Generic );
999 use Scalar::Util;
1000 };
1001  
1002 # tie( %$opts, 'Getopt::Class::Alias', $dictionary );
1003 sub TIEHASH
1004 {
1005 # $this is actually the HASH tied
1006 my $this = shift( @_ );
1007 my $class = ref( $this ) || $this;
1008 # Valid options are:
1009 # dict: options dictionary
1010 # debug
1011 my $opts = {};
1012 $opts = shift( @_ ) if( @_ );
1013 # print( STDERR __PACKAGE__ . "::TIEHASH() called with following arguments: '", join( ', ', @_ ), "'.\n" );
1014 my $call_offset = 0;
1015 while( my @call_data = caller( $call_offset ) )
1016 {
1017 # printf( STDERR "[$call_offset] In file $call_data[1] at line $call_data[2] from subroutine %s has bitmask $call_data[9]\n", (caller($call_offset+1))[3] );
1018 unless( $call_offset > 0 && $call_data[0] ne $class && (caller($call_offset-1))[0] eq $class )
1019 {
1020 # print( STDERR "Skipping package $call_data[0]\n" );
1021 $call_offset++;
1022 next;
1023 }
1024 last if( $call_data[9] || ( $call_offset > 0 && (caller($call_offset-1))[0] ne $class ) );
1025 $call_offset++;
1026 }
1027 # print( STDERR "Using offset $call_offset with bitmask ", ( caller( $call_offset ) )[9], "\n" );
1028 my $bitmask = ( caller( $call_offset - 1 ) )[9];
1029 my $offset = $warnings::Offsets{uninitialized};
1030 # print( STDERR "Caller (2)'s bitmask is '$bitmask', warnings offset is '$offset' and vector is '", vec( $bitmask, $offset, 1 ), "'.\n" );
1031 my $should_display_warning = vec( ( $bitmask // 0 ), $offset, 1 );
1032
1033 my $dict = $opts->{dict} || return( __PACKAGE__->error( "No dictionary was provided to Getopt::Class:Alias" ) );
1034 if( Scalar::Util::reftype( $dict ) ne 'HASH' )
1035 {
1036 #warn( "Dictionary provided is not an hash reference.\n" ) if( $should_display_warning );
1037 #return;
1038 return( __PACKAGE__->error({ message => "Dictionary provided is not an hash reference.", no_return_null_object => 1 }) );
1039 }
1040 elsif( !scalar( keys( %$dict ) ) )
1041 {
1042 #warn( "The dictionary hash reference provided is empty.\n" ) if( $should_display_warning );
1043 #return;
1044 return( __PACKAGE__->error( "The dictionary hash reference provided is empty." ) );
1045 }
1046
1047 my $aliases = $opts->{aliases} || do
1048 {
1049 #warn( "No aliases map was provided to Getopt::Class:Alias\n" ) if( $should_display_warning );
1050 #return;
1051 return( __PACKAGE__->error( "No aliases map was provided to Getopt::Class:Alias" ) );
1052 };
1053 if( Scalar::Util::reftype( $aliases ) ne 'HASH' )
1054 {
1055 #warn( "Aliases map provided is not an hash reference.\n" ) if( $should_display_warning );
1056 #return;
1057 return( __PACKAGE__->error( "Aliases map provided is not an hash reference." ) );
1058 }
1059 my $hash =
1060 {
1061 data => {},
1062 dict => $dict,
1063 aliases => $aliases,
1064 warnings => $should_display_warning,
1065 debug => ( $opts->{debug} || 0 ),
1066 # _data_repo => 'data',
1067 colour_open => '<',
1068 colour_close => '>',
1069 };
1070 return( bless( $hash => $class ) );
1071 }
1072  
1073 sub FETCH
1074 {
1075 my $self = shift( @_ );
1076 my $data = $self->{data};
1077 # my $dict = $self->{dict};
1078 my $key = shift( @_ );
1079 # my $def = $dict->{ $key };
1080 return( $data->{ $key } );
1081 }
1082  
1083 sub STORE
1084 {
1085 my $self = shift( @_ );
1086 my $class = ref( $self );
1087 my $data = $self->{data};
1088 # Aliases contains both the original dictionary key and all its aliases
1089 my $aliases = $self->{aliases};
1090 my( $pack, $file, $line ) = caller;
1091 my( $key, $val ) = @_;
1092 no warnings 'uninitialized';
1093 my $dict = $self->{dict};
1094 my $enabled = $self->{enable};
1095 my $fallback = sub
1096 {
1097 my( $k, $v ) = @_;
1098 $data->{ $k } = $v;
1099 };
1100 if( $enabled && CORE::exists( $aliases->{ $key } ) )
1101 {
1102 my $def = $aliases->{ $key } || do
1103 {
1104 CORE::warn( "No dictionary definition found for \"$key\".\n" ) if( $self->{warnings} );
1105 return( $fallback->( $key, $val ) );
1106 };
1107 if( !$self->_is_array( $def->{alias} ) )
1108 {
1109 CORE::warn( "I was expecting an array reference for this alias, but instead got '$def->{alias}'.\n" ) if( $self->{warnings} );
1110 return( $fallback->( $key, $val ) );
1111 }
1112 my $alias = $def->{alias} || do
1113 {
1114 CORE::warn( "No alias property found. This should not happen.\n" ) if( $self->{warnings} );
1115 return( $fallback->( $key, $val ) );
1116 };
1117 if( Scalar::Util::reftype( $alias ) ne 'ARRAY' )
1118 {
1119 CORE::warn( "Alias property is not an array reference. This should not happen.\n" ) if( $self->{warnings} );
1120 return( $fallback->( $key, $val ) );
1121 }
1122 $data->{ $key } = $val;
1123 foreach my $a ( @$alias )
1124 {
1125 next if( $a eq $key );
1126 # We do not set the value, if for some reason, the user would have removed this key
1127 # $data->{ $a } = $val if( CORE::exists( $data->{ $a } ) );
1128 $data->{ $a } = $val;
1129 }
1130 }
1131 else
1132 {
1133 $data->{ $key } = $val;
1134 }
1135 }
1136  
1137 1;
1138 # NOTE: POD
1139 __END__