argparser of python and logging the exception

Viewed 12
    import argparse
    import logging
handlers = [logging.FileHandler('logger.log',mode='a'), logging.StreamHandler() ] 
logging.basicConfig( level=logging.DEBUG,format='[%(levelname)s] %(message)s' ,handlers = handlers )

    parser = argparse.ArgumentParser();
    parser = argparse.ArgumentParser(description="./script.py --db <db path>  --scratch <scratch path> ")
    required_args = parser.add_argument_group('required arguments')
    required_args.add_argument('--db' , '-d', type=str, help='db directory name. Default is release',required=True)
    required_args.add_argument('--scratch', '-s', type=str, help='Scratch path where report will be written')

    try:
         args = parser.parse_args()
    except  SystemExit as se:
        logging.fatal("Invalid Command line argument:" )

My code is something like the above. I want to capture invalid exception arguments into the logger at the last line where I am capturing the exception. In precise whatever the message argparse is printing on stdout for any invalid arguments, I want to redirect all of them to my logger. Can you please let me how can I do it?

0 Answers
Related