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

Prevent rewrapping with \b #24

Closed
aazuspan opened this issue Feb 28, 2022 · 9 comments
Closed

Prevent rewrapping with \b #24

aazuspan opened this issue Feb 28, 2022 · 9 comments

Comments

@aazuspan
Copy link

Hi @ewels, rich-click is an awesome package and I love that it's a drop-in replacement for click. However, I'm running into a small issue with rewrapping. click uses the \b flag to prevent rewrapping for a paragraph, which allows you to preserve newlines in a command's help. rich-click doesn't seem to support that flag and removes newlines.

Say you have a command foo:

@click.command()
def foo():
    """This is a command.

    \b
    Examples
        $ foo
        $ foo bar
    """
    return

Here's the output of foo --help using just click:

image

With rich-click, the newlines are lost.

image

Using Markdown instead might be an alternative, but would require rewriting docstrings and would break compatibility with click. Any chance of support being added for \b in rich-click?

Thanks!

@ewels
Copy link
Owner

ewels commented Feb 28, 2022

Yes good point, it would be nice to support all of the click escape markers - \f too..

@ewels
Copy link
Owner

ewels commented Feb 28, 2022

Ok, looks like the \f stuff was already working, that must be in the click code upstream of what we're replacing.

@ewels ewels closed this as completed in cba9726 Feb 28, 2022
@ewels
Copy link
Owner

ewels commented Feb 28, 2022

Ok, that hopefully got all of the various places that \b should work.. 🤞🏻

Let me know how you get on with the new code - thanks for the issue! 😀

@aazuspan
Copy link
Author

aazuspan commented Mar 1, 2022

Works perfectly. Thanks for such a quick fix!

@blag
Copy link

blag commented Apr 6, 2022

I'm still having this issue, although it's working perfectly with just Click.

import rich_click as click

@click.group(context_settings={'help_option_names': ['-h', '--help']})
@click.pass_context
def airflow_cmd(ctx):
    pass


@airflow_cmd.group('users')
@click.pass_context
def users(ctx):
    """Commands for managing users"""


@users.command('create')
@click.option('-f', '--firstname', metavar='FIRSTNAME', required=True, type=str, help="First name of the user")
@click.option('-l', '--lastname', metavar='LASTNAME', required=True, type=str, help="Last name of the user")
@click.option('-e', '--email', metavar='EMAIL', required=True, type=str, help="Email of the user")
@click.option('-u', '--username', metavar='USERNAME', required=True, type=str, help="Username of the user")
@click.password_option('-p', '--password', metavar='PASSWORD', help="Password of the user, required to create a user without --use-random-password")
@click.option('--use-random-password', is_flag=True, default=False, help="Do not prompt for password. Use random string instead. Required to create a user without --password")
@click.option('-r', '--role', metavar='ROLE', required=True, help="Role of the user, pre-existing roles include Admin, User, Op, Viewer, and Public")
@click.pass_context
@cli_utils.action_cli(check_db=True)
def users_create(ctx, firstname, lastname, email, username, password, use_random_password, role):
    """
    Creates new user in the DB

    Example
    \b
    To create a user with "Admin" role and username "admin":

    \b
    $ airflow users create \\
        --username admin \\
        --firstname FIRST_NAME \\
        --lastname LAST_NAME \\
        --role Admin \\
        --email admin@example.org
    """
    ...

Expected output (the double newlines a la #49 being respected would be nice here, but that's not this issue):

 Usage: airflow-ng users create [OPTIONS]

 Creates new user in the DB
  Example
  To create a user with "Admin" role and username "admin":
  airflow users create \
       --username admin \
       --firstname FIRST_NAME \
       --lastname LAST_NAME \
       --role Admin \
       --email admin@example.org

 ...

Actual output:

 Usage: airflow-ng users create [OPTIONS]

 Creates new user in the DB
  Example To create a user with "Admin" role and username "admin":
  airflow users create \     --username admin \     --firstname FIRST_NAME \     --lastname LAST_NAME \
 --role Admin \     --email admin@example.org

 ...

If I'm doing something I shouldn't be doing, please let me know.

Is there a good workaround? Can I enable markdown for just this one command and leave the rest of the commands I'm converting to Click using rich markup?

Besides this issue, I'm loving Click and Rich-Click. Thank you for making my CLI life so easy and ✨pretty✨! ❤️

@ewels
Copy link
Owner

ewels commented Apr 6, 2022

@blag - just to check, what version of rich-click is this with?

@ewels
Copy link
Owner

ewels commented Apr 6, 2022

ok I can't easily run your code example as it doesn't stand alone, but if I paste your docstring into the examples/click/01_simple.py script it works fine for me:

@cli.command(short_help="Optionally use short-help for the group help text")
@click.option("--all", is_flag=True, help="Get everything")
def download(all):
    """
    Creates new user in the DB

    Example

    To create a user with "Admin" role and username "admin":

    \b
    $ airflow users create \\
        --username admin \\
        --firstname FIRST_NAME \\
        --lastname LAST_NAME \\
        --role Admin \\
        --email admin@example.org
    """
    print("Downloading")
 ❯ python examples/click/01_simple.py download --help
Debug mode is off

 Usage: 01_simple.py download [OPTIONS]

 Creates new user in the DB
 Example
 To create a user with "Admin" role and username "admin":

 $ airflow users create \
     --username admin \
     --firstname FIRST_NAME \
     --lastname LAST_NAME \
     --role Admin \
     --email admin@example.org


╭─ Options ─────────────────────────────────────────────────────────────────╮
│  --all       Get everything                                               │
│  --help      Show this message and exit.                                  │
╰───────────────────────────────────────────────────────────────────────────╯

@blag
Copy link

blag commented Jun 8, 2022

I thought I had already responded to this but I don't see my comment here so I guess not.

The issue I had was that I was using rich-click version 1.3.0, and support for this was released in rich-click version 1.3.1.

Once I updated to rich-click 1.3.1, everything works perfectly, as expected.

Thank you for your help and apologies for not checking that before commenting. This package is seriously awesome! 😄

@ewels
Copy link
Owner

ewels commented Jun 8, 2022

Ok that's a relief 😅 Thanks for letting me know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants