When I'm writing a command line script I oftentimes will specifically design my script so this is possible. The key is to parse the args separate from the main function.
For example the main function might look like this:
def main(**kwargs):
# the body of the script goes here
Then elsewhere in the module I will configure the arg parser, parse the args and pass the result into the main script:
def run():
parser = ... # configure parser here
configs = parse_args(parser)
main(**configs)
That way, if someone wants to call the script from within Python, they can do so (it also makes testing much easier):
import somescript
somescript.main(option='value', option2='value2')
Unfortunately, it appears that the authors of the script you are using did not do anything like that. As stated in another answer you can overwrite sys.argv
, then import the script. While that may feel hacky, it should be less resource intensive than opening a new process and calling the command separately.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…