Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[argparse] Add parameter to sort help output arguments #83960

Closed
brnglr mannequin opened this issue Feb 27, 2020 · 3 comments
Closed

[argparse] Add parameter to sort help output arguments #83960

brnglr mannequin opened this issue Feb 27, 2020 · 3 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@brnglr
Copy link
Mannequin

brnglr mannequin commented Feb 27, 2020

BPO 39779
Nosy @rhettinger, @brnglr

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2020-02-28.05:59:11.268>
created_at = <Date 2020-02-27.23:40:07.415>
labels = ['type-feature', 'library']
title = '[argparse] Add parameter to sort help output arguments'
updated_at = <Date 2020-02-28.09:25:30.546>
user = 'https://github.com/brnglr'

bugs.python.org fields:

activity = <Date 2020-02-28.09:25:30.546>
actor = 'brian.gallagher'
assignee = 'none'
closed = True
closed_date = <Date 2020-02-28.05:59:11.268>
closer = 'rhettinger'
components = ['Library (Lib)']
creation = <Date 2020-02-27.23:40:07.415>
creator = 'brian.gallagher'
dependencies = []
files = []
hgrepos = []
issue_num = 39779
keywords = []
message_count = 3.0
messages = ['362849', '362867', '362872']
nosy_count = 2.0
nosy_names = ['rhettinger', 'brian.gallagher']
pr_nums = []
priority = 'normal'
resolution = 'rejected'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue39779'
versions = []

@brnglr
Copy link
Mannequin Author

brnglr mannequin commented Feb 27, 2020

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.

@brnglr brnglr mannequin added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Feb 27, 2020
@rhettinger
Copy link
Contributor

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.

@brnglr
Copy link
Mannequin Author

brnglr mannequin commented Feb 28, 2020

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.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

1 participant