Sort argparse help alphabetically
This is similar to @mgilson's answer. I thought I had posted this earlier, but apparently not.
d = dict()
d['--first'] = ('-f', "type=int", "default=1", "help='First Hour'")
d['--dur'] = ('-d', type=int, default=-1, help='Duration in Hours. Use -1 for all')
# etc
for prim_option in sorted(d):
p.add_arguments(prim_option, *d[prim_option])
You can adjust what exactly is used as the key in the dictionary, as well as the arguments to sorted
and the exact structure of the call to add_arguments
, to get the desired sorting order. This adheres to the publicly documented interface of argparse
, but does add a layer to the process of defining your parser. (Depending on your philosophy, such separation of information about the options from the implementation of the parser might be a good thing.)
When you create the ArgumentParser class you can pass in a help formatter: http://docs.python.org/library/argparse.html#formatter-class
So apparently you can use one of the supplied formatters, but cannot override and replace them without reverse engineering a bit:
>>> h = argparse.ArgumentDefaultsHelpFormatter
>>> print h.__doc__
Help message formatter which adds default values to argument help.
Only the name of this class is considered a public API. All the methods
provided by the class are considered an implementation detail.
An alternative, definitely more ugly way to do it than proposed by @MartijnPieters:
p = argparse.ArgumentParser()
#add arguements here
for g in p._action_groups:
g._group_actions.sort(key=lambda x:x.dest)
It may be nice to put this in a try
/except
clause as it's only formatting help, therefore it shouldn't really matter for the execution of the program if this piece of code fails on an AttributeError
or something ...
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,
)
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.
which outputs:
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