I don't know why this is happening. It was my understanding that the user would at least get a chance to use -h before the default actions were executed.
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()
Now if /usr/bin/igblastn is there, then it's fine but if its not, just calling on this program raises the exception in self_check_if_executable_exists.
File "execution.py", line 220, in <module>
argument_class = ap()
File "/home/willisjr/utilities/pyig/src/arg_parse.py", line 159, in __init__
self.args = self.parser.parse_args()
File "/usr/local/lib/python2.7/argparse.py", line 1656, in parse_args
args, argv = self.parse_known_args(args, namespace)
File "/usr/local/lib/python2.7/argparse.py", line 1678, in parse_known_args
default = self._get_value(action, default)
File "/usr/local/lib/python2.7/argparse.py", line 2203, in _get_value
raise ArgumentError(action, msg)
argparse.ArgumentError: argument -x/--executable: path to executable /usr/bin/igblastn does not exist, use -h for help
It was my understanding that the user always had a chance to run --help or -h or before any action was taken resulting in this argument error. Is my understanding of the arg parser not clear?
3.3.1and newer. I'm not sure about the2.7side. See my answer.