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

In argparse empty choices cannot be printed in the help #68632

Closed
py-user mannequin opened this issue Jun 13, 2015 · 7 comments
Closed

In argparse empty choices cannot be printed in the help #68632

py-user mannequin opened this issue Jun 13, 2015 · 7 comments
Labels
3.9 only security fixes 3.10 only security fixes 3.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@py-user
Copy link
Mannequin

py-user mannequin commented Jun 13, 2015

BPO 24444
Nosy @py-user, @ambv, @miss-islington, @akulakov
PRs
  • bpo-24444: fix an error in argparse help when help for an option is blank #28050
  • [3.10] bpo-24444: fix an error in argparse help when help for an option is blank (GH-28050) #28931
  • [3.9] bpo-24444: fix an error in argparse help when help for an option is blank (GH-28050) #28932
  • 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 2021-10-13.20:37:35.828>
    created_at = <Date 2015-06-13.06:47:02.770>
    labels = ['type-bug', 'library', '3.9', '3.10', '3.11']
    title = 'In argparse empty choices cannot be printed in the help'
    updated_at = <Date 2021-10-13.20:37:35.826>
    user = 'https://github.com/py-user'

    bugs.python.org fields:

    activity = <Date 2021-10-13.20:37:35.826>
    actor = 'lukasz.langa'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-10-13.20:37:35.828>
    closer = 'lukasz.langa'
    components = ['Library (Lib)']
    creation = <Date 2015-06-13.06:47:02.770>
    creator = 'py.user'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 24444
    keywords = ['patch']
    message_count = 7.0
    messages = ['245297', '245448', '273317', '403849', '403858', '403871', '403872']
    nosy_count = 5.0
    nosy_names = ['py.user', 'lukasz.langa', 'paul.j3', 'miss-islington', 'andrei.avk']
    pr_nums = ['28050', '28931', '28932']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue24444'
    versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

    @py-user
    Copy link
    Mannequin Author

    py-user mannequin commented Jun 13, 2015

    >>> import argparse
    >>> 
    >>> parser = argparse.ArgumentParser()
    >>> _ = parser.add_argument('foo', choices=[], help='%(choices)s')
    >>> parser.print_help()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/home/guest/tmp/tests/misc/git/example/cpython/main/Lib/argparse.py", line 2358, in print_help
        self._print_message(self.format_help(), file)
      File "/home/guest/tmp/tests/misc/git/example/cpython/main/Lib/argparse.py", line 2342, in format_help
        return formatter.format_help()
      File "/home/guest/tmp/tests/misc/git/example/cpython/main/Lib/argparse.py", line 278, in format_help
        help = self._root_section.format_help()
      File "/home/guest/tmp/tests/misc/git/example/cpython/main/Lib/argparse.py", line 208, in format_help
        func(*args)
      File "/home/guest/tmp/tests/misc/git/example/cpython/main/Lib/argparse.py", line 208, in format_help
        func(*args)
      File "/home/guest/tmp/tests/misc/git/example/cpython/main/Lib/argparse.py", line 517, in _format_action
        parts.append('%*s%s\n' % (indent_first, '', help_lines[0]))
    IndexError: list index out of range
    >>>

    It's not very useful to print empty choices, but the choices list could be formed dynamically. So the command-line user can't figure out what's happen.

    @py-user py-user mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Jun 13, 2015
    @paulj3
    Copy link
    Mannequin

    paulj3 mannequin commented Jun 18, 2015

    Does this work for

        help='choices: %(choices)s'

    Without setting up my own test case, I'm guessing that the problem is with an empty help string. help='' should be ok, but the test handle that probably occurs earlier.

    Choices, empty or not, are included earlier in the help line, as well as usage. So there isn't real need to include them in the user defined part of the help line.

    Still, there are other bug issues regarding the formatting of choices. That part needs refinement to handle very long lists of choices, as well as ones that don't fit the usual 'list' format.

    @paulj3
    Copy link
    Mannequin

    paulj3 mannequin commented Aug 21, 2016

    This error is also produced by

    help = ' '
    

    that is, a help line with all blanks. As long as there is a nonblank character in the help line it format fine.

    This issue doesn't need to be resolved by itself, but should be considered if and when the formatting of choices is reworked.

    @ambv
    Copy link
    Contributor

    ambv commented Oct 13, 2021

    New changeset 6fafc25 by andrei kulakov in branch 'main':
    bpo-24444: fix an error in argparse help when help for an option is blank (GH-28050)
    6fafc25

    @ambv
    Copy link
    Contributor

    ambv commented Oct 13, 2021

    New changeset fd2be6d by Miss Islington (bot) in branch '3.10':
    bpo-24444: fix an error in argparse help when help for an option is blank (GH-28050) (GH-28931)
    fd2be6d

    @ambv
    Copy link
    Contributor

    ambv commented Oct 13, 2021

    New changeset fb72034 by Miss Islington (bot) in branch '3.9':
    [3.9] bpo-24444: fix an error in argparse help when help for an option is blank (GH-28050) (GH-28932)
    fb72034

    @ambv
    Copy link
    Contributor

    ambv commented Oct 13, 2021

    Thanks, Andrei! ✨ 🍰 ✨

    @ambv ambv added 3.9 only security fixes 3.10 only security fixes 3.11 only security fixes labels Oct 13, 2021
    @ambv ambv closed this as completed Oct 13, 2021
    @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
    3.9 only security fixes 3.10 only security fixes 3.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant