Python argparse似乎不支持太长的争论?

时间:2018-02-09 22:33:45

标签: python-3.x terminal argparse

我创建了一个包含以下args的脚本:

def arguments():
    ''' gets arguments '''

    parser = argparse.ArgumentParser(
        description="This scripts start the setup for the project, this setup is defined by the Core \
                     Module of each Setup, so first it download the scripts from that module and \
                     proceed to run them from a temporal directory installing the desired \
                     components and applying the configuration."
    )

    parser.add_argument(
        "--project_name",
        help="The name for the project",
        type=str,
        required=True)

    parser.add_argument(
        "--project_dir",
        help="The dir where to create the project",
        type=str,
        required=True)

    parser.add_argument(
        "--core_npm_package",
        type=str,
        help="The NPM core package",
        required=True)

    parser.add_argument(
        "--npm_packages",
        nargs='+',
        type=str,
        help="The NPM package for install if no source code is required",
        required=True)

    ''' the modules data array in JSON format, it follows the structure defined in 
        src/app/modules/setup.module/vos/module.vo.ts '''

    parser.add_argument(
        "--modules_data",
        type=str,
        help="The data of each module in JSON format",
        required=True)

    return parser.parse_args()

然后当我尝试使用带有以下参数的python3运行脚本时:

python3 run_setup.py --project_name TEST_PROJECT --project_dir /home/diego/Desktop/ --npm_packages @creard_nativescript_ng/auth.module --core_npm_package @creard_nativescript_ng/core.module --modules_data 'some long json or any data'

我得到了输出:

usage: run_setup.py [-h] [--project_name PROJECT_NAME]
                    [--project_dir PROJECT_DIR]
run_setup.py: error: unrecognized arguments: --npm_packages @creard_nativescript_ng/auth.module --core_npm_package @creard_nativescript_ng/core.module --modules_data [{"moduleName":"auth.module","package":"@creard_nativescript_ng/auth.module","options":{"loginConfig":{"title":"title modified"}}}]

但是如果我删除任何参数(也是 - 我认为是罪魁祸首的--modules_data)总是指出我错过了一些论点,无论哪个论点,但一起都不起作用。

0 个答案:

没有答案