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

Option format text should replace newlines with spaces #834

Closed
warsaw opened this issue Jul 21, 2017 · 1 comment
Closed

Option format text should replace newlines with spaces #834

warsaw opened this issue Jul 21, 2017 · 1 comment
Labels
Milestone

Comments

@warsaw
Copy link

@warsaw warsaw commented Jul 21, 2017

In Mailman 3, we write options like so:

@click.option(
    '-C', '--config', 'config_file',
    envvar='MAILMAN_CONFIG_FILE',
    type=click.Path(exists=True, dir_okay=False, resolve_path=True),
    help=_("""\
    Configuration file to use.  If not given, the environment variable
    MAILMAN_CONFIG_FILE is consulted and used if set.  If neither are given, a
    default configuration file is loaded."""))

This makes it possible to translate the help text, but because the source formatting is a triple quoted string, there are embedded newlines in the help text. We have to preserve the original source string text so that it can be looked up in the i18n catalog, and we can't really change the _() function to strip newlines at runtime because the source strings are extracted by an offline process that does not execute the source code.

After some pdb tracing, I've noticed that click already dedents the help text, which is great, but it also needs to substitute spaces for newlines, otherwise the wrapping of the help text isn't correct. For example, the above by default gets printed on the console like so:

$ mailman --help
...
Options:
  -C, --config PATH  Configuration file to use.  If not given, the
                     environment variable
                     MAILMAN_CONFIG_FILE is consulted
                     and used if set.  If neither are given, a
                     default
                     configuration file is loaded.

Here's a small change that makes the -C option print correctly:

    def format_options(self, ctx, formatter):
        """Writes all the options into the formatter if they exist."""
        opts = []
        for param in self.get_params(ctx):
            rv = param.get_help_record(ctx)
            if rv is not None:
                part_a, part_b = rv
                opts.append((part_a, part_b.replace('\n', ' ')))

        if opts:
            with formatter.section('Options'):
                formatter.write_dl(opts)

and now:

$ mailman --help
...
Options:
  -C, --config PATH  Configuration file to use.  If not given, the
                     environment variable MAILMAN_CONFIG_FILE is consulted
                     and used if set.  If neither are given, a default
                     configuration file is loaded.

Much better!

@fdavis fdavis added the enhancement label May 17, 2018
@fdavis

This comment has been minimized.

Copy link
Contributor

@fdavis fdavis commented May 17, 2018

Thanks for the issue and all the details!
There have been a quite few issues regarding formatting. Notably #513.
Hopefully we can gather all of these bits up and define a good format interface

warsaw added a commit to warsaw/click that referenced this issue May 24, 2018
Closes pallets#834
@davidism davidism added this to the 8.0 milestone May 6, 2019
@davidism davidism modified the milestones: 8.0, 7.1 Feb 17, 2020
@davidism davidism closed this Feb 17, 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.