python 命令行互动_将Python脚本变为命令行--click模块使用

import click

# click.option 中的命令规则可参考参数名称。它接受的前两个参数为长、短选项(顺序随意),其中:

#

# 长选项以 “--” 开头,比如 “--string-to-echo”

# 短选项以 “-” 开头,比如 “-s”

@click.group()

def main():

pass

@main.command()

@click.option('-u', '--user_name', type=str, help='add user_name')

def get_user(user_name):

click.echo(f'search user:{user_name}')

@main.command()

@click.option('-u', '--user_name', required=True, type=str, help="要添加的用户名")

@click.option('-p', '--password', required=True, type=str, help="要添加的密码")

@click.option('-t', '--id_type', required=True, default="phone", type=str, help="添加的账户类型",show_default=True)

def add_user(user_name, password, id_type):

click.echo(f"{user_name=} {password=} {id_type=}")

if __name__ == '__main__':

main()

使用方式

python3 demo.py get-user -u "122"

或者

python3 demo.py add-user -u "123" -p "12"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值