import db.user
import argparse

parser = argparse.ArgumentParser(description="使用命令操作用户")
parser.add_argument(
    "operate", type=str, help="用户操作", choices=("add", "passwd", "del", "list")
)
parser.add_argument("-u","--username", type=str, help="用户名")

arg = parser.parse_args()

if arg.operate == 'add':
    if arg.username == None:
        print("未指定用户")
    if db.user.New(arg.username,"123123"):
        print("已创建")
    else:
        print("创建失败")
if arg.operate == 'passwd':
    if arg.username == None:
        print("未指定用户")
    passwd = input("输入密码")
    if input("确认密码") == passwd:
        if db.user.UpdatePassword(arg.username, passwd):
            print("已更新")
        else:
            print("失败")
    else:
        print("取消")
if arg.operate == 'del':
    if arg.username == None:
        print("未指定用户")
    if input("重复用户名以确认删除") == arg.username:
        if db.user.Del(arg.username):
            print("成功")
        else:
            print("失败")
    else:
        print("取消")
    
if arg.operate == 'list':
    for row in db.user.list():
        print(row[0])

