Changeset 176
- Timestamp:
- 07/07/04 18:44:42 (4 years ago)
- Files:
-
- manage/trunk/CLI.pm (modified) (2 diffs)
- manage/trunk/manage (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
manage/trunk/CLI.pm
r175 r176 263 263 my $type = ref($cmd->{$key}); 264 264 265 if ($type eq 'HASH' and $cmd->{$key}->{VALIDATE}) { 266 my $type = ref($cmd->{$key}->{VALIDATE}); 267 268 if (not $type) { 269 if ($cmd->{$key}->{VALIDATE} and $cmd->{$key}->{HELP}) { 270 my %commands; 271 272 $process_help->($cmd->{$key}->{HELP}, \%commands, '<eol>'); 273 return ('ERROR', "Invalid input '$token'.", $ctx) 274 unless $commands{$token}; 275 } 276 } elsif ($type eq 'CODE') { 277 return ('ERROR', "Invalid input '$token'.", $ctx) 278 unless $cmd->{$key}->{VALIDATE}->($ctx, $token, @{$ctx->args()}); 279 } elsif ($type eq 'HASH') { 280 return ('ERROR', "Invalid input '$token'.", $ctx) 281 unless $cmd->{$key}->{VALIDATE}->{$token}; 282 } elsif ($type eq 'ARRAY') { 283 my $found = 0; 284 285 VALIDATE_ARRAY: for my $t (@{$cmd->{$key}->{VALIDATE}}) { 286 $found =1, last VALIDATE_ARRAY 287 if $token eq $t; 288 } 289 return ('ERROR', "Invalid input '$token'.", $ctx) 290 unless $found; 291 } else { 292 return ('ERROR', 'Invalid value for VALIDATE target.', $ctx); 293 } 294 } 295 265 296 $ctx->next_arg($token) if $trackvar; 266 297 … … 270 301 271 302 if ($type eq 'HASH') { 272 # Do validation273 if ($cmd->{$key}->{VALIDATE}) {274 my $type = ref($cmd->{$key}->{VALIDATE});275 276 if (not $type) {277 if ($cmd->{$key}->{VALIDATE} and $cmd->{$key}->{HELP}) {278 my %commands;279 280 $process_help->($cmd->{$key}->{HELP}, \%commands, '<eol>');281 return ('ERROR', "Invalid input '$token'.", $ctx)282 unless $commands{$token};283 }284 } elsif ($type eq 'CODE') {285 return ('ERROR', "Invalid input '$token'.", $ctx)286 unless $cmd->{$key}->{VALIDATE}->($ctx, $token, @{$ctx->args()});287 } elsif ($type eq 'HASH') {288 return ('ERROR', "Invalid input '$token'.", $ctx)289 unless $cmd->{$key}->{VALIDATE}->{$token};290 } elsif ($type eq 'ARRAY') {291 my $found = 0;292 293 VALIDATE_ARRAY: for my $t (@{$cmd->{$key}->{VALIDATE}}) {294 $found =1, last VALIDATE_ARRAY295 if $token eq $t;296 }297 return ('ERROR', "Invalid input '$token'.", $ctx)298 unless $found;299 } else {300 return ('ERROR', 'Invalid value for VALIDATE target.', $ctx);301 }302 }303 304 303 $ctx->inject_token($token) if $rx eq 'ACTION'; 305 304 return $self->parse_node($ctx->next_grammar($cmd->{$key})); manage/trunk/manage
r175 r176 43 43 white => "\e[37m", 44 44 ); 45 %C = ( bold => "", nounderline => "", underline => "", reset => "", normal => "", black => "", 46 red => "", green => "", brown => "", blue => "", magenta => "", cyan => "", 47 white => "" ) unless (-t STDOUT); 48 49 our @ALTCOLOURS = ("$C{cyan}", "$C{cyan}$C{bold}"); 45 %C = ( bold => "", nobold => "", nounderline => "", underline => "", 46 reset => "", normal => "", black => "", 47 red => "", green => "", brown => "", blue => "", magenta => "", 48 cyan => "", white => "" ) unless (-t STDOUT); 49 50 our @ALTCOLOURS = ("$C{cyan}$C{bold}", "$C{cyan}$C{nobold}"); 50 51 # Global variables 51 52 our $PROMPT = "$SELF> "; … … 61 62 debug => { 62 63 value => \$DEBUG, 63 pattern => '\d +',64 pattern => '\d', 64 65 help => 'Set debug level (0-9).', 65 66 }, … … 225 226 my @candidates; 226 227 push(@th, "") if $action =~ /More input expected/; 227 my $prefix = ' ' x length("@th[0 .. $#th - 1]") . (@th < @ath ? ' ' : '');228 my $prefix = ' ' x length("@th[0 .. $#th - 1]") . (@th <= @ath ? ' ' : ''); 228 229 229 230 $action = "$C{underline}@ath$C{nounderline}\n $prefix^ $action"; … … 259 260 if ($grammar->{ACTION}) { 260 261 $action .= "\n ${prefix}Expected end of command."; 261 } else {262 $action .= "\n ${prefix}Can't find any candidates, probably a grammar error."262 # } else { 263 # $action .= "\n ${prefix}Can't find any candidates, probably a grammar error." 263 264 } 264 265 } … … 313 314 } 314 315 }, 316 VALIDATE => sub { 317 my ($ctx, $value, $pattern) = (shift, shift, $CONF{shift()}->{pattern}); 318 319 $pattern = '.+' unless defined($pattern); 320 return $value =~ /^$pattern$/; 321 }, 315 322 HELP => [ '<value>', 'Set value.' ], 316 323 }, 324 VALIDATE => 1, 317 325 HELP => sub { 318 326 my $ctx = shift;
