gpt4 book ai didi

python - 使用 argparse 折叠多个选项

转载 作者:行者123 更新时间:2023-12-01 05:49:03 25 4
gpt4 key购买 nike

我正在寻找一种优雅的方式来折叠 bool 切换的帮助消息argparse。例如这个:

import argparse

parser = argparse.ArgumentParser("booleans")

parser.add_argument('--no-store', action='store_false',
help="Don't do it'")
parser.add_argument('--store', action='store_true',
help="Do it")

parser.print_help()

打印:

usage: booleans [-h] [--no-store] [--store]

optional arguments:
-h, --help show this help message and exit
--no-store Don't do it'
--store Do it

但是我有一大堆 bool 标志,我真正想要的是能够以使其打印的方式编写它:

usage: booleans [-h] [--[no-]store]

optional arguments:
-h, --help show this help message and exit
--[no-]store Do or don't do it.

有什么好的方法可以让我折叠参数并提供自定义帮助文本和选项名称?

最佳答案

您可以为 ArgumentParser 指定 usage 参数并编写自己的语法行。

关于python - 使用 argparse 折叠多个选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15072359/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com