We rewrite the given code as follows
#foo.py
import argparse
class InvalidArgError(Exception):pass
parser = argparse.ArgumentParser()
parser.add_argument("echo")
args = parser.parse_args()
try:
print (args.echo)
raise InvalidArgError
except InvalidArgError as e:
print eWhen this script is run at the terminal as follows
$ python foo.py echo bar
We get the following output
usage: foo.py [-h] echo foo.py: error: unrecognized arguments: bar