I'm considering adding argparse to existing batch jobs which run python. These jobs take command line arguments which can vary depending on how the jobs are to run. I'd like to use the named argument feature in argparse but instead of exiting the job if an argument is eg. missing or misnamed, I'd like to trap that error and hand the subsequent processing off to existing python routines which handle the job exit. These routines create logs, backout db transactions, flush files to disk and email whoever is responsible for the job of the problem encountered.
I've tried using python try except block (below) to trap the error but it fails. I've also seen solutions which involve subclassing argparse but this results in a much more complicated code base than prior to introducing argparse and is therefore counter-productive.
Is there a simple solution to this problem I'm missing? Many thanks.
try:
args = my_parser.parse_args()
except Exception as parseError:
do my stuff