python 参数解析器_Python参数解析器,在h之前引发异常
我不知道為什么會這樣。我的理解是用戶至少有機會在執(zhí)行默認(rèn)操作之前使用-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()
現(xiàn)在,如果/usr/bin/igblastn在那里,那么就可以了,但是如果不是的話,只要調(diào)用這個程序就會在self\u check_中引發(fā)異常,如果有可執(zhí)行文件。
^{pr2}$
根據(jù)我的理解,用戶總是有機會運行--help或-h,或者在采取任何導(dǎo)致此參數(shù)錯誤的操作之前。我對arg解析器的理解不清楚嗎?
總結(jié)
以上是生活随笔為你收集整理的python 参数解析器_Python参数解析器,在h之前引发异常的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java mysql 全文索引_MySQ
- 下一篇: python中引入sql的优点_SQL