在使用Python 点击库编写命令行界面(CLI)时,是否可以定义三个选项,只有在第一个(可选)未设置的情况下才需要第二个和第三个选项?
我的用例是一个登录系统,它允许我通过authentication token(选项1),或者通过username(选项2)和password(选项3)进行身份验证.
如果给出了令牌,则无需检查username并password定义或提示它们.否则,如果忽略了此令牌,然后username和password成为必需的,必须给予.
这可以使用回调以某种方式完成吗?
我的入门代码当然不能反映出预期的模式:
@click.command()
@click.option('--authentication-token', prompt=True, required=True)
@click.option('--username', prompt=True, required=True)
@click.option('--password', hide_input=True, prompt=True, required=True)
def login(authentication_token, username, password):
print(authentication_token, username, password)
if __name__ == '__main__':
login()
Run Code Online (Sandbox Code Playgroud)