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
argparse print_help() fails if COLUMNS is set to a low value #57929
Comments
% cat test_argparse_narrow.py % COLUMNS=15 ./python test_argparse_narrow.py
Traceback (most recent call last):
File "test_argparse_narrow.py", line 2, in <module>
argparse.ArgumentParser().print_help()
File "/home/zbyszek/python/cpython/Lib/argparse.py", line 2347, in print_help
self._print_message(self.format_help(), file)
File "/home/zbyszek/python/cpython/Lib/argparse.py", line 2321, in format_help
return formatter.format_help()
File "/home/zbyszek/python/cpython/Lib/argparse.py", line 276, in format_help
help = self._root_section.format_help()
File "/home/zbyszek/python/cpython/Lib/argparse.py", line 206, in format_help
func(*args)
File "/home/zbyszek/python/cpython/Lib/argparse.py", line 206, in format_help
func(*args)
File "/home/zbyszek/python/cpython/Lib/argparse.py", line 514, in _format_action
help_lines = self._split_lines(help_text, help_width)
File "/home/zbyszek/python/cpython/Lib/argparse.py", line 615, in _split_lines
return _textwrap.wrap(text, width)
File "/home/zbyszek/python/cpython/Lib/textwrap.py", line 316, in wrap
return w.wrap(text)
File "/home/zbyszek/python/cpython/Lib/textwrap.py", line 291, in wrap
return self._wrap_chunks(chunks)
File "/home/zbyszek/python/cpython/Lib/textwrap.py", line 220, in _wrap_chunks
raise ValueError("invalid width %r (must be > 0)" % self.width)
ValueError: invalid width -1 (must be > 0) argparse should not fail if the user resizes the window to something very thin... |
The code works fine on 3.2.2, Win7, IDLE, narrowest window possible (about 14 chars), which actually wraps to the window width. (In command window, lines are fixed length and scroll bar is added if window is narrowed.) What system and version are you running? The error directly comes from textwrap. In the other hand, textwrap.wrap works with widths down to 1 (on 3.2.2), which suggests that argparse is calling it wrong. Except that it is not on my system. Could you add 'print(width)' before the call to textwrap |
See also bpo-13107. |
I am not setting columns, so that might be the important difference. |
Please try my commandline example or set os.environ['COLUMNS'] = 15. |
Now I get error, even with Window actually about 100 columns wide. |
Hi, I am having the same problem while running ipython in a batch mode emacs. Apparently you can't even start ipython if the columns are less than 27, since they use the argparse library for the magic method help printing and they preformat the strings at import time. So importing the library fails. :( I have a simple patch that fixes it but from what i can tell if you set the columns to 10 or 1000 there is no effect on the output, so i wonder if it even pays any attention to the width during rendering. If you think it's of value I am happy to write tests. Or if someone has a better implementation idea I'll be happy to implement it, but if you are on a terminal that is getting <27 characters then you can't really expect the formmating of the messages to be that readable. --- cpython/Lib/argparse.py 2012-07-22 12:10:42.751869655 +1000
+++ /tmp/ediff3953qkY 2012-07-22 12:54:51.380700044 +1000
@@ -95,6 +95,7 @@
def _callable(obj):
return hasattr(obj, '__call__') or hasattr(obj, '__bases__')
+MIN_WIDTH = 10
SUPPRESS = '==SUPPRESS=='
@@ -486,7 +487,10 @@
# determine the required width and the entry label
help_position = min(self._action_max_length + 2,
self._max_help_position)
- help_width = self._width - help_position
+ if self._width - help_position > MIN_WIDTH:
+ help_width = self._width - help_position
+ else:
+ help_width = MIN_WIDTH
action_width = help_position - self._current_indent - 2
action_header = self._format_action_invocation(action) |
Definitely a bug here. Attached is a patch and a test, based on Russell Sim's suggestion, that should fix it. |
Thank you Zbyszek and Steven for your report and patch, but this was fixed in bpo-13107. |
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: