根据缺失的信息调整异常消息

时间:2015-02-20 11:41:02

标签: python python-2.7 command-line-arguments python-2.x command-line-parsing

有这样的代码:

if __name__ == "__main__":
    parser.add_argument("--u_shape_dict", default="", action='store',
                    help="required for ...")

    parser.add_argument("--v_shape_dict", default="", action='store',
                    help="required for ...")

    u_shape_dict = args['u_shape_dict']
    if not u_shape_dict:
        raise Exception('u_shape_dict is missing. Please provide it')

    v_shape_dict = args['v_shape_dict']
    if not v_shape_dict:
        raise Exception('v_shape_dict is missing. Please provide it')

我想将这两个例外合并为一个:

if not u_shape_dict or v_shape_dict:
    raise Exception('%s is missing. Please provide it' % )

有可能这样做吗? 在%签名后我需要放置什么?

1 个答案:

答案 0 :(得分:3)

您应该使用required选项,argparse将负责错误报告