javascript - Command Validation in Minimist -


newbie minimist command parser , facing issue it.
user enters command :

project -a    

i have validate if entered command has right option.
code follows:

var commands = ["project", "user"]; var commandentered = require('minimist')(command.split(' '));   if(commands.indexof(commandentered._) > -1){     //i.e. check if user has entered either project or user , following    if(commandentered._ == "project") {      var options = ["a", "u", "l"];         delete commandentered._;         var optionsentered = object.keys(commandentered);           for(var i=0;i<optionsentered.length;i++){            if(options.indexof(optionsentered) > -1){                 if(optionsentered == "a" && commandentered.a == true)                 {                     console.log("option entered");                 }             }         }    } }   else{   return "invalid command"; }   

how validate command unwanted options or if there command:

 project -a -n <name>   

how can set rules telling if option 'n' name has provided , option 'l' cannot included if option 'a' present. there way can fix this?

thanks in advance

this can easy or complex checking, depending on needs.

basic check if there 'a' command:

if (commandentered.a) {   console.log('we have option a') } else {   console.log("we don't have it") } 

check unwanted options (e.g.):

var validoptions = ['a', 'u', 'l']; function isoptionpresent(opt) {   (var = 0; i<validoptions.length; i++) {     if (validoptions[i] == opt) {       return true     }   }   return false }  (var opt in commandentered) {   if (!isoptionpresent(opt)) {     console.log('this command not allowed')     console.log('use these commands: ' + validoptions)   } } 

also note want change line (adding [0]):

if(commands.indexof(commandentered._[0]) > -1){ 

complex rules mentioned in last sentence - can check them 1 one, if simple

if (commandentered.a && !commandentered.l) {   console.log('you have provide l command command') } if (commandentered.n && 'string' !== typeof(commandentered.n)) {   console.log('command n must followed name of type string')   console.log('e.g. -n myname') } 

if can made better, using object instead of simple array, define type of parameter, not repeat (then don't need isoptionpresent() function):

var validoptions = {'a':'boolean', 'n':'string', 'l':'boolean'}; (var opt in commandentered) {   if (!validoptions[opt]) {     console.log('this command not allowed')     console.log('use these commands: ' + object.keys(validoptions))   } }  if (commandentered.n && validoptions[opt] !== typeof(commandentered.n)) {   console.log('command n must followed name of type string')   console.log('e.g. -n myname') } 

if there lot of complex rules, may better think structure define rule inter relations , check if passed command comply structure. think, far want.


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -