python-click: formatting help text
Asked Answered
O

2

5

This question is about the click package:

  1. Long text of help is not being displayed as desired.
  2. I tried using /b as well but does not seem to affect much.
  3. cmd and powershell both have different results for same code, why?

Screen Shot of Problem

CODE:

import click


def command_required_option_from_option(require_name, require_map):

    class CommandOptionRequiredClass(click.Command):

        def invoke(self, ctx):
            require = ctx.params[require_name]
            if require not in require_map:
                raise click.ClickException(
                    "Unexpected value for --'{}': {}".format(
                        require_name, require))
            if ctx.params[require_map[require]] is None:
                raise click.ClickException(
                    "With {}={} must specify option --{}".format(
                        require_name, require, require_map[require]))
            super(CommandOptionRequiredClass, self).invoke(ctx)

    return CommandOptionRequiredClass

required_options = {
    1: 'gs',  # generator_string
    2: 'nosp',  # number_of_sample_points
    3: 'nocp',  # number_of_center_points
}


@click.command(context_settings=dict(max_content_width=800), cls=command_required_option_from_option('doe', required_options))
@click.option('--input', required=True, type=click.Path(exists=True), metavar='FILE', help="""\b
    Path to csv file""" )
@click.option('--doe', required=True, type=int, help="""
\b
Select DOE algorithm:   
1 Full factorial 
2 2-level fractional factorial
3 Plackett-Burman
4 Sukharev grid
5 Box-Behnken
6 Box-Wilson (Central-composite) with center-faced option
7 Box-Wilson (Central-composite) with center inscribed
8 Box-Wilson (Central-composite) with centser-circumscribed option
9 Latin hypercube (simple)
10 Latin hypercube (space-filling)
11 Random k-means cluster
12 Maximin reconstruction
13 Halton sequence based
14 Uniform random matrix
...
""",)
@click.option( '--gs', required=False, type=str, help="""\b
    Generator string for the fractional factorial build""")
@click.option(  '--nosp', required=False, type=int, help="""\b
    Number of random sample points""")
@click.option( '--nocp', required=False, type=int, help="""\b
    Number of center points to be repeated (if more than one):""")
def main(input, doe, gs, nosp, nocp):
    click.echo('input: {}'.format(input))
    click.echo('doe: {}'.format(doe))
    click.echo('generator_string: {}'.format(gs))
    click.echo('Num of sample_points: {}'.format(nosp))
    click.echo('Num of center_points: {}'.format(nocp))


if __name__ == "__main__":
    main()
Overcautious answered 9/4, 2019 at 5:21 Comment(3)
What result are you looking for? (4) in CMD is clearly weird, but the rest (at first glance) seem reasonable.Misdemean
4 and 11 in CMD both are weired. I was expecting a long single line as CMD has lots of space on right availableOvercautious
But for starters, A consistant print on both powershell and cmd will be much appreciatedOvercautious
M
6

If you hook click.formatting.wrap_text you can change the behavior of the line wrapper that click.Command.get_help uses.

Code

Since you have already inherited from click.Command we can build our own version of get_help() to hook the line wrapper like:

def command_required_option_from_option(require_name, require_map):

    class CommandOptionRequiredClass(click.Command):

        def get_help(self, ctx):
            orig_wrap_test = click.formatting.wrap_text

            def wrap_text(text, width=78, initial_indent='',
                          subsequent_indent='',
                          preserve_paragraphs=False):
                return orig_wrap_test(text.replace('\n', '\n\n'), width,
                                      initial_indent=initial_indent,
                                      subsequent_indent=subsequent_indent,
                                      preserve_paragraphs=True
                                      ).replace('\n\n', '\n')

            click.formatting.wrap_text = wrap_text
            return super(CommandOptionRequiredClass, self).get_help(ctx)

    return CommandOptionRequiredClass

How does this work?

This works because click is a well designed OO framework. The @click.command() decorator usually instantiates a click.Command object but allows this behavior to be over ridden with the cls parameter. So it is a relatively easy matter to inherit from click.Command in our own class and over ride desired methods.

In this case, we override click.Command.get_help(). In our get_help() we then hook click.formatting.wrap_text(). In our hook, we then set the preserve_paragraphs flag to True. In addition we replace() all \n with \n\n as this is how the original wrap_text() expects paragraphs to be marked.

Test Code:

import click

required_options = {
    1: 'gs',  # generator_string
    2: 'nosp',  # number_of_sample_points
    3: 'nocp',  # number_of_center_points
}

@click.command(context_settings=dict(max_content_width=800),
               cls=command_required_option_from_option('doe', required_options))
@click.option('--input', required=True, type=click.Path(exists=True), 
              metavar='FILE', help="""\b
    Path to csv file""" )
@click.option('--doe', required=True, type=int, help="""
Select DOE algorithm:   
1 Full factorial 
2 2-level fractional factorial
3 Plackett-Burman
4 Sukharev grid
5 Box-Behnken
6 Box-Wilson (Central-composite) with center-faced option
7 Box-Wilson (Central-composite) with center inscribed
8 Box-Wilson (Central-composite) with center-circumscribed option
9 Latin hypercube (simple)
10 Latin hypercube (space-filling)
11 Random k-means cluster
12 Maximin reconstruction
13 Halton sequence based
14 Uniform random matrix
...
""",)
@click.option( '--gs', required=False, type=str, help="""\b
    Generator string for the fractional factorial build""")
@click.option(  '--nosp', required=False, type=int, help="""\b
    Number of random sample points""")
@click.option( '--nocp', required=False, type=int, help="""\b
    Number of center points to be repeated (if more than one):""")
def main(input, doe, gs, nosp, nocp):
    click.echo('input: {}'.format(input))
    click.echo('doe: {}'.format(doe))
    click.echo('generator_string: {}'.format(gs))
    click.echo('Num of sample_points: {}'.format(nosp))
    click.echo('Num of center_points: {}'.format(nocp))

if __name__ == "__main__":
    main(['--help'])

Results:

Usage: test.py [OPTIONS]

Options:
  --input FILE    
                  Path to csv file  [required]
  --doe INTEGER   Select DOE algorithm:
                  1 Full factorial
                  2 2-level fractional factorial
                  3 Plackett-Burman
                  4 Sukharev grid
                  5 Box-Behnken
                  6 Box-Wilson (Central-composite) with center-faced option
                  7 Box-Wilson (Central-composite) with center inscribed
                  8 Box-Wilson (Central-composite) with center-circumscribed
                  option
                  9 Latin hypercube (simple)
                  10 Latin hypercube (space-filling)
                  11 Random k-means cluster
                  12 Maximin reconstruction
                  13 Halton sequence based
                  14 Uniform random matrix
                  ...  [required]
  --gs TEXT       
                  Generator string for the fractional factorial build
  --nosp INTEGER  
                  Number of random sample points
  --nocp INTEGER  
                  Number of center points to be repeated (if more than one):
  --help          Show this message and exit.
Misdemean answered 9/4, 2019 at 14:6 Comment(0)
B
0

Because of your response I was able to sort out

help="""
Select DOE algorithm:   
1 Full factorial 
2 2-level fractional factorial
3 Plackett-Burman
4 Sukharev grid
5 Box-Behnken
6 Box-Wilson (Central-composite) with center-faced option
7 Box-Wilson (Central-composite) with center inscribed
8 Box-Wilson (Central-composite) with center-circumscribed option
9 Latin hypercube (simple)
10 Latin hypercube (space-filling)
11 Random k-means cluster
12 Maximin reconstruction
13 Halton sequence based
14 Uniform random matrix
...
""",)

Can be converted to

help="\n\b\n"+"Select DOE algorithm:\n1 Full factorial\n2 2-level fractional factorial\n3 Plackett-Burman\n4 Sukharev grid\n5 Box-Behnken\n6 Box-Wilson (Central-composite) with center-faced option\n7 Box-Wilson (Central-composite) with center inscribed\n8 Box-Wilson (Central-composite) with center-circumscribed option\n9 Latin hypercube (simple)\n10 Latin hypercube (space-filling)\n11 Random k-means cluster\n12 Maximin reconstruction\n13 Halton sequence based\n14 Uniform random matrix"

I was having issues with coloring messing with new lines of my helper text, but finding this response allowed me to do this:

@click.option('--doe', required=True, type=int, help="\n\b\n"+"Select DOE algorithm:\n"
click.style("1 ", fg="bright_red")+" Full factorial\n"+
click.style("2 ", fg="bright_red")+" 2-level fractional factorial\n"+
click.style("3 ", fg="bright_red")+" Plackett-Burman\n"+
click.style("4 ", fg="bright_red")+" Sukharev grid\n"+
click.style("5 ", fg="bright_red")+" Box-Behnken\n"+
click.style("6 ", fg="bright_red")+" Box-Wilson (Central-composite) with center-faced option\n"+
click.style("7 ", fg="bright_red")+" Box-Wilson (Central-composite) with center inscribed\n"+
click.style("8 ", fg="bright_red")+" Box-Wilson (Central-composite) with center-circumscribed option\n"+
click.style("9 ", fg="bright_red")+" Latin hypercube (simple)\n"+
click.style("10", fg="bright_red")+" Latin hypercube (space-filling)\n"+
click.style("11", fg="bright_red")+" Random k-means cluster\n"+
click.style("12", fg="bright_red")+" Maximin reconstruction\n"+
click.style("13", fg="bright_red")+" Halton sequence based\n"+
click.style("14", fg="bright_red")+" Uniform random matrix\n"
,)

In order to prevent rewrapping in click you need \b on a line by itself. https://click.palletsprojects.com/en/8.1.x/documentation/#preventing-rewrapping

Usually accomplished by

"""
\b
more text
"""

but can also be solved by

"\n\b\n"+"more text"

or

"\n\b\nmore text"
Bilek answered 31/7, 2023 at 19:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.