python 参数解析器_Python参数解析器,在h之前引发异常

我不知道为什么会这样。我的理解是用户至少有机会在执行默认操作之前使用-h。import os, sys, argparse

class argument_parser():

# call on all our file type parsers in the sequence_anlysis_method

def __init__(self):

self.db_directory = os.path.dirname(os.path.abspath(sys.argv[0]))

"""A customized argument parser that does a LOT of error checking"""

self.parser = argparse.ArgumentParser(

prog="igblast")

general = self.parser.add_argument_group(

title="\nGeneral Settings")

general.add_argument(

"-x", '--executable',

default="/usr/bin/igblastn",

type=self._check_if_executable_exists,

help="The location of the executable, default is /usr/bin/igblastn")

self.args = self.parser.parse_args()

def _check_if_executable_exists(self,x_path):

if not os.path.exists(x_path):

msg = "path to executable {0} does not exist, use -h for help\n".format(x_path)

raise argparse.ArgumentTypeError(msg)

if not os.access(x_path, os.R_OK):

msg1 = "executable {0} does have not permission to run\n".format(x_path)

raise argparse.ArgumentTypeError(msg1)

else:

return x_path

if __name__ == '__main__':

argument_class = argument_parser()

现在,如果/usr/bin/igblastn在那里,那么就可以了,但是如果不是的话,只要调用这个程序就会在self\u check_中引发异常,如果有可执行文件。

^{pr2}$

根据我的理解,用户总是有机会运行--help或-h,或者在采取任何导致此参数错误的操作之前。我对arg解析器的理解不清楚吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值