首页 > 解决方案 > Python click 模块不适用于连字符参数

问题描述

我尝试使用这样的模块将多个参数传递给我的 python 脚本 ( opsgit) :click

import click

@click.command()
@click.argument('arguments', nargs=-1)
def cli(arguments):
    """CLI for git"""
    cmd = create_command(arguments)
    _execute_command(cmd)

当我执行此命令行时:

$ opsgit git checkout -b pvt_test

我收到此错误:

Usage: opsgit git [OPTIONS] [ARGUMENTS]...
Try "opsgit git --help" for help.
    
Error: no such option: -b

谁能让我知道如何解决这个问题?

标签: pythoncommand-line-interfacepython-click

解决方案


你错过了ignore_unkown_options国旗。这是添加了标志的示例。查看文档以获取有关如何使用的更多信息nargs

import click

@click.command(context_settings=dict(
    ignore_unknown_options=True,
))
@click.argument('arguments', nargs=-1)
def cli(arguments):
    """CLI for git"""
    cmd = click.create_command(arguments)
    _execute_command(cmd)

推荐阅读