[英]Customize argparse help message
我編寫了以下示例代碼來演示我的問題。
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-v', '--version', action='version',
version='%(prog)s 1.0')
parser.parse_args()
這會產生以下幫助消息。
$ python foo.py --help
usage: foo.py [-h] [-v]
optional arguments:
-h, --help show this help message and exit
-v, --version show program's version number and exit
我想自定義此幫助輸出,使其大寫所有短語和句子,並在句子后放置句號。 換句話說,我希望像這樣生成幫助消息。
$ python foo.py --help
Usage: foo.py [-h] [-v]
Optional arguments:
-h, --help Show this help message and exit.
-v, --version Show program's version number and exit.
這是我可以使用 argparse API 控制的東西嗎? 如果是這樣,如何? 你能否提供一個小例子來說明如何做到這一點?
首先:大寫這些短語與慣例argparse
,並且argparse
並沒有真正幫助您輕松更改這些字符串。 這里有三類不同的字符串:來自幫助格式化程序的樣板文本、章節標題和每個特定選項的幫助文本。 所有這些字符串都是可本地化的; 您可以通過gettext()
模塊支持為所有這些字符串提供“大寫”翻譯。 也就是說,如果您有足夠的決心並稍微閱讀源代碼,則可以訪問並替換所有這些字符串。
version
操作包括默認help
文本,但您可以通過設置help
參數來提供自己的help
文本。 這同樣適用於help
操作; 如果將add_help
參數設置為False
,則可以手動添加該操作:
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument('-v', '--version', action='version',
version='%(prog)s 1.0', help="Show program's version number and exit.")
parser.add_argument('-h', '--help', action='help', default=argparse.SUPPRESS,
help='Show this help message and exit.')
接下來, optional arguments
message 是組標題; 每個解析器都有兩個默認組,一個用於位置參數,另一個用於可選。 您可以通過屬性_positionals
和_optionals
來訪問它們,這兩個屬性都有一個title
屬性:
parser._positionals.title = 'Positional arguments'
parser._optionals.title = 'Optional arguments'
請注意,通過訪問以下划線開頭的名稱,您正在冒險進入模塊的未記錄的私有 API,並且您的代碼可能會在未來的更新中中斷。
最后,要更改usage
字符串,您必須對幫助格式化程序進行子類化; 將子類作為formatter_class
參數傳入:
class CapitalisedHelpFormatter(argparse.HelpFormatter):
def add_usage(self, usage, actions, groups, prefix=None):
if prefix is None:
prefix = 'Usage: '
return super(CapitalisedHelpFormatter, self).add_usage(
usage, actions, groups, prefix)
parser = argparse.ArgumentParser(formatter_class=CapitalisedHelpFormatter)
演示,將所有這些放在一起:
>>> import argparse
>>> class CapitalisedHelpFormatter(argparse.HelpFormatter):
... def add_usage(self, usage, actions, groups, prefix=None):
... if prefix is None:
... prefix = 'Usage: '
... return super(CapitalisedHelpFormatter, self).add_usage(
... usage, actions, groups, prefix)
...
>>> parser = argparse.ArgumentParser(add_help=False, formatter_class=CapitalisedHelpFormatter)
>>> parser._positionals.title = 'Positional arguments'
>>> parser._optionals.title = 'Optional arguments'
>>> parser.add_argument('-v', '--version', action='version',
... version='%(prog)s 1.0', help="Show program's version number and exit.")
_VersionAction(option_strings=['-v', '--version'], dest='version', nargs=0, const=None, default='==SUPPRESS==', type=None, choices=None, help="Show program's version number and exit.", metavar=None)
>>> parser.add_argument('-h', '--help', action='help', default=argparse.SUPPRESS,
... help='Show this help message and exit.')
_HelpAction(option_strings=['-h', '--help'], dest='help', nargs=0, const=None, default='==SUPPRESS==', type=None, choices=None, help='Show this help message and exit.', metavar=None)
>>> print(parser.format_help())
Usage: [-v] [-h]
Optional arguments:
-v, --version Show program's version number and exit.
-h, --help Show this help message and exit.
編輯:如果此后顯着擴展了這一點,並將繼續在GitHub 上這樣做。 為了保持一致性,我將原樣保留我的答案。
與其依賴於內部 API(如有更改,恕不另行通知),這里提供了一種僅使用公共 API 的替代方法。 它可以說更復雜,但反過來又可以讓您最大程度地控制打印的內容:
class ArgumentParser(argparse.ArgumentParser):
def __init__(self, *args, **kwargs):
super(ArgumentParser, self).__init__(*args, **kwargs)
self.program = { key: kwargs[key] for key in kwargs }
self.options = []
def add_argument(self, *args, **kwargs):
super(ArgumentParser, self).add_argument(*args, **kwargs)
option = {}
option["flags"] = [ item for item in args ]
for key in kwargs:
option[key] = kwargs[key]
self.options.append(option)
def print_help(self):
# use self.program/self.options to produce custom help text
這個怎么運作:
argparse.ArgumentParser
構造函數來捕獲和存儲程序信息(例如描述、用法)argparse.ArgumentParser.add_argument()
以捕獲和存儲添加的參數(例如標志、幫助、默認值)argparse.ArgumentParser.print_help()
並使用先前存儲的程序信息/參數來生成幫助文本這是一個涵蓋一些常見用例的完整示例。 請注意,它絕不是完整的(例如,不支持位置參數或具有多個參數的選項),但應該提供可能的印象:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import sys
import argparse
import textwrap
class ArgumentParser(argparse.ArgumentParser):
def __init__(self, *args, **kwargs):
super(ArgumentParser, self).__init__(*args, **kwargs)
self.program = { key: kwargs[key] for key in kwargs }
self.options = []
def add_argument(self, *args, **kwargs):
super(ArgumentParser, self).add_argument(*args, **kwargs)
option = {}
option["flags"] = [ item for item in args ]
for key in kwargs:
option[key] = kwargs[key]
self.options.append(option)
def print_help(self):
wrapper = textwrap.TextWrapper(width=80)
# Print usage
if "usage" in self.program:
print("Usage: %s" % self.program["usage"])
else:
usage = []
for option in self.options:
usage += [ "[%s %s]" % (item, option["metavar"]) if "metavar" in option else "[%s %s]" % (item, option["dest"].upper()) if "dest" in option else "[%s]" % item for item in option["flags"] ]
wrapper.initial_indent = "Usage: %s " % os.path.basename(sys.argv[0])
wrapper.subsequent_indent = len(wrapper.initial_indent) * " "
output = str.join(" ", usage)
output = wrapper.fill(output)
print(output)
print()
# Print description
if "description" in self.program:
print(self.program["description"])
print()
# Print options
print("Options:")
maxlen = 0
for option in self.options:
option["flags2"] = str.join(", ", [ "%s %s" % (item, option["metavar"]) if "metavar" in option else "%s %s" % (item, option["dest"].upper()) if "dest" in option else item for item in option["flags"] ])
if len(option["flags2"]) > maxlen:
maxlen = len(option["flags2"])
for option in self.options:
template = " %-" + str(maxlen) + "s "
wrapper.initial_indent = template % option["flags2"]
wrapper.subsequent_indent = len(wrapper.initial_indent) * " "
if "help" in option and "default" in option:
output = option["help"]
output += " (default: '%s')" % option["default"] if isinstance(option["default"], str) else " (default: %s)" % str(option["default"])
output = wrapper.fill(output)
elif "help" in option:
output = option["help"]
output = wrapper.fill(output)
elif "default" in option:
output = "Default: '%s'" % option["default"] if isinstance(option["default"], str) else "Default: %s" % str(option["default"])
output = wrapper.fill(output)
else:
output = wrapper.initial_indent
print(output)
# Main
if (__name__ == "__main__"):
#parser = argparse.ArgumentParser(description="Download program based on some library.", argument_default=argparse.SUPPRESS, allow_abbrev=False, add_help=False)
#parser = argparse.ArgumentParser(usage="%s [OPTION]..." % os.path.basename(sys.argv[0]), description="Download program based on some library.", argument_default=argparse.SUPPRESS, allow_abbrev=False, add_help=False)
#parser = ArgumentParser(usage="%s [OPTION]..." % os.path.basename(sys.argv[0]), description="Download program based on some library.", argument_default=argparse.SUPPRESS, allow_abbrev=False, add_help=False)
parser = ArgumentParser(description="Download program based on some library.", argument_default=argparse.SUPPRESS, allow_abbrev=False, add_help=False)
parser.add_argument("-c", "--config-file", action="store", dest="config_file", metavar="file", type=str, default="config.ini")
parser.add_argument("-d", "--database-file", action="store", dest="database_file", metavar="file", type=str, help="SQLite3 database file to read/write", default="database.db")
parser.add_argument("-l", "--log-file", action="store", dest="log_file", metavar="file", type=str, help="File to write log to", default="debug.log")
parser.add_argument("-f", "--data-file", action="store", dest="data_file", metavar="file", type=str, help="Data file to read", default="data.bin")
parser.add_argument("-t", "--threads", action="store", dest="threads", type=int, help="Number of threads to spawn", default=3)
parser.add_argument("-p", "--port", action="store", dest="port", type=int, help="TCP port to listen on for access to the web interface", default="12345")
parser.add_argument("--max-downloads", action="store", dest="max_downloads", metavar="value", type=int, help="Maximum number of concurrent downloads", default=5)
parser.add_argument("--download-timeout", action="store", dest="download_timeout", metavar="value", type=int, help="Download timeout in seconds", default=120)
parser.add_argument("--max-requests", action="store", dest="max_requests", metavar="value", type=int, help="Maximum number of concurrent requests", default=10)
parser.add_argument("--request-timeout", action="store", dest="request_timeout", metavar="value", type=int, help="Request timeout in seconds", default=60)
parser.add_argument("--main-interval", action="store", dest="main_interval", metavar="value", type=int, help="Main loop interval in seconds", default=60)
parser.add_argument("--thread-interval", action="store", dest="thread_interval", metavar="value", type=int, help="Thread loop interval in milliseconds", default=500)
parser.add_argument("--console-output", action="store", dest="console_output", metavar="value", type=str.lower, choices=["stdout", "stderr"], help="Output to use for console", default="stdout")
parser.add_argument("--console-level", action="store", dest="console_level", metavar="value", type=str.lower, choices=["debug", "info", "warning", "error", "critical"], help="Log level to use for console", default="info")
parser.add_argument("--logfile-level", action="store", dest="logfile_level", metavar="value", type=str.lower, choices=["debug", "info", "warning", "error", "critical"], help="Log level to use for log file", default="info")
parser.add_argument("--console-color", action="store", dest="console_color", metavar="value", type=bool, help="Colorized console output", default=True)
parser.add_argument("--logfile-color", action="store", dest="logfile_color", metavar="value", type=bool, help="Colorized log file output", default=False)
parser.add_argument("--log-template", action="store", dest="log_template", metavar="value", type=str, help="Template to use for log lines", default="[%(created)d] [%(threadName)s] [%(levelname)s] %(message)s")
parser.add_argument("-h", "--help", action="help", help="Display this message")
args = parser.parse_args(["-h"])
產生的輸出:
Usage: argparse_custom_usage.py [-c file] [--config-file file] [-d file]
[--database-file file] [-l file] [--log-file
file] [-f file] [--data-file file] [-t THREADS]
[--threads THREADS] [-p PORT] [--port PORT]
[--max-downloads value] [--download-timeout
value] [--max-requests value] [--request-timeout
value] [--main-interval value] [--thread-
interval value] [--console-output value]
[--console-level value] [--logfile-level value]
[--console-color value] [--logfile-color value]
[--log-template value] [-h] [--help]
Download program based on some library.
Options:
-c file, --config-file file Default: 'config.ini'
-d file, --database-file file SQLite3 database file to read/write (default:
'database.db')
-l file, --log-file file File to write log to (default: 'debug.log')
-f file, --data-file file Data file to read (default: 'data.bin')
-t THREADS, --threads THREADS Number of threads to spawn (default: 3)
-p PORT, --port PORT TCP port to listen on for access to the web
interface (default: '12345')
--max-downloads value Maximum number of concurrent downloads
(default: 5)
--download-timeout value Download timeout in seconds (default: 120)
--max-requests value Maximum number of concurrent requests (default:
10)
--request-timeout value Request timeout in seconds (default: 60)
--main-interval value Main loop interval in seconds (default: 60)
--thread-interval value Thread loop interval in milliseconds (default:
500)
--console-output value Output to use for console (default: 'stdout')
--console-level value Log level to use for console (default: 'info')
--logfile-level value Log level to use for log file (default: 'info')
--console-color value Colorized console output (default: True)
--logfile-color value Colorized log file output (default: False)
--log-template value Template to use for log lines (default:
'[%(created)d] [%(threadName)s] [%(levelname)s]
%(message)s')
-h, --help Display this message
Martijn 給出了幾個想到的修復 - 提供help
參數和自定義 Formatter 類。
另一種部分修復是在創建參數后修改幫助字符串。 add_argument
創建並返回一個包含參數和默認值的Action
對象。 您可以保存指向此的鏈接,並修改Action
。 您還可以獲得這些操作的列表,並據此采取行動。
讓我舉例說明,對於帶有默認幫助和其他參數的簡單解析器,操作列表是:
In [1064]: parser._actions
Out[1064]:
[_HelpAction(option_strings=['-h', '--help'], dest='help', nargs=0, const=None, default='==SUPPRESS==', type=None, choices=None, help='show this help message and exit', metavar=None),
_StoreAction(option_strings=['-f', '--foo'], dest='foo', nargs=None, const=None, default=None, type=None, choices=None, help=None, metavar=None)]
我可以查看和修改其中任何一個的help
屬性:
In [1065]: parser._actions[0].help
Out[1065]: 'show this help message and exit'
In [1066]: parser._actions[0].help='Show this help message and exit.'
產生這個幫助:
In [1067]: parser.parse_args(['-h'])
usage: ipython3 [-h] [-f FOO]
optional arguments:
-h, --help Show this help message and exit.
-f FOO, --foo FOO
使用parser._actions
列表使用 'private' 屬性,有些人認為這是不明智的。 但是在 Python 中,公共/私有的區別並不嚴格,可以小心地打破。 Martijn 通過訪問parser._positionals.title
來做到這parser._positionals.title
。
更改該組標題的另一種方法是使用自定義參數組
ogroup=parser.add_argument_group('Correct Optionals Title')
ogroup.add_argument('-v',...)
ogroup.add_argument('-h',...)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.