问题描述
我对使用 argparse 的 ArgumentDefaultsHelpFormatter 类格式化程序感兴趣(我的程序有几个子命令).默认情况下,输入和输出参数分别设置为 sys.stdin 和 sys.stdout.但是,这两个参数的格式可能会让用户有点困惑(例如(默认:', mode 'r' at 0x10028e0c0>).有没有办法专门且轻松地更改这两个参数的输出格式以获得诸如默认:标准输入"或默认:标准输出"之类的东西?
I am interested in using the ArgumentDefaultsHelpFormatter class formatter of argparse (my program has several sub-commands). By default, the input and output arguments are set to sys.stdin and sys.stdout respectively. However, the formatting for these two arguments may be a little bit confusing for users (e.g (default: ', mode 'r' at 0x10028e0c0>). Is there a way to specifically and easily change the output format for these two arguments to get something like 'default: STDIN' or 'default: STDOUT'?
谢谢
import sys
import argparse
parser = argparse.ArgumentParser(prog='PROG',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--infile',
'-i',
metavar='File',
help='The input file/stream.',
default=sys.stdin,
type=argparse.FileType('r'),
required=False)
parser.add_argument('--outfile',
'-o',
metavar='File',
help='The output file/stream.',
default=sys.stdout,
type=argparse.FileType('r'),
required=False)
parser.add_argument('--whatever-arg',
'-w',
type=str,
default='any',
help='Change something',
required=False)
args = parser.parse_args()
parser.print_help()
给出:
usage: PROG [-h] [--infile File] [--outfile File]
[--whatever-arg WHATEVER_ARG]
optional arguments:
-h, --help show this help message and exit
--infile File, -i File
The input file/stream. (default: <open file '<stdin>',
mode 'r' at 0x10028e0c0>)
--outfile File, -o File
The output file/stream. (default: <open file
'<stdout>', mode 'w' at 0x10028e150>)
--whatever-arg WHATEVER_ARG, -w WHATEVER_ARG
Change something (default: any)
推荐答案
你可以子类化 ArgumentDefaultsHelpFormatter 来做你想做的事.
You can subclass ArgumentDefaultsHelpFormatter to do what you want.
from argparse import ArgumentDefaultsHelpFormatter,RawDescriptionHelpFormatter
class CustomFormatter(argparse.ArgumentDefaultsHelpFormatter, argparse.RawDescriptionHelpFormatter):
def _get_help_string(self, action):
help = action.help
if '%(default)' not in action.help:
if action.default is not argparse.SUPPRESS:
defaulting_nargs = [argparse.OPTIONAL, argparse.ZERO_OR_MORE]
if action.option_strings or action.nargs in defaulting_nargs:
if type(action.default) == type(sys.stdin):
print action.default.name
help += ' (default: ' + str(action.default.name) + ')'
else:
help += ' (default: %(default)s)'
return help
parser = argparse.ArgumentParser(prog='PROG', formatter_class=CustomFormatter)
对我来说结果是:
optional arguments:
-h, --help show this help message and exit
--infile File, -i File
The input file/stream. (default: <stdin>)
--outfile File, -o File
The output file/stream. (default: <stdout>)
--whatever-arg WHATEVER_ARG, -w WHATEVER_ARG
Change something (default: any)
这篇关于Argparse 和 ArgumentDefaultsHelpFormatter.选择 sys.stdin/stdout 作为默认值时的默认值格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!