Skip to content

Commit

Permalink
Strip single newlines in option and group-command helps.
Browse files Browse the repository at this point in the history
Fixes #23
  • Loading branch information
ewels committed Feb 28, 2022
1 parent 1221279 commit 0ffd717
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

- Added support for `HEADER_TEXT` and `FOOTER_TEXT` to go before and after help output
- Catch Abort exceptions from `cmd+c` and print nicely using `ABORTED_TEXT`
- Handle missing `click.types._NumberRangeBase` in click 7x
- Require at least click v7.0 (released 2018)
- Handle missing `click.types._NumberRangeBase` in click 7x [#16](https://github.com/ewels/rich-click/issues/16)
- Require at least click v7.0 (released 2018) [#16](https://github.com/ewels/rich-click/issues/16)
- Unwrap single newlines in option and group-command help texts [#23](https://github.com/ewels/rich-click/issues/23)

## Version 1.0.0 (2022-02-18)

Expand Down
19 changes: 17 additions & 2 deletions examples/01_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@


@click.group()
@click.option("--debug/--no-debug", "-d/-n", default=False, help="Enable debug mode")
@click.option(
"--debug/--no-debug",
"-d/-n",
default=False,
help="""Enable debug mode.
Newlines are removed by default.
Double newlines are preserved.""",
)
def cli(debug):
"""
My amazing tool does all the things.
Expand Down Expand Up @@ -33,7 +41,14 @@ def sync(type, all):
@cli.command()
@click.option("--all", is_flag=True, help="Get everything")
def download(all):
"""Pretend to download some files from somewhere"""
"""
Pretend to download some files from
somewhere. Multi-line help strings are unwrapped
until you use a double newline.
Only the first paragraph is used in group help texts.
Don't forget you can opt-in to rich and markdown formatting!
"""
print("Downloading")


Expand Down
30 changes: 26 additions & 4 deletions src/rich_click/rich_click.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,11 @@ def _get_parameter_help(param, ctx):
items = []

if getattr(param, "help", None):
items.append(_make_rich_rext(param.help, STYLE_OPTION_HELP))
paragraphs = param.help.split("\n\n")
# Remove single linebreaks
if not USE_MARKDOWN:
paragraphs = [p.replace("\n", " ").strip() for p in paragraphs]
items.append(_make_rich_rext("\n".join(paragraphs).strip(), STYLE_OPTION_HELP))

# Append metavar if requested
if APPEND_METAVARS_HELP:
Expand Down Expand Up @@ -218,6 +222,26 @@ def _get_parameter_help(param, ctx):
return Columns(items)


def _make_command_help(helptext):
"""Build cli help text for a click group command.
Returns the first paragraph of help text for a command, rendered either as a
Rich Text object or as Markdown.
Ignores single newlines as paragraph markers, looks for double only.
Args:
helptext (str): Help text
Returns:
Text or Markdown: Styled object
"""
paragraphs = helptext.split("\n\n")
# Remove single linebreaks
if not USE_MARKDOWN:
paragraphs[0] = paragraphs[0].replace("\n", " ")
return _make_rich_rext(paragraphs[0].strip(), STYLE_OPTION_HELP)


def rich_format_help(obj, ctx, formatter):
"""Print nicely formatted help text using rich
Expand Down Expand Up @@ -426,9 +450,7 @@ def rich_format_help(obj, ctx, formatter):
continue
cmd = obj.get_command(ctx, command)
helptext = cmd.help or ""
commands_table.add_row(
command, _make_rich_rext(helptext.split("\n")[0])
)
commands_table.add_row(command, _make_command_help(helptext))
if commands_table.row_count > 0:
console.print(
Panel(
Expand Down

0 comments on commit 0ffd717

Please sign in to comment.