arparse 输出未对齐 [英] arparse output not aligned

查看:22
本文介绍了arparse 输出未对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 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

推荐答案

根据你的参数我得到:

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

DELETEADD 是元变量,代表跟随标志的参数.在正常的帮助显示中,它们紧跟在标志之后,-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 '.

我会用:

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.

这篇关于arparse 输出未对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆