GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Argparse: Add a "display" arg · Issue #86189 · python/cpython

Argparse: Add a "display" arg #86189

New issue
Copy link
New issue
Copy link
Open
Open
Argparse: Add a "display" arg#86189
Copy link
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

Description

ThatXliner
mannequin
opened on Oct 12, 2020
Issue body actions
BPO 42023
Nosy @rhettinger, @ThatXliner

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 2020-10-12.21:18:16.139>
labels = ['type-feature', 'library', '3.9', '3.10']
title = 'Argparse: Add a "display" arg'
updated_at = <Date 2020-10-14.19:40:18.135>
user = 'https://github.com/ThatXliner'

bugs.python.org fields:

activity = <Date 2020-10-14.19:40:18.135>
actor = 'ThatXliner'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2020-10-12.21:18:16.139>
creator = 'ThatXliner'
dependencies = []
files = []
hgrepos = []
issue_num = 42023
keywords = []
message_count = 10.0
messages = ['378526', '378599', '378601', '378632', '378644', '378646', '378647', '378649', '378650', '378651']
nosy_count = 3.0
nosy_names = ['rhettinger', 'paul.j3', 'ThatXliner']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue42023'
versions = ['Python 3.9', 'Python 3.10']

Activity

  1. ThatXliner commented on Oct 12, 2020

    @ThatXliner
    ThatXlinermannequin
    on Oct 12, 2020
    MannequinAuthor
    More actions

    In argparse, I've always wanted a way to make colored help text like those of poetry and pipenv. But argparse's show help isn't well documented (Therefore, I can't find a way to completely modify the help text), and the metavar arg isn't really helpful. I want to do something like this:

    >>> import argparse
    >>> parser = argparse.ArgumentParser()
    >>> parser.add_argument("foo", help="To foo a bar", display="FOO!!!!")
    >>> parser.print_help()
    
    usage: main.py [-h] foo
    
    positional arguments:
      FOO!!!!     To foo a bar
    
    optional arguments:
      -h, --help  show this help message and exit
    
    >>>

    That way, I can colorize the help output.

  2. ThatXliner
    added
    3.9 (EOL)end of lifeend of life
    3.10 (EOL)end of lifeend of life
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    type-featureA feature request or enhancementA feature request or enhancement
    on Oct 12, 2020
  3. rhettinger commented on Oct 14, 2020

    @rhettinger
    rhettinger
    on Oct 14, 2020
    Contributor
    More actions

    and the metavar arg isn't really helpful.

    Is your issue with *metavar* that it changes the help() output in two places?

  4. paulj3 commented on Oct 14, 2020

    @paulj3
    paulj3mannequin
    on Oct 14, 2020
    Mannequin
    More actions

    I'm not following this request either, especially the colored part.

    However, I can imagine adding a variable that gives the user full control over the action-invocation. In:

    argparse.HelpFormatter._format_action_invocation method.
    

    Currently it accepts the metavar (but not the tuple version) for a positional, but for optionals constructs:

    -s ARGS, --long ARGS

    This can be quite long and ugly if the ARGS is a long choices, or there are many flag alternatives. A number of SO users have asked for something simpler, usually with just on flag or

    -s, --long ARGS

    There may even be a bug/issue(s) related to this already.

  5. ThatXliner commented on Oct 14, 2020

    @ThatXliner
    ThatXlinermannequin
    on Oct 14, 2020
    MannequinAuthor
    More actions

    However, I can imagine adding a variable that gives the user full control over the action-invocation. In:

    argparse.HelpFormatter._format_action_invocation method.
    

    That's what I'm trying to change. If there was a public API/method for manipulating the left side of the help menu.

    (This part) (Manipulatable via help=)

  6. paulj3 commented on Oct 14, 2020

    @paulj3
    paulj3mannequin
    on Oct 14, 2020
    Mannequin
    More actions

    That method could be customized in a HelpFormatter subclass. There already are several subclasses (documented), and writing your own is allowed, if not actually encouraged.

    https://docs.python.org/3/library/argparse.html#formatter-class

    Adding an extra parameter to 'add_argument', and passing that on through the Action class (and subclasses) is, potentially, a bigger task.

    So if you can get by with just customizing _format_action_invocation, there's no need for further action here.

    I'll look for some examples, here or on SO, of customizing this method.

  7. ThatXliner commented on Oct 14, 2020

    @ThatXliner
    ThatXlinermannequin
    on Oct 14, 2020
    MannequinAuthor
    More actions

    Should I rename this to "Argparse: finer control on help txt"

  8. ThatXliner commented on Oct 14, 2020

    @ThatXliner
    ThatXlinermannequin
    on Oct 14, 2020
    MannequinAuthor
    More actions

    So if you can get by with just customizing _format_action_invocation, there's no need for further action here.

    Then maybe we could just make a new function that takes 2 arguments: Name of another action class (or the action class itself), and a tuple of the arg names to be displayed. It returns a new action class that contains the help text properties of the given action class with customized displayed arg names.

    Maybe like

    parser.add_argument("-f", "--foo", action=argparse.CustomArgName(action="store_true", name=("-F!!", "--FOO!"), help="TO foo a bar"))

  9. paulj3 commented on Oct 14, 2020

    @paulj3
    paulj3mannequin
    on Oct 14, 2020
    Mannequin
    More actions

    Some StackOverFlow answers cusomizing this help method:

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

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

  10. ThatXliner commented on Oct 14, 2020

    @ThatXliner
    ThatXlinermannequin
    on Oct 14, 2020
    MannequinAuthor
    More actions

    Editing the description, etc. seems like a good way to do this. But it seems "hacky". The other answer on StackOverflow implements a custom class which does nice things, but not what I want.

  11. ThatXliner commented on Oct 14, 2020

    @ThatXliner
    ThatXlinermannequin
    on Oct 14, 2020
    MannequinAuthor
    More actions

    In the end, I think python's argparse needs more built-in help formatters...

    Should I close this?

  12. ezio-melotti
    transferred this issue fromon Apr 10, 2022
  13. erlend-aasland
    added this to Argparse issueson May 19, 2022
  14. erlend-aasland
    moved this to Features in Argparse issueson May 19, 2022
  15. picnixz
    removed
    3.10 (EOL)end of lifeend of life
    3.9 (EOL)end of lifeend of life
    on Mar 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

    Projects

    • Argparse issues
      Status
      Features
      Show more project fields

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.