gpt4 book ai didi

python-3.x - argparse - 没有前缀的可选参数

转载 作者:行者123 更新时间:2023-12-04 10:22:48 24 4
gpt4 key购买 nike

有关的:

Using argparse for mandatory argument without prefix

Python: argparse optional arguments without dashes

我需要一个简单的脚本来接受 ./x start./x stop .和 startstop应该在帮助菜单中有自己的条目。

这对于 argparse 似乎不是 super 可行的。

我接近

parser.add_argument('--start', action='store_true', help='stuff')
parser.add_argument('--stop', action='store_true', help='stuff2')

但是,这需要在每个参数之前使用破折号。

有没有办法在没有子命令的情况下做到这一点? (特别是因为 startstop 都接受相同的可选标志集)。

最佳答案

parser.add_argument('foobar', choices=['start', 'stop'], 
help='a positional that accepts one of two words')

关于python-3.x - argparse - 没有前缀的可选参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60768149/

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