Mock command line arguments for Python script with `optparse`?

The Python script that I want to use (called snakefood ) usually runs from the command line and takes command line arguments, for example

 sfood /path/to/my/project 

Command line arguments are parsed in the gendeps.py file using optparse . However, I want to use the snakefood module from another script. Is there a way that I can somehow make fun of passing command line arguments to snakefood or a way to rewrite gendeps.py so that it no longer depends on optparse ?

+3
source share
1 answer

You can always assign a new sys.argv list:

 import sys sys.argv = ['programname', '-iq', '-q', directory] gendeps.gendeps() 

optparse uses sys.argv[1:] as input, unless explicit arguments are passed.

+10
source

Source: https://habr.com/ru/post/988550/


All Articles