argparse argument dependency

前端 未结 3 1572
無奈伤痛
無奈伤痛 2021-01-01 20:22

If I call the script below with these options:

--user u1 --password p1 --foo f1   --user u2   --user u3 --password p3

Then it will print:

3条回答
  •  梦毁少年i
    2021-01-01 20:39

    Define a custom user type which holds both username and password.

    def user(s):
        try:
            username, password = s.split()
        except:
            raise argparse.ArgumentTypeError('user must be (username, password)')
    
    group.add_argument('--user', type=user, action='append')
    

提交回复
热议问题