This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: [argparse] Add parameter to sort help output arguments
Type: enhancement Stage: resolved
Components: Library (Lib) Versions:
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: brian.gallagher, rhettinger
Priority: normal Keywords:

Created on 2020-02-27 23:40 by brian.gallagher, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg362849 - (view) Author: (brian.gallagher) * Date: 2020-02-27 23:40
1 import argparse                                                                       
  2                                                                                       
  3 parser = argparse.ArgumentParser(description='Test')                                  
  4 parser.add_argument('c', help='token c')                                              
  5 parser.add_argument('b', help='token b')                                              
  6 parser.add_argument('d', help='token d')                                              
  7 parser.add_argument('-a', help='token a')                                             
  8 parser.add_argument('-z', help='token z')                                             
  9 parser.add_argument('-f', help='token f', required=True)                                                                                                                
 10 parser.print_help() 

It would be nice if we could have the option to alphabetically sort the tokens in the optional and positional arguments sections of the help message in order to find an argument more quickly when reading long help descriptions.

Currently we output the following, when the above program is ran:

positional arguments:
  c           token c
  b           token b
  d           token d

optional arguments:
  -h, --help  show this help message and exit
  -a A        token a
  -z Z        token z
  -f F        token f

I'm proposing that we provide a mechanism to allow alphabetical ordering of both sections, like so:

positional arguments:
  b           token b
  c           token c
  d           token d

optional arguments:
  -h, --help  show this help message and exit
  -a A        token a
  -f F        token f
  -z Z        token z

I've chosen to leave -h as an exception, as it will always be there as an optional argument, but it could easily be treated no different.

We could provide an optional argument to print_help(sort=False) as a potential approach.

If this is something that the maintainer's would be willing to accept, I'd love to take it on and prepare a patch.
msg362867 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-02-28 05:59
Thanks for the suggestion, but I am going to decline.

Since the parser remembers the order the arguments were added, the programmer already has complete control over the ordering of arguments.
msg362872 - (view) Author: (brian.gallagher) * Date: 2020-02-28 09:25
That makes sense. For what it's worth, the use-case that inspired this was for commands with a lot of optional arguments in a company where a large amount of contributors (who may not be aware of an effort to order the arguments in the source code) were able to make changes to the command.

I understand that isn't a particularly compelling reason though, as it can be addressed by other means -- increasing diligence at the code review stage, commit hooks, testing, etc.

Thanks for taking a look Raymond.
History
Date User Action Args
2022-04-11 14:59:27adminsetgithub: 83960
2020-02-28 09:25:30brian.gallaghersetmessages: + msg362872
2020-02-28 05:59:11rhettingersetstatus: open -> closed

nosy: + rhettinger
messages: + msg362867

resolution: rejected
stage: resolved
2020-02-27 23:40:07brian.gallaghercreate