问题
Newbie to Minimist command parser and facing an issue with it.
User enters a command :
Project -a
I have to validate if entered command has right option.
So my code is as 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 and then do 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 a entered");
}
}
}
}
}
else{
return "Invalid Command";
}
How will I validate command for unwanted options or say If there is a command:
project -a -n <name>
How Can I set rules telling that if option is 'n' then name has to be provided and option 'l' cannot be included if option 'a' is present. Is there any way I can fix this?
Thanks In advance
回答1:
This can be easy or complex checking, depending on your needs.
Basic check if there is 'a' command:
if (commandEntered.a) {
console.log('we have option a')
} else {
console.log("we don't have it")
}
Check for unwanted options (e.g.):
var validOptions = ['a', 'u', 'l'];
function isOptionPresent(opt) {
for (var i = 0; i<validOptions.length; i++) {
if (validOptions[i] == opt) {
return true
}
}
return false
}
for (var opt in commandEntered) {
if (!isOptionPresent(opt)) {
console.log('this command is not allowed')
console.log('use these commands: ' + validOptions)
}
}
Also note that you probably want to change your line (adding [0]):
if(commands.indexOf(commandEntered._[0]) > -1){
Complex rules as you mentioned in last sentence - you can check them one by one, if it is simple
if (commandEntered.a && !commandEntered.l) {
console.log('you have to provide L command with A command')
}
if (commandEntered.n && 'string' !== typeof(commandEntered.n)) {
console.log('command N must be followed with name of type string')
console.log('e.g. -n myname')
}
If can be made better, with using object instead of simple array, which will also define the type of the parameter, so you will not repeat yourself too much (then you don't need isOptionPresent() function):
var validOptions = {'a':'boolean', 'n':'string', 'l':'boolean'};
for (var opt in commandEntered) {
if (!validOptions[opt]) {
console.log('this command is not allowed')
console.log('use these commands: ' + Object.keys(validOptions))
}
}
if (commandEntered.n && validOptions[opt] !== typeof(commandEntered.n)) {
console.log('command N must be followed with name of type string')
console.log('e.g. -n myname')
}
If there is a lot of complex rules, it may be better to think about some structure that will define rule inter relations and to check if the passed command comply with the structure. But I think, that is too far from what you want.
来源:https://stackoverflow.com/questions/27021844/command-validation-in-minimist