gpt4 book ai didi

python - argparse 子解析器的帮助中缺少字符

转载 作者:太空宇宙 更新时间:2023-11-03 21:43:05 25 4
gpt4 key购买 nike

import argparse

parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter)
commands = parser.add_subparsers(title='Available Commands')

cmd1_parser = commands.add_parser('setup1', help="Setup Device 1")
cmd1_parser.add_argument('-f', "--freq", help="Frequency in (Hz)", metavar='\b', type=int, required=True)
cmd1_parser.add_argument('-p', "--power", help="Power level in (dB)", metavar='\b', type=int, required=True)
cmd1_parser.add_argument('-c', "--current", help="Current (mA)", metavar='\b', type=int, required=True)

cmd2_parser = commands.add_parser('setup2', help="Setup Device 2")
cmd2_parser.add_argument('-l', "--length", help="Length in meters", metavar='\b', type=int, required=True)
cmd2_parser.add_argument('-b', "--breadth", help="Breadth in meters", metavar='\b', type=int, required=True)
cmd2_parser.add_argument('-w', "--width", help="Width in meters", metavar='\b', type=int, required=True)

parser.print_help()
cmd1_parser.print_help()
cmd2_parser.print_help()

此帮助输出的结果:

usage: test2.py [-h] {setup1,setup2} ...

optional arguments:
-h, --help show this help message and exit

Available Commands:
{setup1,setup2}
setup1 Setup Device 1
setup2 Setup Device 2
usage: test2.py setup1 [-h] -f -p -c

optional arguments:
-h, --help show this help message and exit
-f, --freq Frequency in (Hz)
-, --power Power level in (dB)
-c, --current Current (mA)
usage: test2.py setup2 [-h] -l -b -w

optional arguments:
-h, --help show this help message and exit
-l, --length Length in meters
-, --breadth Breadth in meters
-w, --width Width in meters

在 Windows 命令提示符中,“power”和“breadth”分别缺少字符“p”和“b”。然而,使用 PyCharm 获得正确的结果。在 Google 上搜索但找不到任何解决方案。有人可以告诉我如何解决这个问题吗?

最佳答案

更新:

作为你的新要求,查看add_argmument()提供的选项,没有办法做到这一点。我能想到的最合适的是:

cmd1_parser = commands.add_parser('setup1', help="Setup Device 1")
cmd1_parser.add_argument('-f', '--freq', help="Frequency in (Hz)", metavar='(Hz)',type=int, required=True)
cmd1_parser.add_argument('-p', "--power", help="Power level in (dB)", metavar='(dB)', type=int, required=True)
cmd1_parser.add_argument('-c', "--current", help="Current (mA)", metavar='(mA)', required=True)

输出是:

optional arguments:
-h, --help show this help message and exit
-f (Hz), --freq (Hz) Frequency in (Hz)
-p (dB), --power (dB)
Power level in (dB)
-c (mA), --current (mA)
Current (mA)

====================

旧答案:

只需将 metavar='\b' 替换为 metavar='' 即可。 '\b' 是一个退格键,在打印选项字符串后,python 将“打印”元变量(该参数的一种名称),因此它将调用退格键并删除您的字符。

这是使用上述操作后的输出:

>>> parser.print_help()
usage: [-h] {setup1,setup2} ...

optional arguments:
-h, --help show this help message and exit

Available Commands:
{setup1,setup2}
setup1 Setup Device 1
setup2 Setup Device 2
>>> cmd1_parser.print_help()
usage: setup1 [-h] -f -p -c

optional arguments:
-h, --help show this help message and exit
-f , --freq Frequency in (Hz)
-p , --power Power level in (dB)
-c , --current Current (mA)
>>> cmd2_parser.print_help()
usage: setup2 [-h] -l -b -w

optional arguments:
-h, --help show this help message and exit
-l , --length Length in meters
-b , --breadth Breadth in meters
-w , --width Width in meters

关于python - argparse 子解析器的帮助中缺少字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52698258/

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