问题描述
新手指向命令解析器,面对一个问题。
用户输入命令:
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>
如何设置规则告诉如果选项是'n',则必须提供名称和选项如果存在选项'a',则不能包括'l'。有什么办法可以解决这个问题吗?
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?
感谢提前
推荐答案
基本检查是否有'a'命令:
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)
}
}
另外请注意,您可能想更改行(添加[0]):
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')
}
如果可以做得更好,简单数组,这也将定义参数的类型,所以你不会重复自己太多(那么你不需要isOptionPresent()函数):
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.
这篇关于Minimist中的命令验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!