首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用JSON表示命令行参数的好方法是什么?

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

Stack Overflow用户
提问于 2013-08-12 05:56:15
回答 1查看 358关注 0票数 0

我正在尝试对grunt-closure-linter npm项目进行改进(这样我就可以以一种高效的方式使用它),以下是我现在被困在它上面的地方:

我想指定一种将选项传递给命令行gjslint的方法,该命令行Linter是Closure Linter的驱动程序。

代码语言:javascript
复制
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
...

正如你所看到的,这个东西有很多选项!

这个繁琐的任务非常简洁,所以我很快就能找到将其注入命令行的位置,但是我想知道如何才能最好地转换一个合理的JSON表示,比如

代码语言:javascript
复制
{
    "max_line_length": '120',
    "summary": true
}

转换为命令行选项字符串:

代码语言:javascript
复制
--max_line_length 120 --summary

甚至还不清楚是否有用JSON表示它的标准方法。我确实想到,其他人可能认为使用值true指定一个普通的无参数参数是不明智的。

我想我可以退回到一个更明确但不那么结构化的

代码语言:javascript
复制
[ "--max_line_length", "120", "--summary" ]

或者诸如此类的东西,尽管考虑到我有多想避免逗号和引号,并将其保留为普通字符串,这几乎是不现实的。

这应该如何定义呢?

EN

回答 1

Stack Overflow用户

发布于 2013-08-12 15:43:19

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

只需将一个带有camelCased键的对象传递给它,它就会完成剩下的工作。

代码语言:javascript
复制
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;
};

示例:

代码语言:javascript
复制
toArgs({ maxLineLength: 120 });

输出:

代码语言:javascript
复制
['--max_line_length', '120']
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18177109

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档