按字母顺序对 argparse 帮助进行排序 [英] Sort argparse help alphabetically

查看:26
本文介绍了按字母顺序对 argparse 帮助进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Python (2.7) 的 argparse 工具,并希望按选项按字母顺序自动对它生成的帮助进行排序.

I am using Python's (2.7) argparse facility and would like to automatically sort the help it produces alphabetically by option.

默认情况下,帮助条目按添加顺序排序*,如下所示:

By default help entries are sorted in the order they are added*, as in:

p = argparse.ArgumentParser(description='Load duration curves and other plots')
p.add_argument('--first', '-f', type=int, default=1, help='First Hour')
p.add_argument('--dur', '-d', type=int, default=-1, help='Duration in Hours. Use -1 for all')
p.add_argument('--title', '-t', help='Plot Title (for all plots), default=file name')
p.add_argument('--interp', '-i', action="store_true", default=True, 
                help='Use linear interpolation for smoother curves')
...
args = p.parse_args()

当被调用为 python script -h 时会产生:

Which when called as python script -h produces:

usage: script.py [-h] [--first FIRST] [--dur DUR] [--title TITLE] [--interp]

Load duration curves and other plots

optional arguments:
  -h, --help            show this help message and exit
  --first FIRST, -f FIRST
                        First Hour
  --dur DUR, -d DUR     Duration in Hours. Use -1 for all
  --title TITLE, -t TITLE
                        Plot Title (for all plots), default=file name
  --interp, -i          Use linear interpolation for smoother curves

是否可以按字母顺序自动排序?这将是 dur、first、h、interp、title.

Is it possible to automatically sort them alphabetically instead? This would be dur, first, h, interp, title.

*显然,解决方法是通过使用 p.add_argument 按字母顺序添加条目来手动维护,但我试图避免这样做.

*Obviously the work around is to manually maintain by adding entries using p.add_argument in alphabetical added order but I am trying to avoid doing so.

推荐答案

您可以通过提供自定义的HelpFormatter;其内部结构未正式记录.这意味着在 Python 版本与版本之间的兼容性方面您是靠自己的,但我发现界面非常稳定:

You can do this by providing a custom HelpFormatter class; the internals of which are officially undocumented. This means you are on your own when it comes to compatibility from Python version to version, but I find the interface quite stable:

from argparse import HelpFormatter
from operator import attrgetter

class SortingHelpFormatter(HelpFormatter):
    def add_arguments(self, actions):
        actions = sorted(actions, key=attrgetter('option_strings'))
        super(SortingHelpFormatter, self).add_arguments(actions)


p = argparse.ArgumentParser(...
    formatter_class=SortingHelpFormatter,
)

这里我对选项字符串进行排序(('--dur', '-d') 等),但您可以选择要排序的内容.这个简单的排序选项将单破折号选项放在最后,就像 -h 选项一样.

Here I sort on the option strings (('--dur', '-d'), etc.), but you could take your pick as to what you want to sort on. This simple sorting option puts the single-dash options last, like the -h option.

输出:

usage: [-h] [--first FIRST] [--dur DUR] [--title TITLE] [--interp]

Load duration curves and other plots

optional arguments:
  --dur DUR, -d DUR     Duration in Hours. Use -1 for all
  --first FIRST, -f FIRST
                        First Hour
  --interp, -i          Use linear interpolation for smoother curves
  --title TITLE, -t TITLE
                        Plot Title (for all plots), default=file name
  -h, --help            show this help message and exit

这篇关于按字母顺序对 argparse 帮助进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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