| 1 | #!/usr/bin/perl | ||
| 2 | package Getopt::toHash; | ||
| 3 | require Switch; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use Switch; | ||
| 8 | |||
| 9 | our $VERSION = '1.01'; | ||
| 10 | |||
| 11 | # ABSTRACT: Turns command line arguments into hash and validates. | ||
| 12 | |||
| 13 | |||
| 14 | =pod | ||
| 15 | How to use: | ||
| 16 | |||
| 17 | use Getopt::toHash; | ||
| 18 | |||
| 19 | --Grab all arguments, return into hash ref. | ||
| 20 | my $Args = Getopt::toHash->get_em(); | ||
| 21 | |||
| 22 | Everything else is optional beyond this point. | ||
| 23 | |||
| 24 | --Validation | ||
| 25 | If you want the module to only make sure certain arguments are there and not validate them. | ||
| 26 | $Args->validate('required'=>['-a','-b','-c']); | ||
| 27 | |||
| 28 | The Module can validate on 7 Different constraints | ||
| 29 | REGEXP - {'REGEXP=>'^[-+]?[0-9]'} - Specify a regex the passed data must comply with. | ||
| 30 | MINMAX - {'MINMAX'=>{min=>20,max=>40}} - Specify minimum and maximum numbers the argument must be. | ||
| 31 | GREATER_THAN - {'GREATER_THAN'=>{min=>20}} - Specify a minimum number the argument must be. | ||
| 32 | LESS_THAN - {'LESS_THAN'=>{max=>40}} - Specify a maximum number the argument must be lower than. | ||
| 33 | ASCII - ['ASCII'] - The argument must be any ASCII text with spaces allowed. | ||
| 34 | NOSPACES - ['NOSPACES'] - The argument must have no spaces. | ||
| 35 | ANY - ['ANY'] - The argument must be defined. | ||
| 36 | |||
| 37 | You can validate each argument on as many types of validation as you want by stringing together each array ref or hash. The Argument must pass each constraint | ||
| 38 | |||
| 39 | --Example 1 - Validating with a inline object. | ||
| 40 | $Args->validate( '-a'=>[ 'ASCII','NOSPACES',{ 'MINMAX'=>{min=>20,max=>40} } ] ); | ||
| 41 | |||
| 42 | --Example 2 - Validating with a hash spec. | ||
| 43 | my %spec = ( '-a'=>[ 'ASCII','NOSPACES', {'MINMAX'=>{min=>20,max=>40} } ] ); | ||
| 44 | $Args->validate(%spec); | ||
| 45 | |||
| 46 | The module will display error messages and die on failed validation by default. If you dont want this you can pass... | ||
| 47 | $Args->validate( no_errors=>'1','-a'=>[ 'ASCII','NOSPACES',{ 'MINMAX'=>{min=>20,max=>40} } ] ); | ||
| 48 | =cut | ||
| 49 | |||
| 50 | |||
| 51 | sub get_em { | ||
| 52 | |||
| 53 | my $class = shift; | ||
| 54 | my @args = @ARGV; | ||
| 55 | my $arg_count = scalar @ARGV; | ||
| 56 | |||
| 57 | if (!@ARGV) {return;} | ||
| 58 | |||
| 59 | my $arg_hash; | ||
| 60 | |||
| 61 | # Make sure there is even number of args and values passed | ||
| 62 | if (($arg_count % 2) != 0) { die "Odd number of arguments ($arg_count) passed, pass in (-arg value) pairs"; } | ||
| 63 | |||
| 64 | my $it = natatime( 2, @args ); | ||
| 65 | while (my @vals = $it->()) { | ||
| 66 | $arg_hash = put_em_in($arg_hash, @vals); | ||
| 67 | } | ||
| 68 | |||
| 69 | bless $arg_hash, $class; | ||
| 70 | |||
| 71 | return $arg_hash; | ||
| 72 | |||
| 73 | } | ||
| 74 | |||
| 75 | sub put_em_in { | ||
| 76 | |||
| 77 | my $self = shift; | ||
| 78 | my @pair = @_; | ||
| 79 | |||
| 80 | my $arg = $pair[0]; | ||
| 81 | my $val = $pair[1]; | ||
| 82 | |||
| 83 | chomp($val); | ||
| 84 | chomp($arg); | ||
| 85 | |||
| 86 | if ($arg !~ /-/) { die "Specify arguments with a \"-\"";} | ||
| 87 | |||
| 88 | if (exists $self->{$arg}) { | ||
| 89 | die "Lets not pass the same argument twice, ok?"; | ||
| 90 | } | ||
| 91 | |||
| 92 | $self->{$arg} = $val; | ||
| 93 | |||
| 94 | return $self; | ||
| 95 | |||
| 96 | } | ||
| 97 | |||
| 98 | #from List::Utils | ||
| 99 | sub natatime ($@) { | ||
| 100 | |||
| 101 | my $n = shift; | ||
| 102 | my @list = @_; | ||
| 103 | |||
| 104 | return sub | ||
| 105 | { | ||
| 106 | return splice @list, 0, $n; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 | sub validate { | ||
| 111 | |||
| 112 | my $hash = shift; | ||
| 113 | my %passed = @_; | ||
| 114 | my $errors = 'yes'; | ||
| 115 | |||
| 116 | my $self; | ||
| 117 | |||
| 118 | #Foreach of the passed Args set a hash key in $self | ||
| 119 | foreach my $key (keys %passed) { | ||
| 120 | if (defined($passed{$key})) { | ||
| 121 | $self->{$key} = $passed{$key}; | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | if (exists $self->{no_errors}) { | ||
| 126 | $errors = 'no'; | ||
| 127 | delete $self->{no_errors}; | ||
| 128 | } | ||
| 129 | |||
| 130 | #If you only pass required args | ||
| 131 | if (exists $self->{required}) { | ||
| 132 | foreach my $arg (@{$self->{required}}) { | ||
| 133 | if (!exists $hash->{$arg}){ | ||
| 134 | die "You must pass $arg\n"; | ||
| 135 | } else { | ||
| 136 | $hash->{valid} = 'TRUE'; | ||
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | return $hash; | ||
| 141 | |||
| 142 | } else { | ||
| 143 | |||
| 144 | my $rv; | ||
| 145 | foreach my $key (keys %{$self}) { | ||
| 146 | |||
| 147 | foreach my $entry (@{$self->{$key}}) { | ||
| 148 | |||
| 149 | my $ref_type = ref($entry); | ||
| 150 | |||
| 151 | if ($ref_type ne 'HASH' and $ref_type) { die "Invalid validation structure - $ref_type - for $key.\n"; } | ||
| 152 | |||
| 153 | if ($ref_type eq 'HASH') { | ||
| 154 | |||
| 155 | foreach my $key2 (keys %{$entry}) { | ||
| 156 | |||
| 157 | switch ($key2) { | ||
| 158 | case /REGEXP/ { $rv->{$key}->{$key2} = REGEXP($hash->{$key},$entry->{$key2}); } | ||
| 159 | case /MINMAX/ { $rv->{$key}->{$key2} = MINMAX($hash->{$key},$entry->{$key2}->{min},$entry->{$key2}->{max}); } | ||
| 160 | case /GREATER_THAN/ { $rv->{$key}->{$key2} = GREATER_THAN($hash->{$key},$entry->{$key2}->{min}); } | ||
| 161 | case /LESS_THAN/ { $rv->{$key}->{$key2} = LESS_THAN($hash->{$key},$entry->{$key2}->{max}); } | ||
| 162 | else { warn "$entry is not a validation type.\n"; } | ||
| 163 | } | ||
| 164 | } | ||
| 165 | } | ||
| 166 | |||
| 167 | if (!$ref_type) { | ||
| 168 | |||
| 169 | switch ($entry) { | ||
| 170 | case /ASCII/ { $rv->{$key}->{$entry} = ASCII($hash->{$key}); } | ||
| 171 | case /NOSPACES/ { $rv->{$key}->{$entry} = NOSPACES($hash->{$key}); } | ||
| 172 | case /ANY/ { $rv->{$key}->{$entry} = ANY($hash->{$key}); } | ||
| 173 | case /INT/ { $rv->{$key}->{$entry} = INT($hash->{$key}); } | ||
| 174 | else { warn "$entry is not a validation type.\n"; } | ||
| 175 | } | ||
| 176 | } | ||
| 177 | } | ||
| 178 | } | ||
| 179 | if ($errors eq 'yes') { | ||
| 180 | results($rv); | ||
| 181 | } | ||
| 182 | |||
| 183 | $hash->{validation} = $rv; | ||
| 184 | return $hash; | ||
| 185 | } | ||
| 186 | } | ||
| 187 | |||
| 188 | sub results { | ||
| 189 | my $self = shift; | ||
| 190 | my $validation_fails; | ||
| 191 | |||
| 192 | foreach my $arg (keys %{$self}) { | ||
| 193 | |||
| 194 | foreach my $type (keys %{$self->{$arg}}) { | ||
| 195 | my $result = $self->{$arg}->{$type}->{result}; | ||
| 196 | my $data = $self->{$arg}->{$type}->{data}; | ||
| 197 | |||
| 198 | if ($result eq 'INVALID') { | ||
| 199 | $validation_fails++; | ||
| 200 | print "Invalid [$arg], Type [$type]"; | ||
| 201 | while (my($k,$v) = each %{$self->{$arg}->{$type}} ) { | ||
| 202 | if ($k eq 'data' or $k eq 'result'){next;} | ||
| 203 | print ", Req: ($k = $v)"; | ||
| 204 | } | ||
| 205 | print ", Data: $data.\n"; | ||
| 206 | } | ||
| 207 | } | ||
| 208 | } | ||
| 209 | if ($validation_fails) { | ||
| 210 | die "$validation_fails Invalid arguments.\n"; | ||
| 211 | } | ||
| 212 | } | ||
| 213 | |||
| 214 | sub ASCII { | ||
| 215 | my $data = shift; | ||
| 216 | |||
| 217 | if (!defined $data) { return {result=>'INVALID',data=>'NOT_DEFINED'}; } | ||
| 218 | if ($data =~ /^[\x20-\x7E]+$/) { return {result=>'VALID',data=>$data}; } | ||
| 219 | |||
| 220 | return {result=>'INVALID',data=>$data}; | ||
| 221 | } | ||
| 222 | |||
| 223 | sub NOSPACES { | ||
| 224 | my $data = shift; | ||
| 225 | |||
| 226 | if (!defined $data) { return {result=>'INVALID',data=>'NOT_DEFINED'}; } | ||
| 227 | if ($data !~ /\s/) { return {result=>'VALID',data=>$data}; } | ||
| 228 | |||
| 229 | return {result=>'INVALID',data=>$data}; | ||
| 230 | } | ||
| 231 | |||
| 232 | sub ANY { | ||
| 233 | my $data = shift; | ||
| 234 | |||
| 235 | if (!defined $data) { return {result=>'INVALID',data=>'NOT_DEFINED'}; } | ||
| 236 | if (defined $data) { return {result=>'VALID',data=>$data}; } | ||
| 237 | |||
| 238 | return {result=>'INVALID',data=>$data}; | ||
| 239 | } | ||
| 240 | |||
| 241 | sub INT { | ||
| 242 | my $data = shift; | ||
| 243 | |||
| 244 | if (!defined $data) { return {result=>'INVALID',data=>'NOT_DEFINED'}; } | ||
| 245 | if ($data !~ qr/^[0-9]*$/) { return {result=>'INVALID',data=>$data}; } | ||
| 246 | |||
| 247 | return {result=>'VALID',data=>$data}; | ||
| 248 | } | ||
| 249 | |||
| 250 | sub REGEXP { | ||
| 251 | my $data = shift; | ||
| 252 | my $regexp = shift; | ||
| 253 | |||
| 254 | if (!defined $data) { return {result=>'INVALID',data=>'NOT_DEFINED'}; } | ||
| 255 | if (!$regexp) { die "{'REGEXP=>'^[-+]?[0-9]'} Bad structure for REGEXP validation\n"; } | ||
| 256 | if ($data !~ /$regexp/) { return {result=>'INVALID',data=>$data}; } | ||
| 257 | |||
| 258 | return {result=>'VALID',data=>$data}; | ||
| 259 | } | ||
| 260 | |||
| 261 | |||
| 262 | sub MINMAX { | ||
| 263 | my $data = shift; | ||
| 264 | my $min = shift; | ||
| 265 | my $max = shift; | ||
| 266 | |||
| 267 | if (!defined $data) { return {result=>'INVALID',data=>'NOT_DEFINED'}; } | ||
| 268 | if (!$min or !$max) { die "{'MINMAX'=>{min=>20,max=>40}} Bad structure for MINMAX validation\n";} | ||
| 269 | if ($min !~ qr/^[0-9]*$/ or $max !~ qr/^[0-9]*$/) { die "Numeric only for min and max on MINMAX validation\n";} | ||
| 270 | if ($data !~ qr/^[0-9]*$/) { return {result=>'INVALID',data=>$data,min=>$min,max=>$max}; } | ||
| 271 | if ($data <= $min or $data >= $max ) { return {result=>'INVALID',data=>$data,min=>$min,max=>$max}; } | ||
| 272 | |||
| 273 | return {result=>'VALID',data=>$data,min=>$min,max=>$max}; | ||
| 274 | } | ||
| 275 | |||
| 276 | sub GREATER_THAN { | ||
| 277 | my $data = shift; | ||
| 278 | my $min = shift; | ||
| 279 | |||
| 280 | if (!defined $data) { return {result=>'INVALID',data=>'NOT_DEFINED'}; } | ||
| 281 | if (!$min) { die "{'GREATER_THAN'=>{min=>20}} Bad structure for GREATER_THAN validation\n"; } | ||
| 282 | if ($min !~ qr/^[0-9]*$/) { die "Numeric only for min on GREATER_THAN validation\n"; } | ||
| 283 | if ($data !~ qr/^[0-9]*$/) { return {result=>'INVALID',data=>$data,min=>$min};} | ||
| 284 | if ($data <= $min ) { return {result=>'INVALID',data=>$data,min=>$min}; } | ||
| 285 | |||
| 286 | return {result=>'VALID',data=>$data,min=>$min}; | ||
| 287 | } | ||
| 288 | |||
| 289 | sub LESS_THAN { | ||
| 290 | my $data = shift; | ||
| 291 | my $max = shift; | ||
| 292 | |||
| 293 | if (!defined $data) { return {result=>'INVALID',data=>'NOT_DEFINED'}; } | ||
| 294 | if ($max !~ qr/^[0-9]*$/) { die "{'LESS_THAN'=>{max=>40}} - Bad structure for LESS_THAN validation\n"; } | ||
| 295 | if ($data !~ qr/^[0-9]*$/) { return {result=>'INVALID',data=>$data,max=>$max}; } | ||
| 296 | if ($data >= $max ) { return {result=>'INVALID',data=>$data,max=>$max}; } | ||
| 297 | |||
| 298 | return {result=>'VALID',data=>$data,max=>$max}; | ||
| 299 | } | ||
| 300 | |||
| 301 | 1; |