with argparse, is it possible to check whether the first positional argument has been entered:
import argparse
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("object_type", type=str)
parser.add_argument("--pass1", type=str)
parser.add_argument("--pass2", type=str)
args = parser.parse_args()
print(args)
when calling: myscript.py –pass1=foo, I get the error:
error: the following arguments are required: object_type
what makes sense, but there don’t seem to be a way to catch the error beforehand.
Something like:
if args.object_type is None:
raise argparse.ArgumentError(message="object_type must be defined!")
tried also with try/except with no luck.
possible or not ?