Sortuj pomoc argparse alfabetycznie
Używam obiektu argparse Pythona (2.7) i chciałbym automatycznie sortować pomoc, którą tworzy alfabetycznie według opcji.
Domyślnie wpisy pomocy są sortowane w kolejności, w jakiej zostały dodane *, jak w:
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()
Który kiedy nazywa się jakopython script -h
produkuje:
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
Czy można zamiast tego automatycznie sortować je alfabetycznie? Byłby to tytuł „pierwszy”, „h”, „interp”.
* Oczywiście obejście polega na ręcznym utrzymaniu przez dodanie wpisów przy użyciu p.add_argument w kolejności alfabetycznej, ale staram się tego nie robić.