微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

点击帮助没有出现

如何解决点击帮助没有出现

我有一个不会弹出内置帮助菜单的点击 CLI。它的作用是运行我即使在输入“python file.py --help”时的代码。没有帮助菜单弹出,出于某种原因,代码会运行。我不希望代码运行我希望帮助菜单弹出。对此的任何帮助将不胜感激。

这是代码。我不能包括所有内容

定义 main():

@click.command()
@click.option('--run_name',help='Name of the run being permformed')
@click.option('--run_file',help='Name of the file on computer needed')
@click.option('--description',help='Brief description of run')
def register(run_name,run_file,description):
    ''' 
    Register a new run
    '''

#Allow user to enter run_name,descrption and also allow user to confirm if they want to continue 
run_name = click.prompt('Please enter a run name',type=str)
run_file = click.prompt('Please enter a file name',type=str)
description = click.prompt('Please enter file description',type=str)
click.confirm('Do you want to continue?',abort=True)

解决方法

来自文档:

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 x in range(count):
        click.echo(f"Hello {name}!")

if __name__ == '__main__':
    hello()

应生成此帮助消息。

Usage: click_test.py [OPTIONS]

  Simple program that greets NAME for a total of COUNT times.

Options:
  --count INTEGER  Number of greetings.
  --name TEXT      The person to greet.
  --help           Show this message and exit.
,

尝试运行 python -m your_script.py --help,应该可以。

或者,使用 Poetry 或其他构建工具构建您的包并将其安装到 venv,然后在启用入口点的情况下从那里运行脚本。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。