首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带参数的Python Cement子命令

带参数的Python Cement子命令
EN

Stack Overflow用户
提问于 2017-01-31 10:12:20
回答 3查看 1.2K关注 0票数 1

我正在使用Python水泥框架,并尝试创建一个带有几个子命令的应用程序,每个子命令都有自己的参数。

Diving Right In示例显示了如何生成子命令,但这些子命令都没有自己的参数。

Argument and Options Handling示例显示了如何处理参数,而不是使用子命令。

Application Controllers页面再次显示了如何生成子命令,但没有特定子命令的参数。

Multiple Stacked Controllers非常接近,但我发现我不能让它看起来很正确。这就是我所拥有的:

代码语言:javascript
复制
from cement.core.foundation import CementApp
from cement.core.controller import CementBaseController, expose


class MyBaseController(CementBaseController):
    class Meta:
        label = 'base'
        description = "<Overall application description>"


class MySubcommand(CementBaseController):
    class Meta:
        label = 'subcommand1'
        stacked_on = 'base'
        stacked_type = 'nested'
        description = '<Subcommand description>'
        arguments = [(['--bar'], {'help': 'An option visible only to subcommand1!'})]

    @expose(help='<Redundant subcommand description')
    def subcommand1(self):
        self.app.log.info("This is what happens when you run demo2.py subcommand1")


class MyApp(CementApp):
    class Meta:
        label = 'demo2'
        base_controller = 'base'
        handlers = [MyBaseController, MySubcommand]


with MyApp() as app:
    app.run()

这是可行的,但唯一的问题是出现了笨拙的帮助文本:

代码语言:javascript
复制
> python3 demo2.py subcommand1 --help
usage: demo2 (sub-commands ...) [options ...] {arguments ...}

<Subcommand description>

commands:

  subcommand1
    <Redundant subcommand description

optional arguments:
  -h, --help  show this help message and exit
  --debug     toggle debug output
  --quiet     suppress all output
  --bar BAR   An option visible only to subcommand1!

如您所见,subcommand1是作为subcommand1的另一个子命令提供的,带有冗余的帮助文本。即使我省略了expose help参数,令人困惑的表示仍然存在。

如果我改用stacked_type = 'embedded',那么teach子命令的参数对所有其他命令都是可见的。

EN

回答 3

Stack Overflow用户

发布于 2017-02-01 00:17:10

您应该考虑使用来自argparse扩展的新ArgparseController (随水泥一起提供)。它取代了CementBaseController,它将在水泥3中完全消失。ArgparseController的定制化程度要低得多,而且更多地远离障碍……因此,它更多地是对Argparse的直接使用(即嵌套子命令/控制器实际上是嵌套子解析器,具有自己的参数等)。

  • http://builtoncement.com/2.10/whats_new.html#argparsecontroller
  • http://builtoncement.com/2.10/api/ext/ext_argparse.html#cement-ext-ext-argparse

Cement 2没有为了保持兼容性而放弃CementBaseController或将ArgparseController设置为默认值,但可以从扩展中获得它,并建议使用它。

票数 2
EN

Stack Overflow用户

发布于 2017-01-31 10:12:20

这是一个基于@derks答案的示例。显然,ArgParseController是一种新的做事方式。

代码语言:javascript
复制
from cement.core.foundation import CementApp
from cement.ext.ext_argparse import ArgparseController, expose


class MyBaseController(ArgparseController):
    def default(self):
        self.app.args.print_help()

    @expose(
        arguments=[
            (['a'], {'help': 'A positional argument'}),
            (['--barg', '-b'], {'help': 'Keyword argument'}),
        ],
        help='<Subcommand description>')
    def subcommand(self):
        self.app.log.info("subcommand successful")


class MyApp(CementApp):
    class Meta:
        label = 'demo'
        handlers = [MyBaseController]


with MyApp() as app:
    app.run()

老答案

这是我最初的答案,基本上是一个使用CementBaseController的杂乱无章的东西

expose hide option可以生成一些非常漂亮的帮助文本:

代码语言:javascript
复制
from cement.core.foundation import CementApp
from cement.core.controller import CementBaseController, expose


class MyBaseController(CementBaseController):
    class Meta:
        label = 'base'
        description = "<Overall application description>"


class MySubcommand(CementBaseController):
    class Meta:
        label = 'subcommand1'
        stacked_on = 'base'
        stacked_type = 'nested'
        description = '<Subcommand description>'
        arguments = [(['--bar'], {'help': 'An option visible only to subcommand1!'})]

    @expose(hide=True)
    def default(self):
        self.app.log.info("This is what happens when you run demo2.py subcommand1")


class MyApp(CementApp):
    class Meta:
        label = 'demo3'
        base_controller = 'base'
        handlers = [MyBaseController, MySubcommand]


with MyApp() as app:
    app.run()

帮助文本:

代码语言:javascript
复制
> python3 demo3.py subcommand1 --help
usage: demo3 (sub-commands ...) [options ...] {arguments ...}

<Subcommand description>

optional arguments:
  -h, --help  show this help message and exit
  --debug     toggle debug output
  --quiet     suppress all output
  --bar BAR   An option visible only to subcommand1!
票数 1
EN

Stack Overflow用户

发布于 2019-11-28 06:01:31

对于子命令,有没有一种不使用--help来显示帮助的方法?

代码语言:javascript
复制
python3 demo3.py subcommand1

显示相同的内容

代码语言:javascript
复制
python3 demo3.py subcommand1 --help
票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41948649

复制
相关文章

相似问题

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