【问题标题】:Adjust exception message basing on missing information根据缺失信息调整异常消息
【发布时间】:2015-04-22 01:57:25
【问题描述】:

有这样的代码:

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' % )

有可能吗? 实际上我需要在% 符号之后添加什么?

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-19
      • 1970-01-01
      • 2015-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多