python click demo
时间: 2023-06-22 21:29:26 浏览: 85
Sure! Here's a simple Python Click demo:
```python
import click
@click.command()
@click.option('--count', default=1, help='Number of greetings.')
@click.option('--name', prompt='Your name', help='The person to greet.')
def hello(count, name):
"""Simple program that greets NAME for a total of COUNT times."""
for _ in range(count):
click.echo(f'Hello, {name}!')
if __name__ == '__main__':
hello()
```
This program defines a `hello` command that takes two options: `count` and `name`. The `count` option specifies the number of times to greet the given `name`. The `name` option is a required option that prompts the user to enter a name to greet.
When the `hello` command is run, it will print out a greeting message for the given `name` the specified number of times. For example, running `python hello.py --count=3 --name=World` will print out:
```
Hello, World!
Hello, World!
Hello, World!
```
I hope this helps! Let me know if you have any other questions.
阅读全文