This question is about the click package:
- Long text of help is not being displayed as desired.
- I tried using
/b
as well but does not seem to affect much. cmd
andpowershell
both have different results for same code, why?
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()
If you hook
click.formatting.wrap_text
you can change the behavior of the line wrapper thatclick.Command.get_help
uses.Code
Since you have already inherited from
click.Command
we can build our own version ofget_help()
to hook the line wrapper like:How does this work?
This works because click is a well designed OO framework. The
@click.command()
decorator usually instantiates aclick.Command
object but allows this behavior to be over ridden with thecls
parameter. So it is a relatively easy matter to inherit fromclick.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 hookclick.formatting.wrap_text()
. In our hook, we then set thepreserve_paragraphs
flag toTrue
. In addition wereplace()
all\n
with\n\n
as this is how the originalwrap_text()
expects paragraphs to be marked.Test Code:
Results: