-
-
Notifications
You must be signed in to change notification settings - Fork 29.9k
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
Comments
>>> 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. |
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. 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. |
This error is also produced by
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. |
Thanks, Andrei! ✨ 🍰 ✨ |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: