如果你钩click.formatting.wrap_text
您可以更改换行器的行为click.Command.get_help
uses.
Code
既然你已经继承了click.Command
我们可以构建我们自己的版本get_help()
挂钩换行器,例如:
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
这是如何运作的?
这是可行的,因为 click 是一个设计良好的 OO 框架。这@click.command()
装饰器通常会实例化一个click.Command
对象,但允许此行为被覆盖cls
范围。所以继承是一件比较容易的事情click.Command
在我们自己的班级中并超越所需的方法。
在本例中,我们重写 click.Command.get_help()。在我们的get_help()
然后我们挂钩click.formatting.wrap_text()
。然后在我们的钩子中设置preserve_paragraphs
标记为True
。另外我们replace()
all \n
with \n\n
因为这就是原来的样子wrap_text()
期望对段落进行标记。
测试代码:
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.