Changeset 174

Show
Ignore:
Timestamp:
07/07/04 02:10:37 (4 years ago)
Author:
athomas
Message:

VALIDATE target can now be a HASH, ARRAY or a simple SCALAR. If a SCALAR and
true, the HELP target at the same level is used to validate the input. ie. Any
token not in the help HASH is an error.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • manage/trunk/CLI.pm

    r173 r174  
    115115my $token = $ctx->next_token(); 
    116116my ($width, $height) = GetTerminalSize(*STDIN); 
     117my $process_help = sub { 
     118        my $help = shift; 
     119        my $commands = shift; 
     120        my $command = shift; 
     121        my $type = ref($help); 
     122 
     123                if ($type eq 'ARRAY') { 
     124                        $commands->{$help->[0]} = $help->[1]; 
     125                } elsif ($type eq 'CODE') { 
     126                        %$commands = (%$commands, $help->($ctx, @{$ctx->args()})); 
     127                } elsif (!$type) { 
     128                        $commands->{$command} = $help; 
     129                } 
     130        }; 
    117131 
    118132        # Help!! 
     
    130144                # Construct help string 
    131145                $help .= $format->paragraphs(split(/^\s*$/m, $cmd->{HELPHEADER})) . "\n" if $cmd->{HELPHEADER}; 
    132  
    133         my $process_help = sub { 
    134                 my $help = shift; 
    135                 my $commands = shift; 
    136                 my $command = shift; 
    137                 my $type = ref($help); 
    138  
    139                         if ($type eq 'ARRAY') { 
    140                                 $commands->{$help->[0]} = $help->[1]; 
    141                         } elsif ($type eq 'CODE') { 
    142                                 %$commands = (%$commands, $help->($ctx, @{$ctx->args()})); 
    143                         } elsif (!$type) { 
    144                                 $commands->{$command} = $help; 
    145                         } 
    146                 }; 
    147146 
    148147                for my $key (keys %$cmd) { 
     
    270269                                        # Do validation 
    271270                                        if ($cmd->{$key}->{VALIDATE}) { 
    272                                                 return ('ERROR', "Invalid input '$token'.", $ctx) 
    273                                                         unless $cmd->{$key}->{VALIDATE}->($ctx, $token, @{$ctx->args()}); 
     271                                        my $type = ref($cmd->{$key}->{VALIDATE}); 
     272 
     273                                                if (not $type) { 
     274                                                        if ($cmd->{$key}->{VALIDATE} and $cmd->{$key}->{HELP}) { 
     275                                                        my %commands; 
     276 
     277                                                                $process_help->($cmd->{$key}->{HELP}, \%commands, '<eol>'); 
     278                                                                return ('ERROR', "Invalid input '$token'.", $ctx) 
     279                                                                        unless $commands{$token}; 
     280                                                        } 
     281                                                } elsif ($type eq 'CODE') { 
     282                                                        return ('ERROR', "Invalid input '$token'.", $ctx) 
     283                                                                unless $cmd->{$key}->{VALIDATE}->($ctx, $token, @{$ctx->args()}); 
     284                                                } elsif ($type eq 'HASH') { 
     285                                                        return ('ERROR', "Invalid input '$token'.", $ctx) 
     286                                                                unless $cmd->{$key}->{VALIDATE}->{$token}; 
     287                                                } elsif ($type eq 'ARRAY') { 
     288                                                my $found = 0; 
     289 
     290                                                        VALIDATE_ARRAY: for my $t (@{$cmd->{$key}->{VALIDATE}}) { 
     291                                                                $found =1, last VALIDATE_ARRAY 
     292                                                                        if $token eq $t; 
     293                                                        } 
     294                                                        return ('ERROR', "Invalid input '$token'.", $ctx) 
     295                                                                unless $found; 
     296                                                } else { 
     297                                                        return ('ERROR', 'Invalid value for VALIDATE target.', $ctx); 
     298                                                } 
    274299                                        } 
    275300 
  • manage/trunk/manage

    r173 r174  
    5353# Plugin path 
    5454my $PLUGINS = "/usr/local/libexec/$SELF"; 
    55 $PLUGINS = "./plugins"; 
     55#$PLUGINS = "./plugins"; 
    5656our %CONF = ( 
    5757        prompt => {  
  • manage/trunk/plugins/nodelist2.pm

    r173 r174  
    55                '1)lalala' => sub { print "FOO\n"; }, 
    66                '2)$.*' => { 
     7                        VALIDATE => 1, 
    78                        HELP => sub { 
    89                                return (