链接嵌套的单击多命令

时间:2019-07-18 21:18:00

标签: python-click

我想在主入口点组下面嵌套一个多命令。 This comment说不可能:

  

实际上,所提供的某些保证确实   不太有意义。实际上,在某些情况下   行为也意外导致了错误。因此,在Click 6中,以下内容   事情是真的:

     
      
  • 链命令可以嵌套
  •   
  • 如果链命令在链注释下方,则将其注释   会吃掉所有争论,然后回到外链   不可能
  •   
  • 不再支持组上的可选参数。
  •   

真的不可能吗?是我所需的界面内部不一致,还是只是点击次数受限,应该切换到argparse?

#!/usr/bin/env python3

import click


@click.group(chain=True)
def cli():
    pass


@cli.command()
def greet():
    print("hello")


@cli.group()
def read():
    pass


@cli.group()
def write():
    pass


@read.command()
@click.option("--header/--no-header")
def csv(header):
    print("reading csv")


@write.command()
def json():
    print("writing json")


if __name__ == "__main__":
    cli()

使用chain = False来运行它,

# cl.py read csv write json
# Usage: cl.py read csv [OPTIONS]
# Try "cl.py read csv --help" for help.

# Error: Got unexpected extra arguments (write json)

With chain = True,

# cl.py read csv write json
# Traceback (most recent call last):
#   File "cl.py", line 16, in <module>
#     @cli.group()
#  ...
# RuntimeError: It is not possible to add multi commands as children to another multi command that is in chain mode.  Command "cli" is set to chain and "read" was added as subcommand but it in itself is a multi command.  ("read" is a Group within a chained Group named "cli")

0 个答案:

没有答案