python / cpython

The Python programming language
https://www.python.org
Other
62.36k stars 29.95k forks source link

[argparse] Unify options in help output #71490

Open 6d455403-7a2f-4b36-bbc4-ae3f94622bf1 opened 8 years ago

6d455403-7a2f-4b36-bbc4-ae3f94622bf1 commented 8 years ago
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: ```python assignee = None closed_at = None created_at = labels = ['type-feature', 'library'] title = '[argparse] Unify options in help output' updated_at = user = 'https://bugs.python.org/memeplex' ``` bugs.python.org fields: ```python activity = actor = 'memeplex' assignee = 'none' closed = False closed_date = None closer = None components = ['Library (Lib)'] creation = 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'] ```

6d455403-7a2f-4b36-bbc4-ae3f94622bf1 commented 8 years ago

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}

7a064fe6-c535-4d80-a11f-a04ed39056c5 commented 8 years ago

There are 2 issues here -

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.

7a064fe6-c535-4d80-a11f-a04ed39056c5 commented 8 years ago

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

6d455403-7a2f-4b36-bbc4-ae3f94622bf1 commented 8 years ago

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.