完全定制的Python帮助Usag

2024-04-19 05:17:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用Python创建一个完全定制的“help”用法(我计划将其导入到许多我希望保持样式一致性的程序中),但是遇到了一些麻烦。你知道吗

  1. 我不知道为什么我的描述忽略了新词。。。 尝试了“”和“”
  2. 我不能让“…ARGS”行的“/”换行之后出现“:”,显然它们在自己的行上看起来很奇怪,而且
  3. 我不知道如何在结尾加上换行符。请帮忙??你知道吗

下面是我现在得到的一个示例:

nameconstant version 1.0

USAGE:  myprog.py [constant text] <input.ext> <input.ext>

several lines of text describing my program.. because it will be necessary

___________________
COMPULSORY ARGS:
  input.ext
  output.ext

___________________
OPTIONAL ARGS:
  -v, --verbose  print debugging messages to terminal
  -h, --help

这就是我想要的样子:

nameconstant version 1.0

USAGE:  myprog.py [constant text] <input.ext> <input.ext>

several lines of text
describing my program..

because it will be necessary

___________________
COMPULSORY ARGS:

  input.ext
  output.ext

___________________
OPTIONAL ARGS:

  -v, --verbose      print debugging messages to terminal
  -h, --help
\n - not visible!

以下是我目前正在使用的代码:

#!/usr/bin/env python
import argparse
import sys 
from os import path
version = "1.0"

prog = path.basename(sys.argv[0])

class USAGEformat(argparse.HelpFormatter):
        def add_usage(self, usage, actions, groups, prefix=None):
                if prefix is None:
                        prefix = 'nameconstant version '+ version+'\n\nUSAGE:  '+prog
                return super(USAGEformat, self).add_usage(
                      usage, actions, groups, prefix)

parser = argparse.ArgumentParser(add_help=False, formatter_class=USAGEformat, description='several lines of text\ndescribing my program..\n\nb
ecause it will be necessary', usage=' [optional args] <INPUT.ext> <OUTPUT.ext>')

parser._positionals.title = '___________________\nCOMPULSORY ARGS'
parser._optionals.title = '___________________\nOPTIONAL ARGS'

parser.add_argument('input', metavar="input.ext", type=argparse.FileType('rt'))
parser.add_argument('output', metavar="output.ext", type=argparse.FileType('wt'))
parser.add_argument('-v', '--verbose', action='store_true', default=False, help='print debugging messages to terminal')
parser.add_argument('-h', '--help', action='help', default=argparse.SUPPRESS)

下一步我应该做些什么??谢谢您!非常感谢您的帮助。。我对Python还不熟悉。你知道吗


Tags: textaddparserinputoutputprefixversionargparse
1条回答
网友
1楼 · 发布于 2024-04-19 05:17:34
import argparse
version = "1.0"

class USAGEformat(argparse.RawDescriptionHelpFormatter):
    def add_usage(self, usage, actions, groups, prefix=None):
        if prefix is None:
            prefix = 'nameconstant version '+ version+' USAGE:  '
            return super(USAGEformat, self).add_usage(
                usage, actions, groups, prefix)

parser = argparse.ArgumentParser(add_help=False, formatter_class=USAGEformat,
   description=\
'''several lines of text
describing my program..

because it will be necessary''')

gp1 = parser.add_argument_group('___________________\nCOMPULSORY ARGS')
gp1.add_argument('input', metavar="input.ext", type=argparse.FileType('rt'))
gp1.add_argument('output', metavar="output.ext", type=argparse.FileType('wt'))

gp2 = parser.add_argument_group('___________________\nOPTIONAL ARGS')
gp2.add_argument('-v', ' verbose', action='store_true', default=False,
      help='print debugging messages to terminal')
gp2.add_argument('-h', ' help', action='help', default=argparse.SUPPRESS)

parser.print_help()

产生

2156:~/mypy$ python stack47118098.py 
nameconstant version 1.0 USAGE:  stack47118098.py [-v] [-h]
                                                  input.ext output.ext

several lines of text
describing my program..

because it will be necessary

___________________
COMPULSORY ARGS:
  input.ext
  output.ext

___________________
OPTIONAL ARGS:
  -v,  verbose  print debugging messages to terminal
  -h,  help

RawDescriptionHelpFormatter保留描述的格式。你知道吗

我使用'''...'''作为描述,只是为了让代码看起来更好;这并不重要。你知道吗

我用argument_groups替换了现有组的重命名。你的方式很好,但我认为我的方式是开发者想要的。你知道吗

HelpFormatter格式化不同的片段,在它们之间大量使用\n,最后剥离出重复的片段(包括结尾)。所以我们必须确定并修改相关的方法(format_help)。你知道吗

自从我开始回答后,你在usage行上做了一些更改。你知道吗

我同意这样的评论,即按惯例显示必需的参数,而不使用[]。你知道吗

我使用这个用法是因为带前缀的行太长了。因此,它将其拆分,并将位置放在第二行,与可选位置对齐:

nameconstant version 1.0 USAGE:  stack47118098.py [-v] [-h]
                                                  input.ext output.ext

如果我设置

prefix = 'USAGE:  '

那么用法就是

USAGE:  stack47118098.py [-v] [-h] input.ext output.ext

我们得看看它是如何包装和缩进的,以使使用符合您的规格。你知道吗

编辑

importing custom python modules.. why do only some elements carry over?

我解释说,每次您请求帮助或用法时,都会重新创建帮助格式化程序。定义parser设置格式化程序类,但不创建格式化程序。这意味着格式化程序类使用的任何全局变量都将作为运行时而不是安装时获取其值。你知道吗

例如,version是全局的(对于模块)。它最初是“1.0”,这是出现在用法中的值。但如果我在上面的脚本末尾加上:

version = '2.0'
parser.print_help()

用法行更改为:

nameconstant version 2.0 USAGE:  stack47118098.py [-v] [-h]
                                                  input.ext output.ext

如果导入此脚本时使用:

import stack47118098 as pp

print('===========')
print(pp.version)
print(pp.parser)
pp.parser.print_help()

pp.version = '3.0'
pp.parser.print_help()

第一个帮助(导入后)使用文件中的“2.0”版本。但是第二个帮助使用新定义的version。你知道吗

要更改解析器的description之类的内容,必须使用

pp.parser.description = 'New short description'

也就是说,我正在修改现有对象的属性。你知道吗

相关问题 更多 >