Paranoid-2.07/0000755000175000001440000000000013455754576012762 5ustar acorlissusersParanoid-2.07/lib/0000755000175000001440000000000013455754576013530 5ustar acorlissusersParanoid-2.07/lib/Paranoid/0000755000175000001440000000000013455754576015265 5ustar acorlissusersParanoid-2.07/lib/Paranoid/Data.pm0000644000175000001440000002371513424465247016472 0ustar acorlissusers# Paranoid::Data -- Misc. Data Manipulation Functions # # (c) 2005 - 2017, Arthur Corliss # # $Id: lib/Paranoid/Data.pm, 2.07 2019/01/30 18:25:27 acorliss Exp $ # # This software is licensed under the same terms as Perl, itself. # Please see http://dev.perl.org/licenses/ for more information. # ##################################################################### ##################################################################### # # Environment definitions # ##################################################################### package Paranoid::Data; use 5.008; use strict; use warnings; use vars qw($VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS); use base qw(Exporter); use Paranoid; use Paranoid::Debug qw(:all); use Carp; ($VERSION) = ( q$Revision: 2.07 $ =~ /(\d+(?:\.\d+)+)/sm ); @EXPORT = qw(deepCopy deepCmp); @EXPORT_OK = @EXPORT; %EXPORT_TAGS = ( all => [@EXPORT_OK], ); ##################################################################### # # Module code follows # ##################################################################### sub deepCopy (\[$@%]\[$@%]) { # Purpose: Attempts to safely copy an arbitrarily deep data # structure from the source to the target # Returns: True or False # Usage: $rv = deepCopy($source, $target); # Usage: $rv = deepCopy(@source, @target); # Usage: $rv = deepCopy(%source, %target); my $source = shift; my $target = shift; my $rv = 1; my $counter = 0; my $sref = defined $source ? ref $source : 'undef'; my $tref = defined $target ? ref $target : 'undef'; my ( @refs, $recurseSub ); croak 'source and target must be identical data types' unless ref $sref eq ref $tref; pdebug( 'entering w/(%s)(%s)', PDLEVEL1, $source, $target ); pIn(); $recurseSub = sub { my $s = shift; my $t = shift; my $type = ref $s; my $irv = 1; my ( $key, $value ); # We'll grep the @refs list to make sure there's no # circular references going on if ( grep { $_ eq $s } @refs ) { Paranoid::ERROR = pdebug( 'Found a circular reference in data structure: ' . '(%s) %s', PDLEVEL1, $s, @refs ); return 0; } # Push the reference onto the list push @refs, $s; # Copy data over if ( $type eq 'ARRAY' ) { # Copy over array elements foreach my $element (@$s) { $type = ref $element; $counter++; if ( $type eq 'ARRAY' or $type eq 'HASH' ) { # Copy over sub arrays or hashes push @$t, $type eq 'ARRAY' ? [] : {}; return 0 unless &$recurseSub( $element, $$t[-1] ); } else { # Copy over everything else as-is push @$t, $element; } } } elsif ( $type eq 'HASH' ) { while ( ( $key, $value ) = each %$s ) { $type = ref $value; $counter++; if ( $type eq 'ARRAY' or $type eq 'HASH' ) { # Copy over sub arrays or hashes $$t{$key} = $type eq 'ARRAY' ? [] : {}; return 0 unless &$recurseSub( $value, $$t{$key} ); } else { # Copy over everything else as-is $$t{$key} = $value; } } } # We're done, so let's remove the reference we were working on pop @refs; return 1; }; # Start the copy if ( $sref eq 'ARRAY' or $sref eq 'HASH' ) { # Copy over arrays & hashes if ( $sref eq 'ARRAY' ) { @$target = (); } else { %$target = (); } $rv = &$recurseSub( $source, $target ); } else { # Copy over everything else directly $$target = $$source; $counter++; } $rv = $counter if $rv; pOut(); pdebug( 'leaving w/rv: %s', PDLEVEL1, $rv ); return $rv; } sub _cmpArray (\@\@) { # Purpose: Compares arrays, returns true if identical # Returns: Boolean # Usage: $rv = _cmpArray(@array1, @array2); my $ref1 = shift; my $ref2 = shift; my $rv = 1; my $i = 0; my ( $n, $d1, $d2, $t1, $t2 ); pdebug( 'entering w/%s %s', PDLEVEL2, $ref1, $ref2 ); pIn(); $rv = scalar @$ref1 == scalar @$ref2; $n = scalar @$ref1; # Compare contents if there is any if ( $rv and $n ) { while ( $i <= $n ) { # Collect some meta data $d1 = defined $$ref1[$i]; $d2 = defined $$ref2[$i]; $t1 = $d1 ? ref $$ref1[$i] : 'undef'; $t2 = $d2 ? ref $$ref2[$i] : 'undef'; if ( $d1 == $d2 ) { # Both are undefined, so move to the next item unless ($d1) { $i++; next; } # Both are defined, so check for type $rv = $t1 eq $t2; if ($rv) { # The types are the same, so do some comparisons if ( $t1 eq 'ARRAY' ) { $rv = deepCmp( $$ref1[$i], $$ref2[$i] ); } elsif ( $t1 eq 'HASH' ) { $rv = deepCmp( $$ref1[$i], $$ref2[$i] ); } else { # Compare scalar value of all other types $rv = $$ref1[$i] eq $$ref2[$i]; } } } else { # One of the two are undefined, so quick exit $rv = 0; } # Early exit if we've found a difference already last unless $rv; # Otherwise, on to the next element $i++; } } # A little explicit sanitizing of input for false returns $rv = 0 unless $rv; pOut(); pdebug( 'leaving w/rv: %s', PDLEVEL2, $rv ); return $rv; } sub _cmpHash (\%\%) { # Purpose: Compares hashes, returns true if identical # Returns: Boolean # Usage: $rv = _cmpHash(%hash1, %hash2); my $ref1 = shift; my $ref2 = shift; my $rv = 1; my ( @k1, @k2, @v1, @v2 ); pdebug( 'entering w/%s %s', PDLEVEL2, $ref1, $ref2 ); pIn(); @k1 = sort keys %$ref1; @k2 = sort keys %$ref2; # Compare first by key list $rv = _cmpArray( @k1, @k2 ); if ($rv) { # Compare by value list foreach (@k1) { push @v1, $$ref1{$_}; push @v2, $$ref2{$_}; } $rv = _cmpArray( @v1, @v2 ); } pOut(); pdebug( 'leaving w/rv: %s', PDLEVEL2, $rv ); return $rv; } sub deepCmp (\[$@%]\[$@%]) { # Purpose: Compares data structures, returns true if identical # Returns: Boolean # Usage: $rv = deepCmp(%hash1, %hash2); # Usage: $rv = deepCmp(@array1, @arrays2); my $ref1 = shift; my $ref2 = shift; my $rv = 1; pdebug( 'entering w/%s %s', PDLEVEL1, $ref1, $ref2 ); pIn(); unless ( ref $ref1 eq ref $ref1 ) { $rv = 0; Paranoid::ERROR = pdebug( 'data structures are not the same type', PDLEVEL1 ); } if ( $rv and ref $ref1 eq 'SCALAR' ) { $rv = $ref1 eq $ref2; } elsif ( $rv and ref $ref1 eq 'ARRAY' ) { $rv = _cmpArray( @$ref1, @$ref2 ); } elsif ( $rv and ref $ref1 eq 'HASH' ) { $rv = _cmpHash( %$ref1, %$ref2 ); } else { $rv = 0; Paranoid::ERROR = pdebug( 'called with non-simple data types', PDLEVEL1 ); } pOut(); pdebug( 'leaving w/rv: %s', PDLEVEL1, $rv ); return $rv; } 1; __END__ =head1 NAME Paranoid::Data - Misc. Data Manipulation Functions =head1 VERSION $Id: lib/Paranoid/Data.pm, 2.07 2019/01/30 18:25:27 acorliss Exp $ =head1 SYNOPSIS $rv = deepCopy($source, $target); $rv = deepCopy(@source, @target); $rv = deepCopy(%source, %target); $rv = deepCmp($source, $target); $rv = deepCmp(@source, @target); $rv = deepCmp(%source, %target); =head1 DESCRIPTION This module provides data manipulation functions, which at this time only consists of B and B. =head1 SUBROUTINES/METHODS =head2 deepCopy $rv = deepCopy($source, $target); $rv = deepCopy(@source, @target); $rv = deepCopy(%source, %target); This function performs a deep and safe copy of arbitrary data structures, checking for circular references along the way. Hashes and lists are safely duplicated while all other data types are just copied. This means that any embedded object references, etc., are identical in both the source and the target, which is probably not what you want. In short, this should only be used on pure hash/list/scalar value data structures. Both the source and the target data types must be of an identical type. This function returns the number of elements copied unless it runs into a problem (such as a circular reference), in which case it returns a zero. =head2 deepCmp $rv = deepCmp($source, $target); $rv = deepCmp(@source, @target); $rv = deepCmp(%source, %target); This function performs a deep comparison of arbitrarily complex data structures (i.e., hashes of lists of lists of scalars, etc.). It returns true if the values of the structures are identical, false otherwise. Like the B function there are no provisions for evaluating objects beyond what their values are when coerced as scalar types. End sum, the same caveats that applied to B apply here. =head1 DEPENDENCIES =over =item o L =item o L =item o L =back =head1 BUGS AND LIMITATIONS =head1 AUTHOR Arthur Corliss (corliss@digitalmages.com) =head1 LICENSE AND COPYRIGHT This software is licensed under the same terms as Perl, itself. Please see http://dev.perl.org/licenses/ for more information. (c) 2005 - 2017, Arthur Corliss (corliss@digitalmages.com) Paranoid-2.07/lib/Paranoid/Args.pm0000644000175000001440000012416413424465247016515 0ustar acorlissusers# Paranoid::Args -- Command-line argument parsing functions # # (c) 2005 - 2017, Arthur Corliss # # $Id: lib/Paranoid/Args.pm, 2.07 2019/01/30 18:25:27 acorliss Exp $ # # This software is licensed under the same terms as Perl, itself. # Please see http://dev.perl.org/licenses/ for more information. # ##################################################################### ##################################################################### # # Environment definitions # ##################################################################### package Paranoid::Args; use 5.008; use strict; use warnings; use vars qw($VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS); use base qw(Exporter); use Paranoid; use Paranoid::Debug qw(:all); ($VERSION) = ( q$Revision: 2.07 $ =~ /(\d+(?:\.\d+)+)/sm ); @EXPORT = qw(parseArgs); @EXPORT_OK = ( @EXPORT, qw(PA_DEBUG PA_VERBOSE PA_HELP PA_VERSION) ); %EXPORT_TAGS = ( all => [@EXPORT_OK], ); # I know, this really doesn't protect the contents... use constant PA_DEBUG => { Short => 'D', Long => 'debug', CountShort => 1, }; use constant PA_VERBOSE => { Short => 'v', Long => 'verbose', CountShort => 1, }; use constant PA_HELP => { Short => 'h', Long => 'help', }; use constant PA_VERSION => { Short => 'V', Long => 'version', }; ##################################################################### # # Module code follows # ##################################################################### { # Internal boolean flag for noOptions my $noOptions = 0; sub _NOOPTIONS : lvalue { # Purpose: Gets/sets value of boolean flag $noOptions # Returns: Value of $noOptions # Usage: $flag = _NOOPTIONS; # Usage: _NOOPTIONS = 1; $noOptions; } # Internal errors array my @errors; sub _resetErrors { # Purpose: Empties @errors # Returns: True (1) # Usage: resetErrors(); @errors = (); return 1; } sub _pushErrors { # Purpose: Pushes a new string onto the @errors array # Returns: Same argument as called with # Usage: _pushErrors($message); my $message = shift; push @errors, $message; return $message; } sub listErrors { # Purpose: Gets the contents of @errors # Returns: Contents of @errors # Usage: @errors = listErrors(); my ( %messages, $n, @indices ); # Filter out redundant messages $n = 0; foreach (@errors) { $messages{$_}++; push @indices, $n if $messages{$_} > 1; $n++; } foreach ( sort { $b <=> $a } @indices ) { splice @errors, $_, 1; } return @errors; } # Internal options hash my %options; sub _getOption { # Purpose: Gets the template associated with passed option # Returns: Reference to template hash or undef should the # requested option not be defined # Usage: $tref = _getOption($option); my $option = shift; return exists $options{$option} ? $options{$option} : undef; } sub _setOption { # Purpose: Associates the passed option to the passed template in # %options # Returns: True (1) # Usage: _setOption($option, $tref); my $option = shift; my $tref = shift; $options{$option} = $tref; return 1; } sub _optionsKeys { # Purpose: Returns a list of keys from %options # Returns: keys %options # Usage: @keys = _optionsKeys(); return keys %options; } sub _resetOptions { # Purpose: Empties the %options # Returns: True (1) # Usage: _resetOptions(); %options = (); return 1; } # Internal arguments list my @arguments; sub _getArgRef { # Purpose: Gets a reference the argument array # Returns: Array reference # Usage: $argRef = _getArgRef(); return \@arguments; } sub clearMemory { # Purpose: Empties all internal data structures # Returns: True (1) # Usage: clearMemory(); _NOOPTIONS = 0; _resetErrors(); _resetOptions(); @{ _getArgRef() } = (); return 1; } } sub _tLint { # Purpose: Performs basic checks on a given option template for # correctness # Returns: True (1) if all checks pass, False (0) otherwise # Usage: $rv = _tLint($templateRef); my $tref = shift; # Reference to option template hash my $rv = 1; my ( $oname, @at ); pdebug( 'entering w/(%s)', PDLEVEL2, $tref ); pIn(); # Get the option name for reporting purposes (should have been populated # within parseArgs below) $oname = $$tref{Name}; # Make sure a short or long option is declared if ( !defined $oname ) { _pushErrors('No short or long option name declared'); $rv = 0; } # Make sure the argument template is defined if ($rv) { unless ( defined $$tref{Template} ) { _pushErrors("$oname option declared without a template"); $rv = 0; } } # Make sure the template contains only supported characters if ($rv) { unless ( defined $$tref{Template} && $$tref{Template} =~ /^[\$\@]*$/s ) { _pushErrors( "$oname option declared with an invalid template" . "($$tref{Template})" ); $rv = 0; } } # Make sure option names are sane if ($rv) { if ( defined $$tref{Short} ) { unless ( $$tref{Short} =~ /^[a-zA-Z0-9]$/s ) { _pushErrors( "Invalid name for the short option ($$tref{Short})"); $rv = 0; } } if ( defined $$tref{Long} ) { unless ( $$tref{Long} =~ /^[a-zA-Z0-9-]{2,}$/s ) { _pushErrors( "Invalid name for the long option ($$tref{Long})"); $rv = 0; } } } # Make sure '@' is only used once, if at all, and the option isn't # set to allow bundling if ($rv) { if ( $$tref{Template} =~ /\@/sm ) { @at = ( $$tref{Template} =~ m#(\@)#sg ); if ( @at > 1 ) { _pushErrors( 'The \'@\' symbol can only be used once in the ' . "template for $oname: $_" ); $rv = 0; } if ( $$tref{CanBundle} and defined $$tref{Short} ) { _pushErrors( "Option $$tref{Short} must have CanBundle set to false " . 'if the template contains \'@\'' ); $rv = 0; } } } # Make sure all values in our lists are defined if ($rv) { unless ( ref( $$tref{ExclusiveOf} ) eq 'ARRAY' ) { _pushErrors( "Option ${oname}'s parameter ExclusiveOf must be an " . 'array reference' ); $rv = 0; } unless ( ref( $$tref{AccompaniedBy} ) eq 'ARRAY' ) { _pushErrors( "Option ${oname}'s parameter AccompaniedBy must be an " . 'array reference' ); $rv = 0; } if ($rv) { if ( grep { !defined } @{ $$tref{ExclusiveOf} } ) { _pushErrors( "Option $oname has undefined values in ExclusiveOf"); $rv = 0; } if ( grep { !defined } @{ $$tref{AccompaniedBy} } ) { _pushErrors( "Option $oname has undefined values in ExclusiveOf"); $rv = 0; } } } # Make sure CountShort is enabled only for those with a template of '' # or '$' if ($rv) { if ( $$tref{CountShort} ) { unless ( $$tref{Template} =~ /^\$?$/sm ) { _pushErrors( "Option $oname has CountShort set but with an " . 'incompatible template' ); $rv = 0; } } } pOut(); pdebug( 'leaving w/rv: %s', PDLEVEL2, $rv ); return $rv; } sub _getArgs ($$\@) { # Purpose: Takes passed argument template and extracts the requisite # arguments to satisfy it from the argument list. The # results are stored in the passed option list. # Results: True (1) if successful, False (0) if not # Usage: $rv = _getArgs($option, $argTemplate, @optionArgs); my $option = shift; # Option name my $argTemplate = shift; # Option argument template my $lref = shift; # Array reference for retrieved arguments my $rv = 1; my $argRef = _getArgRef(); my @tmp; pdebug( 'entering w/(%s)(%s)(%s)', PDLEVEL2, $option, $argTemplate, $lref ); pIn(); # Empty the array @$lref = (); pdebug( 'contents of args: %s', PDLEVEL4, @$argRef ); # Start checking the contents of $argTemplate if ( $argTemplate eq '' ) { # Template is '' (boolean option) @$lref = (1); } elsif ( $argTemplate =~ /\@/s ) { # Template has a '@' in it -- we'll need to # grab as many of the next arguments as possible. # Check the noOptions flags if (_NOOPTIONS) { # True: gobble up everything left push @$lref, @$argRef; @$argRef = (); } else { # False: gobble up to the next option-looking thing while ( @$argRef and $$argRef[0] !~ /^--?(?:\w+.*)?$/s ) { push @$lref, shift @$argRef; } # Now, we check to see if the first remaining argument is '--'. # If it is then we must set noOptions to true and gobble the # rest. if ( @$argRef and $$argRef[0] eq '--' ) { _NOOPTIONS = 1; shift @$argRef; push @$lref, @$argRef; @$argRef = (); } } } else { # The template is not empty and has no '@', so we'll just grab the next # n arguments, n being the length of the template # Check the noOptions flag if (_NOOPTIONS) { # True: grab everything we need while ( @$argRef and @$lref < length $argTemplate ) { push @$lref, shift @$argRef; } } else { # False: grab as many non-option-looking things as we can while ( @$argRef and $$argRef[0] !~ /^--?(?:\w+.*)$/s and @$lref < length $argTemplate ) { push @$lref, shift @$argRef; } # Now, we check to see if we still need more arguments and if # the first remaining argument is '--'. If it is then we must # set noOptions to true and gobble what we need. if ( @$lref < length $argTemplate and @$argRef and $$argRef[0] eq '--' ) { _NOOPTIONS = 1; shift @$argRef; while ( @$argRef and @$lref < length $argTemplate ) { push @$lref, shift @$argRef; } } } } # Final check: did we get minimum requisite number of arguments? if ( @$lref < length $argTemplate ) { _pushErrors( pdebug( 'Missing the minimum number of arguments for %s', PDLEVEL1, $option ) ); $rv = 0; } else { pdebug( 'extracted the following arguments: %s', PDLEVEL3, @$lref ); } # sublist '@' portions of multicharacter templates if ( $rv and $argTemplate =~ /\@/sm and length $argTemplate > 1 ) { @tmp = ( [], [], [] ); # First, shift off all preceding '$'s if ( $argTemplate =~ /^(\$+)/s ) { @{ $tmp[0] } = splice @$lref, 0, length $1; } # Next, pop off all trailing '$' if ( $argTemplate =~ /(\$+)\$/s ) { @{ $tmp[2] } = splice @$lref, -1 * length $1; } # Everything left belongs to the '@' @{ $tmp[1] } = @$lref; # Let's put it all together... @$lref = (); push @$lref, @{ $tmp[0] } if @{ $tmp[0] }; push @$lref, $tmp[1]; push @$lref, @{ $tmp[2] } if @{ $tmp[2] }; pdebug( 'sublisted arguments into: %s', PDLEVEL3, @$lref ); } pOut(); pdebug( 'leaving w/rv: %s', PDLEVEL2, $rv ); return $rv; } sub _storeArgs ($$\@) { # Purpose: Stores the passed option arguments in the passed option # template's Value, but in accordance with parameters in the # template # Returns: True (1) # Usage: _storeArgs($optionTemplate, $argTemplate, @optionArgs); my $tref = shift; my $argTemplate = shift; my $lref = shift; pdebug( 'entering w/(%s)(%s)(%s)', PDLEVEL2, $tref, $argTemplate, $lref ); pIn(); pdebug( 'adding values to %s', PDLEVEL3, $$tref{Name} ); # Increment our usage counter $$tref{Count}++; # Store arguments according to the template if ( $argTemplate eq '' ) { # Template is '' $$tref{Value} = 0 unless defined $$tref{Value}; $$tref{Value}++; pdebug( 'Value is now %s', PDLEVEL3, $$tref{Value} ); } elsif ( $argTemplate eq '$' ) { # Template is '$' if ( not $$tref{Multiple} or $$tref{CountShort} ) { # Store the value directly since we # can only be used once $$tref{Value} = $$lref[0]; pdebug( 'Value is now %s', PDLEVEL3, $$tref{Value} ); } else { # Store the value as part of a list since # we can be used multiple times $$tref{Value} = [] unless defined $$tref{Value} and ref $$tref{Value} eq 'ARRAY'; push @{ $$tref{Value} }, $$lref[0]; pdebug( 'Value is now %s', PDLEVEL3, @{ $$tref{Value} } ); } } else { # Template is anything else if ( not $$tref{Multiple} ) { # Store the values directly in a an array # since we can only be used once $$tref{Value} = [@$lref]; pdebug( 'Value is now %s', PDLEVEL3, @{ $$tref{Value} } ); } else { # Store the values as an element of an # array since we can be used multiple times $$tref{Value} = [] unless defined $$tref{Value} and ref $$tref{Value} eq 'ARRAY'; push @{ $$tref{Value} }, [@$lref]; pdebug( 'Value now has %d sets', PDLEVEL3, scalar @{ $$tref{Value} } ); } } pOut(); pdebug( 'leaving w/rv: 1', PDLEVEL2 ); return 1; } sub parseArgs (\@\%;\@) { # Purpose: Extracts and validates all command-line arguments and options, # storing them in an organized hash for easy retrieval # Returns: True (1) if successful, False (0) if not # Usage: $rv = parseArgs(@templates, %options); # Usage: $rv = parseArgs(@templates, %options, @args); my $tlref = shift; # Templates list ref my $oref = shift; # Options hash ref my $paref = shift; # Program argument list ref my $rv = 1; my ( $tref, $oname, $argRef, $arg, $argTemplate ); my ( @tmp, @oargs, $regex ); # Validate arguments $paref = \@ARGV unless defined $paref; pdebug( 'entering w/(%s)(%s)(%s)', PDLEVEL1, $tlref, $oref, $paref ); pIn(); # Clear all internal data structures and reset flag clearMemory(); # Empty the passed options hash %$oref = (); # Make a copy of the argument list $argRef = _getArgRef(); @$argRef = (@$paref); # Assemble %options and lint-check the templates foreach (@$tlref) { # Make sure the element is a hash reference unless ( ref $_ eq 'HASH' ) { _pushErrors('Illegal non-hash reference in templates array'); $rv = 0; next; } # Establish a base template and copy the contents of the passed hash $tref = { Short => undef, Long => undef, Template => '', Multiple => 0, ExclusiveOf => [], AccompaniedBy => [], CanBundle => 0, CountShort => 0, Value => undef, %$_, }; # Set AllOptions for error message reporting $$tref{Name} = defined $$tref{Short} && defined $$tref{Long} ? "-$$tref{Short}/--$$tref{Long}" : defined $$tref{Short} ? "-$$tref{Short}" : defined $$tref{Long} ? "--$$tref{Long}" : undef; # Initialize our usage counter $$tref{Count} = 0; # Anything that has CountShort enabled implies Multiple/CanBundle # and a template of '$' if ( $$tref{CountShort} ) { $$tref{CanBundle} = $$tref{Multiple} = 1; $$tref{Template} = '$' if defined $$tref{Long}; } # Anything that has a Short option and a template of '$' or '' # implies CanBundle $$tref{CanBundle} = 1 if defined $$tref{Short} and $$tref{Template} eq ''; # We'll associate both the long and short options to the same hash # to make sure that we count/collect everything appropriately. # # Store the short option if ( defined $$tref{Short} and length $$tref{Short} ) { # See if a template is already defined if ( defined _getOption( $$tref{Short} ) ) { # It is -- report the error Paranoid::ERROR = _pushErrors( pdebug( 'the %s option has more than one template', PDLEVEL1, $$tref{Short} ) ); $rv = 0; } else { # It's not -- go ahead and store it _setOption( $$tref{Short}, $tref ); } } # Store the long option if ( defined $$tref{Long} and length $$tref{Long} ) { # See if a template is already defined if ( defined _getOption( $$tref{Long} ) ) { # It is -- report the error Paranoid::ERROR = _pushErrors( pdebug( 'the %s option has more than one template', PDLEVEL1, $$tref{Long} ) ); $rv = 0; } else { # It's not -- go ahead and store it _setOption( $$tref{Long}, $tref ); } } # Do a basic lint-check on the template $rv = 0 unless _tLint($tref); } if ($rv) { while (@$argRef) { $arg = shift @$argRef; next unless defined $arg; # Start testing $arg if ( $arg eq '--' and not _NOOPTIONS ) { # $arg is '--', so set the no options flag _NOOPTIONS = 1; } elsif ( not _NOOPTIONS and $arg =~ /^--?/s ) { # '--' hasn't been passed yet and this looks # like an option... # Test types of options if ( $arg =~ /^-(\w.*)$/s ) { # With a single '-' it should be a short option. However, # we'll split the option portion, in case there's more # than one character @tmp = split //s, $1; # If there's more than one character for the option name # it must be either a bunch of bundled options or an # option with a concatenated argument. In case of the # latter (assuming that CanBundle is set to false (a # prerequisite of argument concatenation) and it has a # template of '$' (another prerequisite)) we'll unshift # the rest of the characters back onto the argument list. # # Oh, but first we'll need to get the applicable # option template and then start testing... $tref = _getOption( $tmp[0] ); if ( $#tmp and defined $tref and $$tref{Template} eq '$' and not $$tref{CanBundle} ) { unshift @$argRef, join '', @tmp[ 1 .. $#tmp ]; splice @tmp, 1; } # Start processing all remaining short options in @tmp foreach (@tmp) { # Get the template $tref = _getOption($_); # Make sure the option is supported if ( defined $tref ) { # Make sure option allows bundling if bundled if ($#tmp) { unless ( $$tref{CanBundle} ) { _pushErrors( "Option $_ used bundled with " . 'other options' ); $rv = 0; next; } } # Get the argument template $argTemplate = $$tref{Template}; # Override the template if CountShort is true $argTemplate = '' if $argTemplate eq '$' and $$tref{CountShort}; # Get any accompanying arguments unless ( _getArgs( "-$_", $argTemplate, @oargs ) ) { $rv = 0; next; } # Check if we've call this more than once if ( not $$tref{Multiple} and $$tref{Count} > 0 ) { _pushErrors( "Option $$tref{Name} is only allowed " . 'to be used once' ); $rv = 0; next; } # Store the values _storeArgs( $tref, $argTemplate, @oargs ); } else { # Warn that this is an unknown option _pushErrors("Unknown short option used: $_"); $rv = 0; } } } elsif ( $arg =~ /^--([\w-]+)(?:=(.+))?$/sm ) { # Starts with '--', so must be a long option # Save the extracted option/argument portion @tmp = ($1); push @tmp, $2 if defined $2 and length $2; # If this option had an argument portion we need to # unshift it back onto the argument list *provided* it was # a legal argument, i.e., this option had a template of # '$'. $tref = _getOption( $tmp[0] ); if ( $#tmp and defined $tref ) { # Test for various templates if ( $$tref{Template} eq '$' ) { # Legal invocation -- unshift away unshift @$argRef, $tmp[1]; } elsif ( $$tref{Template} eq '' ) { # Illegal, no arguments expected _pushErrors( "--$tmp[0] does not require any " . 'arguments' ); $rv = 0; next; } else { # Illegal, can't use concatenated arguments in # more complex templates _pushErrors( "--$tmp[0] cannot be called like " . 'this when multiple arguments are ' . 'required.' ); } } # Handle known options if ( defined $tref ) { # Get the argument template $argTemplate = $$tref{Template}; # Snarf extra arguments unless ( _getArgs( "--$tmp[0]", $argTemplate, @oargs ) ) { $rv = 0; next; } # Check if we've call this more than once if ( not $$tref{Multiple} and $$tref{Count} > 0 ) { _pushErrors( "Option $$tref{Name} is only allowed to be used once" ); $rv = 0; next; } # Store the values _storeArgs( $tref, $argTemplate, @oargs ); } else { # Unknown long option _pushErrors("Unknown option: --$tmp[0]"); $rv = 0; } } else { # Unknown option-looking thingy _pushErrors("Unknown option thingy: $arg"); $rv = 0; } } else { # Everything else is payload $$oref{PAYLOAD} = [] unless exists $$oref{PAYLOAD}; push @{ $$oref{PAYLOAD} }, $arg; } } } # Make a list of all the arguments that was used @tmp = (); foreach ( _optionsKeys() ) { push @tmp, $_ if ${ _getOption($_) }{Count}; } # Final sanity check foreach ( sort @tmp ) { $tref = _getOption($_); # Make sure nothing was called that is exclusive of # other called options if ( @{ $$tref{ExclusiveOf} } ) { $regex = '(?:' . join( '|', @{ $$tref{ExclusiveOf} } ) . ')'; if ( grep /^$regex$/sm, @tmp ) { _pushErrors( "$$tref{Name} cannot be called with the following options: " . join ', ', @{ $$tref{ExclusiveOf} } ); $rv = 0; } } # Make sure the option was called in conjunction with others foreach $regex ( @{ $$tref{AccompaniedBy} } ) { unless ( grep /^\Q$regex\E$/sm, @tmp ) { _pushErrors( "$$tref{Name} must be called with the following options: " . join ', ', @{ $$tref{AccompaniedBy} } ); $rv = 0; } } # Copy the values into %$oref $$oref{$_} = $$tref{Value}; } pOut(); pdebug( 'leaving w/rv: %s', PDLEVEL1, $rv ); return $rv; } 1; __END__ =head1 NAME Paranoid::Args - Command-line argument parsing functions =head1 VERSION $Id: lib/Paranoid/Args.pm, 2.07 2019/01/30 18:25:27 acorliss Exp $ =head1 SYNOPSIS use Paranoid::Args; $rv = parseArgs(@templates, %opts); $rv = parseArgs(@templates, %opts, @args); @errors = Paranoid::Args::listErrors(); Paranoid::Args::clearMemory(); =head1 DESCRIPTION The purpose of this module is to provide simplified but validated parsing and extraction of command-line arguments (otherwise known as the contents of @ARGV). It is meant to be used in lieu of modules like B and B, but that does not mean that this module is functionally equivalent -- it isn't. There are things that those modules do that this doesn't, but that's primarily by design. My priorities are a bit different when it comes to this particular task. The primary focus of this module is validation, with the secondary focus being preservation of context. =head2 VALIDATION When validating the use of options and arguments we concern ourselves primarily the following things: =over =item 1) Is the option accompanied by the requisite arguments? =item 2) Was the option called with the other requisite options? =item 3) Was the option called without options meant only for mutually exclusive use? =item 4) Were any unrecognized options used? =back This module also does basic sanity validation of all option templates to ensure correct usage of this module. =head2 PRESERVATION OF CONTEXT Simply put, preservation of context means remembering the order and grouping of associated arguments. Take the hypothetical case of "tagging" files. The traditional approach is to define an option that takes a single string argument and apply them to the remaining contents of @ARGV: ./foo.pl -t "tag1" file1 file2 This module supports that model, with the option argument template being '$' for that single string. But what if you wanted to apply different tags to different files with one command execution? ./foo.pl -t "tag1" file1 file2 -t "tag2" file3 In this case it is important to keep each group of payloads that you want to operate on separate. With this module you could instead use an argument template of '$@', which would return each set independently: %opt = ( 't' => [ [ "tag1", [ "file1", "file2" ] ], [ "tag2", [ "file3" ] ], ], ); Notice that we also preserve the context between the '$' and the '@' by putting the '@' arguments in a sublist. With this example that could possible be considered pointless, but we also support templates like '$$@$' which makes this very useful. Now, instead of having to shift or pop off the encapsulating arguments they now have one permanent ordinal index. You also can now just grab the array reference for the '@' portion and iterate over a complete and separate list rather than having to take a splice of the complete argument array. It's probably just me, but I find that a little easier to track. =head2 SUPPORTED COMMAND-LINE SYNTAX The following list of syntactical options are supported: =over =item o Short option bundling (i.e., "rm -rf") =item o Short option counting (i.e., "ssh -vvv") =item o Short option argument concatenation (i.e., "cut -d' '") =item o Long option "equals" argument concatenation (i.e., "./configure --prefix=/usr") =item o The use of '--' to designate all following arguments are strictly that, even if they look like options. =back This module don't support the hash key/value pairs (i.e., -s foo=one bar=two) or argument type validation (B can validate string, integer, and floating point argument types). And while it supports a short & long option it doesn't support innumerable aliases in addition. In short, if it isn't explicitly documented it isn't supported, though it probably is in B. There are a few restrictions meant to eliminate confusion: =over =item 1) Long and short argument concatenation is only allowed if the argument template is '$' (expecting a single argument, only). =item 2) Short argument concatenation is furthermore only allowed on arguments that aren't allowed to be bundled with other short options. =item 3) Short options supporting bundling can require associate arguments as long as '@' is not part of the argument template. =back =head1 SUBROUTINES/METHODS =head2 parseArgs $rv = parseArgs(@templates, %opts); $rv = parseArgs(@templates, %opts, @args); Using the option templates passed as the first reference this function populates the options hash with all of the parsed options found in the passed arguments. The args list reference can be omitted if you wish the function to work off of B<@ARGV>. Please note that this function makes a working copy of the array, so no alterations will be made to it. If any options and/or arguments fail to match the option template, or if an option is found with no template, a text message is pushed into an errors array and the function will return a boolean false. When the options hash is populated extracted arguments to the options are stored in both long and short form as the keys, assuming they were defined in the template. Otherwise it will use whatever form of option was defined. Any arguments not associated with an option are stored in the options hash in a list associated with the key B. =head2 Paranoid::Args::listErrors @errors = Paranoid::Args::listErrors(); If you need a list of everything that was found wrong during a B run, from template errors to command-line argument validation failures, you can get all of the messages form B. Please note that we show it fully qualified because it is B exported. Each time B is invoked this array is reset. =head2 Paranoid::Args::clearMemory Paranoid::Args::clearMemory(); If the existence of a (most likely) lightly populated array bothers you, you may use this function to empty all internal data structures of their contents. Like B this function is not exported. =head1 OPTION TEMPLATES The function provided by this module depends on templates to extract and validate the options and arguments. Each option template looks similar to the following: { Short => 'v', Long => 'verbose', Template => '$', CountShort => 1, Multiple => 1, CanBundle => 1, ExclusiveOf => [], AccompaniedBy => [], } This template provides extraction of verbose options in the following (and similar) forms: -vvvvv --verbose 5 --verbose=5 If B was instead false you'd have to say '-v 5' instead of '-vvvvv'. When the B function is called the options hash passed to it would be populated with: %opts = ( 'v' => 5, 'verbose' => 5, ); The redundancy is intentional. Regardless of whether you look up the short or the long name you will be able to retrieve the cumulative value. The particulars of all key/value pairs in a template are documented below. B The default template is as follows: { Short => undef, Long => undef, Template => '', Multiple => 0, ExclusiveOf => [], AccompaniedBy => [], CanBundle => 0, CountShort => 0, Value => undef, }; When creating your option templates you only need to specify those that differ from the defaults. In addition, there's a few options that are also modified automatically for you. If your template consists of a I option and has a template of I<''> then I is automatically set to true. If I is enabled then I and I is set to be true as well. Additionally, if there is a I option, the I