8000 [argparse] Unify options in help output · Issue #71490 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

[argparse] Unify options in help output #71490

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

Closed
memeplex mannequin opened this issue Jun 12, 2016 · 5 comments
Closed

[argparse] Unify options in help output #71490

memeplex mannequin opened this issue Jun 12, 2016 · 5 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@memeplex
Copy link
Mannequin
memeplex mannequin commented Jun 12, 2016
BPO 27303

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 = None
created_at = <Date 2016-06-12.20:21:38.673>
labels = ['type-feature', 'library']
title = '[argparse] Unify options in help output'
updated_at = <Date 2016-06-13.03:56:17.543>
user = 'https://bugs.python.org/memeplex'

bugs.python.org fields:

activity = <Date 2016-06-13.03:56:17.543>
actor = 'memeplex'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2016-06-12.20:21:38.673>
creator = 'memeplex'
dependencies = []
files = []
hgrepos = []
issue_num = 27303
keywords = []
message_count = 4.0
messages = ['268400', '268405', '268411', '268416']
nosy_count = 3.0
nosy_names = ['bethard', 'memeplex', 'paul.j3']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue27303'
versions = ['Python 3.6']

@memeplex
Copy link
Mannequin Author
memeplex mannequin commented Jun 12, 2016

Currently when you specify more than one name for an option (typically short and long versions) each name is listed with its entire arg list. This is annoying for options taking many args or choices, for example:

--type {html,pdf,github,blogger}, -t {html,pdf,github,blogger}

Wouldn't it be better to just show something like:

--type|-t {html,pdf,github,blogger}

@memeplex memeplex mannequin added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Jun 12, 2016
@paulj3
Copy link
Mannequin
paulj3 mannequin commented Jun 12, 2016

There are 2 issues here -

  • how to make the 'choices' list most compact

  • how to make the multiple option strings display (long and short) more compact, regardless of why the argument part is long.

When the choices display is too long, 'metavar' is a handy alternative. You can still display the choices in the body of the help message, either as an explicit list or with the %(choices)s string. The long choices list will still appear in the error messages.

There are other bug/issues about formatting the choices list.

I have participated in discussions about replacing

-f FOO, --foo FOO etc 

with

-f/--foo FOO etc

I'm sure that's been raised on Stackoverflow, but there might also be a bug/issue on the topic. I'd have to do some search to find those. I believe it can addressed with a HelpFormatter subclass that changes one method.

@paulj3
Copy link
Mannequin
paulj3 mannequin commented Jun 13, 2016

http://stackoverflow.com/questions/18275023/dont-show-long-options-twice-in-print-help-from-argparse

Once answer demonstrates how to change the Formatter:

class CustomHelpFormatter(argparse.HelpFormatter):
    def _format_action_invocation(self, action):
        if not action.option_strings or action.nargs == 0:
            return super()._format_action_invocation(action)
        default = self._get_default_metavar_for_optional(action)
        args_string = self._format_args(action, default)
        return ', '.join(action.option_strings) + ' ' + args_string

Another answer suggests using metavar=''.

Another SO question with a few more links:

http://stackoverflow.com/questions/23936145/python-argparse-help-message-disable-metavar-for-short-options

@memeplex
Copy link
Mannequin Author
memeplex mannequin commented Jun 13, 2016

Thank you for the tips, Paul. The issue is related to the default behavior but it's always good to know about handy workarounds and extensibility hooks.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
@erlend-aasland erlend-aasland moved this to Features in Argparse issues May 19, 2022
@savannahostrowski
Copy link
Member

This was fixed in #103372

@github-project-automation github-project-automation bot moved this from Features to Doc issues in Argparse issues Sep 23, 2024
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
Status: Doc issues
Development

No branches or pull requests

1 participant
0