[英]What is a good way to represent command-line arguments with JSON?
我正在尝试对grunt-closure-linter npm项目进行改进(这样我就可以以实际的方式使用它了),这就是我现在坚持的地方:
我想指定一种将选项传递到命令行gjslint
的方法,该命令是Closure Linter的驱动程序。
USAGE: /usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/bin/gjslint [flags]
flags:
closure_linter.checker:
--closurized_namespaces: Namespace prefixes, used for testing ofgoog.provide/require
(default: '')
(a comma separated list)
--ignored_extra_namespaces: Fully qualified namespaces that should be not be reported as extra
by the linter.
(default: '')
(a comma separated list)
closure_linter.common.simplefileflags:
-e,--exclude_directories: Exclude the specified directories (only applicable along with -r or
--presubmit)
(default: '_demos')
(a comma separated list)
-x,--exclude_files: Exclude the specified files
(default: 'deps.js')
(a comma separated list)
-r,--recurse: Recurse in to the subdirectories of the given path;
repeat this option to specify a list of values
closure_linter.ecmalintrules:
--custom_jsdoc_tags: Extra jsdoc tags to allow
(default: '')
(a comma separated list)
closure_linter.error_check:
--jslint_error: List of specific lint errors to check. Here is a list of accepted values:
- all: enables all following errors.
- blank_lines_at_top_level: validatesnum
...
如您所见,这东西有很多选择!
grunt任务非常简洁,因此我很快就能够找到将其注入命令行的位置以完成此任务,但是我想知道如何最好地转换类似的JSON表示形式
{
"max_line_length": '120',
"summary": true
}
到命令行选项字符串中:
--max_line_length 120 --summary
甚至不清楚用JSON表示它的任何标准方法。 我确实想到其他人可能不会认为使用值true
指定纯无参数实在是理智。
我想我可能会退回到更加明确但结构化程度较低的位置
[ "--max_line_length", "120", "--summary" ]
或类似的东西,尽管考虑到如何避免使用逗号和引号并将其保留为纯字符串,这几乎是不切实际的。
应该如何定义?
我已对模块dargs进行了调整,以适合您的用例,该模块将options对象转换为命令行参数数组。
只需将其传递给带有camelCased键的对象,其余的操作就会完成。
function toArgs(options) {
var args = [];
Object.keys(options).forEach(function (key) {
var flag;
var val = options[key];
flag = key.replace(/[A-Z]/g, '_$&').toLowerCase();
if (val === true) {
args.push('--' + flag);
}
if (typeof val === 'string') {
args.push('--' + flag, val);
}
if (typeof val === 'number' && isNaN(val) === false) {
args.push('--' + flag, '' + val);
}
if (Array.isArray(val)) {
val.forEach(function (arrVal) {
args.push('--' + flag, arrVal);
});
}
});
return args;
};
例:
toArgs({ maxLineLength: 120 });
输出:
['--max_line_length', '120']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.