Sortieren Sie die Argumenthilfe alphabetisch

Ich verwende die Argumentationsfunktion von Python (2.7) und möchte die von ihr erzeugte Hilfe automatisch alphabetisch nach Optionen sortieren.

Standardmäßig werden die Hilfeeinträge in der Reihenfolge sortiert, in der sie hinzugefügt wurden *, wie 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()

Was, wenn als aufgerufenpython script -h produziert:

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

Ist es möglich, sie stattdessen automatisch alphabetisch zu sortieren? Dies wäre dur, first, h, interp, title.

* Es liegt auf der Hand, Einträge mithilfe von p.add_argument in alphabetischer Reihenfolge hinzuzufügen, aber ich versuche, dies zu vermeiden.

Antworten auf die Frage(5)

Ihre Antwort auf die Frage