[New-bugs-announce] [issue39779] [argparse] Add parameter to sort help output arguments

brian.gallagher report at bugs.python.org
Thu Feb 27 18:40:07 EST 2020


New submission from brian.gallagher <oss.brnglr at gmail.com>:

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.

----------
components: Library (Lib)
messages: 362849
nosy: brian.gallagher
priority: normal
severity: normal
status: open
title: [argparse] Add parameter to sort help output arguments
type: enhancement

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39779>
_______________________________________


More information about the New-bugs-announce mailing list