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

Newlines confuse word wrapping #1397

Closed
mcclure opened this issue Sep 18, 2019 · 5 comments
Closed

Newlines confuse word wrapping #1397

mcclure opened this issue Sep 18, 2019 · 5 comments
Milestone

Comments

@mcclure
Copy link

@mcclure mcclure commented Sep 18, 2019

I am using Python 3.7.0 on Mac OS X 10.13.6, with Click 7.0. I make this small script:

import click

@click.command()
@click.option('--zero-replacement', default=[1.0, 0.0, 0.0, 0.0], nargs=4, type=click.FLOAT, help="Four decimal numbers separated by spaces, representing the replacement vec4 that all-zero weights will be replaced with. Default is 1 0 0 0")
def example(zero_replacement):
	pass

example()

I run python3 example.py --help. It prints:

Usage: example.py [OPTIONS]

Options:
  --zero-replacement FLOAT...  Four decimal numbers separated by spaces,
                               representing the replacement vec4 that all-zero
                               weights will be replaced with. Default is 1 0 0
                               0
  --help                       Show this message and exit.

Oh… that "1 0 0 [newline] 0" is a little confusing. I think, I will put a newline before "Default is". So my code is now:

import click

@click.command()
@click.option('--zero-replacement', default=[1.0, 0.0, 0.0, 0.0], nargs=4, type=click.FLOAT, help="Four decimal numbers separated by spaces, representing the replacement vec4 that all-zero weights will be replaced with.\nDefault is 1 0 0 0")
def example(zero_replacement):
	pass

example()

This prints:

Usage: example.py [OPTIONS]

Options:
  --zero-replacement FLOAT...  Four decimal numbers separated by spaces,
                               representing the replacement vec4 that all-zero
                               weights will be replaced with.
                               Default is 1 0 0
                               0
  --help                       Show this message and exit.

Notice the last 0 gets an auto-line-break even though there is plenty of room for it. It is like the wordwrap algorithm is treating \n as a single-width character.

I see #834 suggests in future Click might delete newlines. That's not what I want in this case, but if that were the behavior I would at least understand it. But this looks clearly wrong.

One more weirdness. I look in the documentation and see instructions for preventing rewrapping
https://click.palletsprojects.com/en/7.x/documentation/#preventing-rewrapping
"Rewrapping can be disabled on a per-paragraph basis by adding a line with solely the \b escape marker in it."

Well, I only want a single newline, not a paragraph. But I decide to try this:

import click

@click.command()
@click.option('--zero-replacement', default=[1.0, 0.0, 0.0, 0.0], nargs=4, type=click.FLOAT, help="Four decimal numbers separated by spaces, representing the replacement vec4 that all-zero weights will be replaced with.\n\b\nDefault is 1 0 0 0")
def example(zero_replacement):
	pass

example()

But when I run it:

Usage: example.py [OPTIONS]

Options:
  --zero-replacement FLOAT...  Four decimal numbers separated by spaces,
                               representing the replacement vec4 that all-zero
                               weights will be replaced with.
                               
                               Default is 1 0
                               0 0
  --help                       Show this message and exit.

It wrapped sooner! It appears to be treating the \n\b\n as THREE characters.

@AdrienPensart

This comment has been minimized.

Copy link

@AdrienPensart AdrienPensart commented Sep 24, 2019

Try this, a little hacky workaround.

from click.formatting import HelpFormatter
HelpFormatter.write_dl.__defaults__ = (50, 2)
@davidism

This comment has been minimized.

Copy link
Member

@davidism davidism commented Feb 17, 2020

I ended up merging the PR for #834, but I'm still willing to consider a fix for this.

@mcclure

This comment has been minimized.

Copy link
Author

@mcclure mcclure commented Feb 17, 2020

So with #834 merged is there currently any way to put a newline into a help message at all?

@davidism

This comment has been minimized.

Copy link
Member

@davidism davidism commented Feb 22, 2020

See #1025 (comment) and #1307 for some ideas on addressing newlines in wrapped option help.

@davidism

This comment has been minimized.

Copy link
Member

@davidism davidism commented Feb 23, 2020

Turns out the underlying wrap_text function already takes a preserve_paragraphs option, but it's not enabled for option help text right now. This enables the behavior in the command help text where \n\n is preserved and each paragraph is wrapped separately.

This isn't exactly what @mcclure asked for, since they wanted to use a single newline, but I don't know if that's possible right now. I can't see a way to distinguish an intentional single line break from the line breaks in an indented """ multiline string that need to be rewrapped (#834) without adding more complexity than I'm comfortable with. I think combined with #1075 it should still be visually distinct.

@davidism davidism added this to the 7.1 milestone Feb 23, 2020
@davidism davidism linked a pull request that will close this issue Feb 23, 2020
@davidism davidism closed this Feb 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Linked pull requests

Successfully merging a pull request may close this issue.

3 participants
You can’t perform that action at this time.