Python argparse:列出用法中的各个选择

2024-01-20

我有一个需要多个参数的程序,例如

breakfast.py --customer=vikings eggs sausage bacon

其中“鸡蛋”、“香肠”和“培根”可以从特定选项列表中指定。

现在我喜欢的输出breakfast.py --help看起来像这样:

usage: breakfast.py [-h] [--customer CUSTOMER] INGREDIENT [INGREDIENT ...]

positional arguments:
  your choice of ingredients:
    bacon              Lovely bacon
    egg                The runny kind
    sausage            Just a roll
    spam               Glorious SPAM
    tomato             Sliced and diced

optional arguments:
  -h, --help           show this help message and exit
  --customer CUSTOMER  salutation for addressing the customer

我尝试了两种方法,但到目前为止都对我来说失败了。

使用参数选择:

import argparse

parser = argparse.ArgumentParser()

toppings = {
    'bacon': "Lovely bacon",
    'egg': 'The runny kind',
    'sausage': 'Just a roll',
    'spam': 'Glorious SPAM',
    'tomato': 'Sliced and diced',
}
parser.add_argument('--customer', default='Mr. and Mrs. Bun', action='store',
                    help='salutation for addressing the customer')
parser.add_argument('ingredients', nargs='+', choices=toppings.keys(),
                    help='your choice of ingredients')

options = parser.parse_args('--customer=Vikings egg sausage bacon'.split())
print("Dear {}, we are happy to serve you {}" \
      .format(options.customer, ', '.join(options.ingredients)))

上述程序的用法打印了一个没有详细信息的字典格式的列表:

usage: breakfast.py [-h] [--customer CUSTOMER]
                      {bacon,egg,sausage,spam,tomato}
                      [{bacon,egg,sausage,spam,tomato} ...]

positional arguments:
  {bacon,egg,sausage,spam,tomato}
                        your choice of ingredients

Adding metavar='INGREDIENT' to add_argument('ingredients', ...)根本没有列出选择:

usage: breakfast.py [-h] [--customer CUSTOMER] INGREDIENT [INGREDIENT ...]

positional arguments:
  INGREDIENT           your choice of ingredients

我简单地尝试使用子程序:

import argparse

parser = argparse.ArgumentParser()

parser.add_argument('--customer', default='Mr. and Mrs. Bun', action='store',
                    help='salutation for addressing the customer')
ingredients = parser.add_subparsers(title='your choice of an ingredient',
                    dest='ingredient', metavar='ingredient')
ingredients.add_parser('bacon', help="Lovely bacon")
ingredients.add_parser('egg', help="The runny kind")
ingredients.add_parser('sausage', help="Just a roll")
ingredients.add_parser('spam', help="Glorious SPAM")
ingredients.add_parser('tomato', help="Sliced and diced")

options = parser.parse_args('--customer=Vikings spam'.split())
print("Dear {}, we are happy to serve you {}" \
      .format(options.customer, options.ingredient))

其中确实以我喜欢的方式列出了用法:

usage: breakfast.py [-h] [--customer CUSTOMER] ingredient ...

optional arguments:
  -h, --help           show this help message and exit
  --customer CUSTOMER  salutation for addressing the customer

your choice of an ingredient:
  ingredient
    bacon              Lovely bacon
    egg                The runny kind
    sausage            Just a roll
    spam               Glorious SPAM
    tomato             Sliced and diced

默认情况下,子程序只允许选择一个选项。幸运的是这个答案表明可以允许多个子命令 https://stackoverflow.com/a/10579924/428542),但这感觉就像是为了获得正确的格式而进行的黑客攻击。我最近从 argparse 转移到配置参数解析 https://github.com/bw2/ConfigArgParse,而这种方法在那里失败了。

我想我最好恢复使用具有多个选择的单个参数,并使用自定义格式。

不幸的是,有关调整 argparse 格式的文档很少,因此我感谢一些如何解决此问题的帮助。


根据这里的反馈,我深入研究了 argparse 代码。使用子解析器的合理解决方案发布于https://stackoverflow.com/a/49977713/428542 https://stackoverflow.com/a/49977713/428542.

此外,我还找到了为每个选项添加伪操作的解决方案,以及修改格式化程序的解决方案。最后,我提出了一个混合解决方案,为每个选项添加伪操作,但通过利用一些实现细节,只有格式化程序才能使用它们。

第一个解决方案定义了一个自定义操作,其目的是不执行任何操作,但仍然打印一些使用信息。这个 NoAction 类给出了不同的选项。

import argparse

class NoAction(argparse.Action):
    def __init__(self, **kwargs):
        kwargs.setdefault('default', argparse.SUPPRESS)
        kwargs.setdefault('nargs', 0)
        super(NoAction, self).__init__(**kwargs)
    def __call__(self, parser, namespace, values, option_string=None):
        pass

parser = argparse.ArgumentParser()
parser.register('action', 'none', NoAction)

parser.add_argument('--customer', default='Mr. and Mrs. Bun', action='store',
                 help='salutation for addressing the customer')
parser.add_argument('ingredients', nargs='*', metavar='INGREDIENT',
                 choices=['bacon', 'egg', 'sausage', 'spam', 'tomato'],
                 help='List of ingredients')

group = parser.add_argument_group(title='your choice of ingredients')
group.add_argument('bacon', help="Lovely bacon", action='none')
group.add_argument('egg', help="The runny kind", action='none')
group.add_argument('sausage', help="Just a roll", action='none')
group.add_argument('spam', help="Glorious SPAM", action='none')
group.add_argument('tomato', help="Sliced and diced", action='none')

options = parser.parse_args('--customer=Vikings egg sausage bacon'.split())
print("Dear {}, we are happy to serve you {}" \
      .format(options.customer, ', '.join(options.ingredients)))

options = parser.parse_args(['--help'])

其输出:

Dear Vikings, we are happy to serve you egg, sausage, bacon

usage: customchoices.py [-h] [--customer CUSTOMER]
                        [INGREDIENT [INGREDIENT ...]]

positional arguments:
  INGREDIENT           List of ingredients

optional arguments:
  -h, --help           show this help message and exit
  --customer CUSTOMER  salutation for addressing the customer

your choice of ingredients:
  bacon                Lovely bacon
  egg                  The runny kind
  sausage              Just a roll
  spam                 Glorious SPAM
  tomato               Sliced and diced

一个小缺点是,各个选择都被添加到成分(用于解析)和解析器(用于格式化)中。我们还可以定义一个方法来直接将选项添加到成分解析器中:

import argparse

class NoAction(argparse.Action):
    def __init__(self, **kwargs):
        kwargs.setdefault('default', argparse.SUPPRESS)
        kwargs.setdefault('nargs', 0)
        super(NoAction, self).__init__(**kwargs)
    def __call__(self, parser, namespace, values, option_string=None):
        pass

class ChoicesAction(argparse._StoreAction):
    def add_choice(self, choice, help=''):
        if self.choices is None:
            self.choices = []
        self.choices.append(choice)
        self.container.add_argument(choice, help=help, action='none')

parser = argparse.ArgumentParser()
parser.register('action', 'none', NoAction)
parser.register('action', 'store_choice', ChoicesAction)

parser.add_argument('--customer', default='Mr. and Mrs. Bun', action='store',
                 help='salutation for addressing the customer')

group = parser.add_argument_group(title='your choice of ingredients')
ingredients = group.add_argument('ingredients', nargs='*', metavar='INGREDIENT',
                 action='store_choice')
ingredients.add_choice('bacon', help="Lovely bacon")
ingredients.add_choice('egg', help="The runny kind")
ingredients.add_choice('sausage', help="Just a roll")
ingredients.add_choice('spam', help="Glorious SPAM")
ingredients.add_choice('tomato', help="Sliced and diced")

尽管有两个动作子类,上面的方法可能是我最喜欢的方法。它只使用公共方法。

另一种方法是修改格式化程序。这是可能的,它修改列表中的action.choices['option1', 'option2']到字典{'option1': 'help_for_option1', 'option2', 'help_for_option2'},以及或多或少的重新实现HelpFormatter._format_action() as HelpFormatterWithChoices.format_choices():

import argparse

class HelpFormatterWithChoices(argparse.HelpFormatter):
    def add_argument(self, action):
        if action.help is not argparse.SUPPRESS:
            if isinstance(action.choices, dict):
                for choice, choice_help in action.choices.items():
                    self._add_item(self.format_choices, [choice, choice_help])
            else:
                super(HelpFormatterWithChoices, self).add_argument(action)
    def format_choices(self, choice, choice_help):
        # determine the required width and the entry label
        help_position = min(self._action_max_length + 2,
                            self._max_help_position)
        help_width = max(self._width - help_position, 11)
        action_width = help_position - self._current_indent - 2
        choice_header = choice

        # short choice name; start on the same line and pad two spaces
        if len(choice_header) <= action_width:
            tup = self._current_indent, '', action_width, choice_header
            choice_header = '%*s%-*s  ' % tup
            indent_first = 0

        # long choice name; start on the next line
        else:
            tup = self._current_indent, '', choice_header
            choice_header = '%*s%s\n' % tup
            indent_first = help_position

        # collect the pieces of the choice help
        parts = [choice_header]

        # add lines of help text
        help_lines = self._split_lines(choice_help, help_width)
        parts.append('%*s%s\n' % (indent_first, '', help_lines[0]))
        for line in help_lines[1:]:
            parts.append('%*s%s\n' % (help_position, '', line))

        # return a single string
        return self._join_parts(parts)

parser = argparse.ArgumentParser(formatter_class=HelpFormatterWithChoices)

toppings = {
    'bacon': "Lovely bacon",
    'egg': 'The runny kind',
    'sausage': 'Just a roll',
    'spam': 'Glorious SPAM',
    'tomato': 'Sliced and diced',
}

parser.add_argument('--customer', default='Mr. and Mrs. Bun', action='store',
                 help='salutation for addressing the customer')

group = parser.add_argument_group(title='your choice of ingredients')
ingredients = group.add_argument('ingredients', nargs='*', metavar='INGREDIENT',
                 choices=toppings)

options = parser.parse_args('--customer=Vikings egg sausage bacon'.split())
print("Dear {}, we are happy to serve you {}" \
      .format(options.customer, ', '.join(options.ingredients)))

print()
options = parser.parse_args(['--help'])

其输出:

Dear Vikings, we are happy to serve you egg, sausage, bacon

usage: helpformatter.py [-h] [--customer CUSTOMER]
                        [INGREDIENT [INGREDIENT ...]]

optional arguments:
  -h, --help           show this help message and exit
  --customer CUSTOMER  salutation for addressing the customer

your choice of ingredients:
  bacon                Lovely bacon
  egg                  The runny kind
  sausage              Just a roll
  spam                 Glorious SPAM
  tomato               Sliced and diced

应该注意的是,这是唯一不打印“成分”本身的帮助行,而只打印选择的方法。

尽管如此,我不喜欢这种方法:它重新实现了太多代码,并且依赖于 argparse 太多的内部实现细节。

还有一种可能的混合方法:子解析器代码argparser使用财产action._choices_actions。这通常是在_SubParsersAction类,用于解析和格式化。如果我们使用此属性但仅用于格式化怎么办?

import argparse

class ChoicesAction(argparse._StoreAction):
    def __init__(self, **kwargs):
        super(ChoicesAction, self).__init__(**kwargs)
        if self.choices is None:
            self.choices = []
        self._choices_actions = []
    def add_choice(self, choice, help=''):
        self.choices.append(choice)
        # self.container.add_argument(choice, help=help, action='none')
        choice_action = argparse.Action(option_strings=[], dest=choice, help=help)
        self._choices_actions.append(choice_action)
    def _get_subactions(self):
        return self._choices_actions

parser = argparse.ArgumentParser()
parser.register('action', 'store_choice', ChoicesAction)

parser.add_argument('--customer', default='Mr. and Mrs. Bun', action='store',
                 help='salutation for addressing the customer')

group = parser.add_argument_group(title='your choice of ingredients')
ingredients = group.add_argument('ingredients', nargs='*', metavar='INGREDIENT',
                 action='store_choice')
ingredients.add_choice('bacon', help="Lovely bacon")
ingredients.add_choice('egg', help="The runny kind")
ingredients.add_choice('sausage', help="Just a roll")
ingredients.add_choice('spam', help="Glorious SPAM")
ingredients.add_choice('tomato', help="Sliced and diced")

options = parser.parse_args('--customer=Vikings egg sausage bacon'.split())
print("Dear {}, we are happy to serve you {}" \
      .format(options.customer, ', '.join(options.ingredients)))

print()
options = parser.parse_args(['--help'])

其输出:

Dear Vikings, we are happy to serve you egg, sausage, bacon

usage: helpformatter2.py [-h] [--customer CUSTOMER]
                         [INGREDIENT [INGREDIENT ...]]

optional arguments:
  -h, --help           show this help message and exit
  --customer CUSTOMER  salutation for addressing the customer

your choice of ingredients:
  INGREDIENT
    bacon              Lovely bacon
    egg                The runny kind
    sausage            Just a roll
    spam               Glorious SPAM
    tomato             Sliced and diced

这也是一个很好的解决方案,尽管它依赖于实现细节_get_subactions() method.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python argparse:列出用法中的各个选择 的相关文章

随机推荐

  • 大型项目没有 PHP?为什么不? [关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 我读过一些帖子 人们指出 没有建议 没有讨论 没有提供 PHP 不应该用于大型项目 作为一名主要的 PHP 开发人员 我问两个问题 大型项目 的
  • 带有 pod 标签的 Prometheus kubelet 指标 [关闭]

    Closed 这个问题是与编程或软件开发无关 help closed questions 目前不接受答案 我想弄清楚如何使用 prometheus stack 将 pod 标签从 kubelet 指标获取到指标标签 在我们的环境中 我们需要
  • 在 C 中调用函数时的序列点和未定义/未指定的行为

    我试图确定我对 C 中序列点的理解 只是想检查一些东西 目前 我认为 1 是未定义的 而 2 只是未指定的 因为在 2 中 在评估参数后存在序列点g and h 所以我们不修改i序列点之间两次 但是参数的求值顺序f仍然未指定 我的理解正确吗
  • 为什么我的 Azure 存储帐户上的队列域丢失?

    我已使用以下设置在 Azure 上成功创建存储帐户 部署 资源管理器 类型 通用 标准 复制 ZRS 在 Azure 门户上 我可以看到 Blob 服务 如果单击它 我可以在 blob 域下创建 blob 容器 https 帐户名称 blo
  • Javascript字典性能问题

    现在我有以下 javascript 字典 var a a SVG343 1942 a UAL534 2153 右边的数字代表时间 键是唯一的 ID 我想让 id 成为键 因为它们是唯一的 我的问题是给出一个时间找到对应的id 我要如何做到这
  • 将外部域名连接到AWS S3网站

    我想在 Amazon S3 上托管一个静态网站 创建了相关的存储桶 测试它们正常 现在我已经在 EuroDNS 注册了一个域名 www gopropel io 我找不到如何将其连接到我的 AWS S3 存储桶 我需要创建路由 53 托管区域
  • Flutter Firebase Messaging 无法创建服务 io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService

    我正在尝试在 Flutter 应用程序中使用 Firebase Messaging 我遵循包装上的所有说明 在我的 pubspec yaml 中 我有这些包 dependencies flutter sdk flutter firebase
  • 当元素的父元素溢出时,使元素宽度拉伸以适合其子元素:auto;

    在这个简化的示例中 我有一个书柜 书架上放着书籍 书架是具有定义宽度的最外面的元素 书架上的书应该从左到右排列 没有包装 书架应该拉伸其宽度以显示书架上的所有书籍 所有书架的宽度必须相同 即最宽书架的宽度 My HTML div class
  • 保护 Raven 数据库的安全

    我正在尝试将 RavenDB 的访问权限限制为只有一名用户 更改设置以保护数据库后 我仍然可以访问 RavenDB 管理工作室 但我不确定为什么 我将 RavenDB 作为 Windows 服务运行 并且使用构建 573 这是我的 Rave
  • 如何向 Skype 聊天室发出 Jenkins 构建状态警报?

    我们公司使用 Skype 进行通信 我希望能够在 Jenkins 构建失败 以及恢复时 时向 Skype 聊天室发送警报 我怎样才能做到这一点 我已经使用了Skype 公共 API http developer skype com acce
  • C++ 和 Java 之间的低延迟 IPC

    对于以下情况 实现 C Java IPC 的最佳方法是什么 最近有人问类似的问题 https stackoverflow com questions 5900887 ipc between java and c applications 但
  • 将非数组变量的地址传递给声明为“Type ptr[static 1]”的函数参数是否有效?

    如上所述here https stackoverflow com questions 3430315 what is the purpose of static keyword in array parameter of function
  • ESLint 显示括号中的错误

    我的 javascript 代码工作正常 除了 ESLint 显示我有错误 例如 错误 myFunction 已定义但从未使用 no unused vars and 错误 文档 未定义 no undef 这只是一个问题 因为我使用的是外部
  • 如何设置单个单元格而不是事件的背景颜色?

    我不想为圣诞节和复活节等创建事件 而是希望能够为受影响的日期单元格着色 甚至可能为每个事件提供灰色半透明文本 在 FullCalendar 中有什么简单的方法可以做到这一点吗 EDIT 有人向我指出 fc state highlight 用
  • Mysql从管道导入

    我试图弄清楚如何将输出通过管道传输到 mysqlimport 中 但没有任何运气 我有一个巨大的文件 250 GB 我想在处理它后通过管道传输到 mysqlimport 我不想创建中间文件 表 我正在想象这样的事情 猫基因组 mpileup
  • C# 和 Visual Studio 2005 中的程序集之间的循环引用

    我正在努力标准化所有应用程序的分层 多层设计的单一方式 我正在尝试将我的所有应用程序设为 5 层 Code 用户界面 业务对象 或映射器 数据访问 关系型数据库管理系统 假设我正在开发一个具有用户登录 注销功能的应用程序 我正在 VS200
  • Pandas:根据特定列的值计数选择行

    从 panda 数据框中选择所有行的最简单方法是什么 谁的 sym 在整个表中恰好出现两次 例如 在下表中 我想选择 b e 中包含 sym 的所有行 因为这些符号的 value counts 等于 2 df pd DataFrame sy
  • 在 python 中执行 bash 脚本

    我创建了一个 Expect 脚本 执行时 它会通过 ssh 连接到服务器并执行一系列命令 伪代码如下所示 usr bin expect spawn ssh usr myip expect password send mypassword n
  • Web Workers 是沙箱不受信任的 JavaScript 代码的安全方法吗

    我想知道网络工作者是否可以安全地沙箱不受信任的 JavaScript 代码 举例来说 在绘图应用程序的上下文中 开发人员可以实现新的绘图工具 您可以将他们的代码放入 webworker 中 并且每当用户单击画布时 向他们发送一条包含光标位置
  • Python argparse:列出用法中的各个选择

    我有一个需要多个参数的程序 例如 breakfast py customer vikings eggs sausage bacon 其中 鸡蛋 香肠 和 培根 可以从特定选项列表中指定 现在我喜欢的输出breakfast py help看起