0

我正在尝试对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 来表示它。我确实想到,其他人可能认为使用 value 指定一个普通的 no-param 参数是不明智的true

我想我想我可以退回到一个更明确但结构更少的

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

或类似的,尽管考虑到我会多么想避免使用逗号和引号并将其保留为纯字符串,这几乎不切实际。

这应该如何定义?

4

1 回答 1

0

我已经针对您的用例调整了我的模块dargs,它将选项对象转换为命令行参数数组。

只需将带有 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']
于 2013-08-12T07:43:18.980 回答