-
-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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: add max_text_width parameter to ArgumentParser #83990
Comments
It is often desirable to limit the help text width, for instance to 78 or 88 columns, regardless of the actual size of the terminal window. Currently you can achieve this in rather cumbersome ways, for instance by setting "os.environ['COLUMNS'] = '80'" (but this requires the "os" module, which may not be needed otherwise by your module, and may lead to other undesired effects), or by writing a custom formatting class. IMHO there should be a simpler option for such a basic task. I propose to add a max_text_width parameter to ArgumentParser. This would require only minor code changes to argparse (see attached patch), should I open a pull request on GitHub? |
https://bugs.python.org/issue13041 is (I think) the latest issue/patch to deal with the help width. I don't like the idea of adding more parameters to the There are a couple of ways that a user can do this already. One is a custom version of the Another is a subclass of HelpFormatter. It just needs to customize the I vaguely recall suggesting such a subclass in a previous bug/issue, but can't find that. Subclassing HelpFormatter is an established way of customizing the format. Here's a discussion of this on StackOverflow. It uses a simple lambda as https://stackoverflow.com/questions/44333577/explain-lambda-argparse-helpformatterprog-width formatter = lambda prog: argparse.HelpFormatter(prog, width=100) and |
I concur with Paul. Let's pass on this suggestion. |
That lambda function would not give the same result, as it would constrain the text width to a fixed value, while my proposal would just set a maximum limit (if the terminal is narrower, the actual text width would be reduced). With the current implementation all possible solutions are in my opinion overkilling for such a basic task. Maybe it is not a direct consequence of this, but as a matter of fact most Python scripts using |
But you can replace the simple 'lambda' with a function that takes the max with 'columns'. In other words, include: width = _shutil.get_terminal_size().columns
width -= 2
width = min(max_text_width, width) The only thing that the 'formatter_class' parameter requires is a callable that takes 'prog' as argument. That can be a class, a subclass, a lambda or a function. |
OK, I will do this for my package, but I do not believe many others will do the same without a |
For the benefit of other developers willing to control text width with 541: error: Argument "formatter_class" to "ArgumentParser" has incompatible type "Callable[[Any], RawDescriptionHelpFormatter]"; expected "Type[HelpFormatter]" So I am reverting back to the following custom formatting class: class RawDescriptionHelpFormatterMaxTextWidth80(argparse.RawDescriptionHelpFormatter):
"""Set maximum text width = 80."""
def __init__(self, prog):
width = min(80, shutil.get_terminal_size().columns - 2)
argparse.RawDescriptionHelpFormatter.__init__(self, prog, width=width) |
Of course I still think there should be an easier way to do this though. |
I opened two issues regarding the mypy error, I understand it is going to be fixed in typeshed: |
The issue has been fixed in width = min(80, shutil.get_terminal_size().columns - 2)
formatter_class = lambda prog: argparse.RawDescriptionHelpFormatter(prog, width=width) |
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: