首页 文章

docopt参数解析:如何避免意大利面条代码?

提问于
浏览
1

这是我第一次使用docopt而且我正在努力解决args解析我想要实现的小命令行程序 .

'''Usage:
    ovh_mails.py list [--ugly]
    ovh_mails.py add (<address> [--pswd=<password>][--description=<description>] | --file <filename>)
    ovh_mails.py remove (<address> | --file <filename>)
    ovh_mails.py (-h | --help)

Arguments:
    <password>                        Password to access the mailbox (if not provided it's random generated)
    <filename>                        Name of the files to process. Check README to see how to format it

Options: 
    -h, --help                        Show this help message
    -u, --ugly                        Print without nice tables
    -p, --pswd=<password>  Set the password to the one provided

Commands:
    list                              list all the email addresses currently configured
    add                               add one or more (configured in <filename>) email addresses
    remove                            remove one ore more (configured in <filename>) email addresses'''

我现在拥有的参数的解析是:

if __name__ == '__main__':
args = docopt(__doc__)
#Validate args ---- TODO

# 'List' command parsing
if args['list']:
    if args['--ugly']:
        eman = EmailManager(niceoutput=False)
    else:
        eman = EmailManager()
    eman.list_emails()
# 'Add' command parsing
elif args['add']:
    if args['<address>']:
        eman = EmailManager()
        emails = (
                  {
                   'address': args['<address>'],
                   'password': None,
                   'description': None,
                   },
                  )
        if args['--description']:
            emails[0]['description'] = args['<description>']
        if args['--pswd']:
            emails[0]['password'] = args['<password>']
    if args['--file']:
        raise NotImplemented


    eman.add_emails(emails)


# 'remove' command parsing       
elif args['remove']:
    if args['<address>']:
        eman = EmailManager()
        emails = (
                  {
                   'address': args['<address>'],
                   },
                  )
    eman.remove_emails(emails)
    if args['--file']:
        raise NotImplemented

有没有更好,更pythonic矿石更优雅的方式,这样做?

1 回答

  • 0

    您可以为每个命令编写验证函数并将它们放入 validate 字典中:

    command = next((c for c in 'list add remove'.split() if args[c]), 'help')
    try:
        validate[command](args)
    except ValidationError as error:
        print_usage_and_exit(error)
    

    docopt recommends schema用于数据验证 .

相关问题