简体   繁体   English

用JSON表示命令行参数的好方法是什么?

[英]What is a good way to represent command-line arguments with JSON?

I'm trying to make improvements to the grunt-closure-linter npm project (so I can actually use it in a productive fashion), and here's where I'm stuck on it now: 我正在尝试对grunt-closure-linter npm项目进行改进(这样我就可以以实际的方式使用它了),这就是我现在坚持的地方:

I want to specify a way to pass options through to the command line gjslint which is the driver for the Closure Linter. 我想指定一种将选项传递到命令行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
...

As you can see this thing has many options! 如您所见,这东西有很多选择!

The grunt task is pretty concise so I was quickly able to find where to inject this into the command line to get this done, however I'm wondering how best to go about converting a sane JSON representation like grunt任务非常简洁,因此我很快就能够找到将其注入命令行的位置以完成此任务,但是我想知道如何最好地转换类似的JSON表示形式

{
    "max_line_length": '120',
    "summary": true
}

into a commandline options string: 到命令行选项字符串中:

--max_line_length 120 --summary

It's not even clear that there is any standard way to represent it with JSON. 甚至不清楚用JSON表示它的任何标准方法。 It did occur to me that somebody else might not consider it sane to specify a plain no-param argument using the value true . 我确实想到其他人可能不会认为使用值true指定纯无参数实在是理智。

I figure I guess I could fall back to a more unambiguous but less structured 我想我可能会退回到更加明确但结构化程度较低的位置

[ "--max_line_length", "120", "--summary" ]

or some such, though that's hardly practical considering how tempted I would be to avoid the commas and quotes and leave it as a plain string. 或类似的东西,尽管考虑到如何避免使用逗号和引号并将其保留为纯字符串,这几乎是不切实际的。

How is this supposed to get defined? 应该如何定义?

I've adapted my module dargs , which converts an object of options to an array of command-line arguments, for your use-case. 我已对模块dargs进行了调整,以适合您的用例,该模块将options对象转换为命令行参数数组。

Just pass it an object with camelCased keys and it will do the rest. 只需将其传递给带有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;
};

Example: 例:

toArgs({ maxLineLength: 120 });

Outputs: 输出:

['--max_line_length', '120']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM