【发布时间】:2023-03-23 08:03:01
【问题描述】:
使用 argparse,目前我的 main.py --help 看起来像这样
usage: main.py [--options] <command>
options:
-h, --help show this help message and exit
--debug Logs extra information for debugging
--version show program's version number and exit
--license reads the GPLv3
commands:
install install packages
remove remove packages
update update package list and upgrade the system
有没有一种简单的方法可以让子解析器命令显示在全局选项之前?
目前我的配置有点长,但如果需要上下文我可以。我没有什么疯狂的事情,这是重要的事情。
formatter = lambda prog: argparse.RawDescriptionHelpFormatter(prog,
max_help_position=64)
bin_name = Path(argv[0]).name
version = __version__
parser = nalaParser( formatter_class=formatter,
usage=f'{bin_name} [--options] <command>',
)
subparsers = parser.add_subparsers(title='commands', metavar='', dest='command')
parser._optionals.title = "options"
install_parser = subparsers.add_parser('install', help='install packages')
remove_parser = subparsers.add_parser('remove', help='remove packages')
update_parser = subparsers.add_parser('update', help='update package list and upgrade the system')
parser.add_argument('--debug', action='store_true', help='Logs extra information for debugging')
parser.add_argument('--version', action='version', version=f'{bin_name} {version}')
parser.add_argument('--license', action=GPLv3)
因为我已经在进行子类化,所以我只是在下面添加了这个方法到 nalaParser
def format_help(self):
formatter = self._get_formatter()
# usage
formatter.add_usage(self.usage, self._actions,
self._mutually_exclusive_groups)
# description
formatter.add_text(self.description)
index = -1
# Put options last in the group
for action_group in self._action_groups[:]:
index = + 1
if action_group.title == 'options':
self._action_groups.append(self._action_groups.pop(index))
# positionals, optionals and user-defined groups
for action_group in self._action_groups:
formatter.start_section(action_group.title)
formatter.add_text(action_group.description)
formatter.add_arguments(action_group._group_actions)
formatter.end_section()
# epilog
formatter.add_text(self.epilog)
# determine help from format above
return formatter.format_help()
【问题讨论】:
-
查看
format_help方法的代码。这就是设置格式化程序的原因,包括组。