question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Newlines confuse word wrapping

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
AdrienPensartcommented, Sep 24, 2019

Try this, a little hacky workaround.

from click.formatting import HelpFormatter
HelpFormatter.write_dl.__defaults__ = (50, 2)
0reactions
davidismcommented, 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

310. Why does my text wrap to the next line before reaching ...
Position the cursor in the text that is wrapping in the wrong place and from the Home tab, Paragraph group, select the dialog...
Read more >
How can I soft-wrap a word to a new line?
This is a hard wrap, and resizing the window will not change the location of the wrap. Using :set wrap , soft wrapping...
Read more >
Four ways to make text fit in an Excel cell - Office Watch
About wrapping text, adding line breaks and merging cells in Excel. ... The line break can make editing the text confusing because it ......
Read more >
white-space - CSS-Tricks
Interestingly, the final line break is not honored. As per the CSS 2.1 spec: “Lines are broken at preserved newline characters, ...
Read more >
Is there a way to prevent notepad from inserting new lines ...
That is not the default behavior. Try to disable word wrap, then re-enable it. What if you change the size of the window...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found