问题描述
我正在使用argparse作为参数,我有许多argparse语句.我希望在输出中不应该打印大写字母DELETE或将它们对齐.在我的另一个argparse案例中,大写单词未在单个列中对齐.
I am using argparse for arguments, I have numbers of argparse statements. I want in the output the capital DELETE should not be print or they should be aligned.In my case for another argparse the capital words are not aligned in a single column.
parser = argparse.ArgumentParser()
parser.add_argument( '-del' ,action='store' ,dest='delete' , help="Del a POX"
parser.add_argument( '-a' ,action='store' ,dest='add' , help="add a POX"
return parser
python myscript.h -h
-del DELETE Del a POX
-a Add add a POX
推荐答案
使用您的参数,我得到:
With your parameters I get:
In [417]: parser=argparse.ArgumentParser()
In [418]: a1=parser.add_argument('-del',dest='delete', help='help')
In [419]: a2=parser.add_argument('-a',dest='add', help='help')
In [420]: parser.print_help()
usage: ipython3 [-h] [-del DELETE] [-a ADD]
optional arguments:
-h, --help show this help message and exit
-del DELETE help
-a ADD help
DELETE
和ADD
是metavar,是将跟随该标志的参数的代表.在常规帮助显示中,它们紧随标志-a ADD
之后.我不知道是什么在'-a Add'中产生了额外的空间.
The DELETE
and ADD
are metavars, standins for the argument that will follow the flag. In the normal help display they follow immediately after the flag, -a ADD
. I don't know what is producing the extra space in '-a Add '.
我本可以通过以下方式设置您的论点
I would have set up your arguments with:
In [421]: parser=argparse.ArgumentParser()
In [422]: a1=parser.add_argument('-d','--delete', help='help')
In [423]: a2=parser.add_argument('-a','--add', help='help')
In [424]: parser.print_help()
usage: ipython3 [-h] [-d DELETE] [-a ADD]
optional arguments:
-h, --help show this help message and exit
-d DELETE, --delete DELETE
help
-a ADD, --add ADD help
并使用metavar
参数,此处为空字符串:
And with the metavar
parameter, here an empty string:
In [425]: parser=argparse.ArgumentParser()
In [426]: a1=parser.add_argument('-d','--delete', metavar='', help='help')
In [427]: a2=parser.add_argument('-a','--add', metavar='', help='help')
In [428]: parser.print_help()
usage: ipython3 [-h] [-d] [-a]
optional arguments:
-h, --help show this help message and exit
-d , --delete help
-a , --add help
dest
通常是从第一个--
标志字符串推导出的;但是可以像您所做的那样进行显式设置. metavar
是从dest
派生的-通常是大写的-实际上我不知道是什么产生了Add
而不是ADD
.
dest
is normally deduced from the first --
flag string; but can, as you do, be set explicitly. metavar
is derived from the dest
- usually upper cased - in fact I don't know what produces the Add
instead of ADD
.
它对齐该行的help
部分,但不对齐Matavar部分.
It aligns the help
portion of the line, but does not align the matavar part.
这篇关于稀疏输出未对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!