Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use nosetests in python while also passing/accepting arguments for argparse?

I want to use nose and coverage in my project. When I run nose with --with-coverage argument, my programs argument-parsing module goes nuts because "--with-coverage" isn't a real argument according to it.

How do I turn the argparse off, but during testing only? Nose says all my tests fail because of the bad argument.

like image 522
Luis Uzher Avatar asked Mar 11 '15 01:03

Luis Uzher


People also ask

What does Nargs do in Argparse?

Number of Arguments If you want your parameters to accept a list of items you can specify nargs=n for how many arguments to accept. Note, if you set nargs=1 , it will return as a list not a single value.

How do you add arguments in Argparse?

After importing the library, argparse. ArgumentParser() initializes the parser so that you can start to add custom arguments. To add your arguments, use parser. add_argument() .

What does parser Add_argument do in Python?

The add_argument() methodDefine how a single command-line argument should be parsed. Each parameter has its own more detailed description below, but in short they are: name or flags - Either a name or a list of option strings, e.g. foo or -f, --foo .


1 Answers

I actually just ran into this issue myself the other day. You don't need to "disable" your parsing module or anything. What you can do is change the module that uses argparse to ignore those arguments it receives that it doesn't recognize. That way they can still be used by other scripts (for example if your command-line call passes secondary arguments to another program execution).

Without your code, I'll assume you're using the standard parse_args() method on your argparse.ArgumentParser instance. Replace it with parse_known_args() instead.

Then, whenever you subsequently reference the parsed-arguments Namespace object, you'll need to specify and element, specifically 0. While parse_args() returns the args object alone, parse_known_args() returns tuple: the first element is the parsed known arguments, and the latter element contains the ignored unrecognized arguments (which you can later use/pass in your Python code, if necessary).

Here's the example change from my own project:

class RunArgs(object):
    '''
    A placeholder for processing arguments passed to program execution.
    '''

    def __init__(self):
        self.getArgs()
        #self.pause = self.args.pause  # old assignment
        self.pause = self.args[0].pause  # new assignment
        #...

    def __repr__(self):
        return "<RunArgs(t=%s, #=%s, v=%s)>" % (str(x) for x in (self.pause,self.numreads,self.verbose))

    def getArgs(self):
        global PAUSE_TIME
        global NUM_READS
        parser = argparse.ArgumentParser()
        parser.add_argument('-p', '--pause', required=False, 
            type=self.checkPauseArg, action='store', default=PAUSE_TIME)
        parser.add_argument('-n', '--numreads', required=False, 
            type=self.checkNumArg, action='store', default=NUM_READS)
        parser.add_argument('-v', '--verbose', required=False,
            action='store_true')
        #self.args = parser.parse_args()  # old parse call
        self.args = parser.parse_known_args()  # new parse call
        #...
like image 146
TCAllen07 Avatar answered Oct 05 '22 04:10

TCAllen07