Back

python - command line options 命令行帮助 --help

发布时间: 2023-07-16 01:31:00

refer to:
https://click.palletsprojects.com/en/8.1.x/options/#basic-value-options

非常好用。

$ pip install -U click

使用:

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()

直接 python run.py --help

另一个例子

@click.command()
@click.option("--target", default="'C:\'", prompt="Please input path(e.g. C:\)", help="Target Path you want to scan, e.g. 'C:\\'")
@click.option('--strategy',
              prompt="Please choose the scan strategy",
              default="sha1",
              type=click.Choice(['sha1', 'regexp'], case_sensitive=False),
              help="sha1: scan files by comparing file's sha1(scan all files) \r\n \
                  regexp: scan files by regxp(in this mode it will skip binary files) \r\n "
              )
@click.option('--output', default="result.csv", prompt="Output file name", help="The file name of the CSV result, e.g. 'result.csv' ")

def hello(target, strategy, output):
    """
    A virus scanner. It can scan the files by sha1 or by regexp.  \n
    Usage:  \n
    $ python3 scanner.py --target=C:\ --strategy=sha1 --output=result.csv
    """

    click.echo(f"Hello, {target}, {strategy}, {output}!")

Back