Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8479637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:05:17+00:00 2026-06-10T19:05:17+00:00

I am using Python’s (2.7) argparse facility and would like to automatically sort the

  • 0

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()

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

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

*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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T19:05:18+00:00Added an answer on June 10, 2026 at 7:05 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Python, how does one parse/access files with Linux-specific features, like ~/.mozilla/firefox/*.default ? I've
Using Python I would like to find the date object for last Wednesday. I
Using python's optparse module I would like to add extra example lines below the
Using Python's Imaging Library I want to create a PNG file. I would like
I'm using Python 2.7. Let's say I have a list like so: string_list =
Using Python, how would I go about reading in (be from a string, file
Using Python, I'd like to compare every possible pair in a list. Suppose I
Using Python 2.5, I'd like to create a temporary file, but add (& modify)
Using Python, I'm trying to parse a string like this: hello I am an
Using Python, and classes, I'm trying to make a game like battleship, mostly for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.