Help with the argparse library

2

I'm trying to use the argparse library to guide the main module ( __main__ ) between two possible executions:

import unittest
import argparse


arg = argparse.ArgumentParser(description='Execution type')
arg.add_argument('--type', action='store', dest='argument', type=int, default=0,
                 required=False, help='Chose the execution type (0=application, 1=unittest)')

options = arg.parse_args()


def main():
    pass


def run_tests():

    from tests import tests_models, tests_dbconnections, tests_dals

    suit = unittest.TestSuite()
    suit.addTest(unittest.makeSuite(tests_models.TestPerson))
    suit.addTest(unittest.makeSuite(tests_models.TestClient))
    suit.addTest(unittest.makeSuite(tests_dbconnections.TestPgSqlConnection))
    suit.addTest(unittest.makeSuite(tests_dals.TestPgSqlDal))

    return suit

if __name__ == '__main__':
    if options.argument:
        unittest.main(defaultTest='run_tests', verbosity=2)
    else:
        main()

But when running: python3 start.py --type 1

I get an error stating that the --type argument is not recognized: usage: start.py [-h] [-v] [-q] [--locals] [-f] [-c] [-b] [tests [tests ...]] start.py: error: unrecognized arguments: --type

    
asked by anonymous 28.07.2016 / 22:35

1 answer

1

This happens when unittest takes control, it will interpret the command line options again. --type is a valid argument for the main application, not for unittest .

You have to separate the command line options from unittest and the main application. One way to do this is to use argparse.parse_known_args() instead of argparse.parse_args() , see:

  

(Documentation)

     

Sometimes a script can only parse some of the command line arguments, passing the remaining arguments to another script or program.

     

In these cases, the parse_known_args() method can be useful. It works   as parse_args() except that it does not produce an error when arguments   extras are present. Instead, it returns a tuple of two   items that contain the namespace and the list of arguments remaining.

Code:

import argparse, unittest, sys

parser = argparse.ArgumentParser(description = 'Execution type')
parser.add_argument('--test', action = 'store_true',
                              help = 'Executar o unittest')

options, args = parser.parse_known_args()

def main():
    pass

def run_tests():
    from tests import tests_models, tests_dbconnections, tests_dals

    suit = unittest.TestSuite()
    suit.addTest(unittest.makeSuite(tests_models.TestPerson))
    suit.addTest(unittest.makeSuite(tests_models.TestClient))
    suit.addTest(unittest.makeSuite(tests_dbconnections.TestPgSqlConnection))
    suit.addTest(unittest.makeSuite(tests_dals.TestPgSqlDal))

    return suit

if __name__ == '__main__':
    if options.test:
        sys.argv[1:] = args
        unittest.main(defaultTest = 'run_tests', verbosity = 2)
    else:
        main()

To call unittest do:

python3 nomedoScript.py --test

To run the main application call the script without arguments:

python3 nomedoScript.py
    
29.07.2016 / 05:39