Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call another module with passing its command line parameter from my module using argparse in python

I searched and tried following stuff but could not found any solution, please let me know if this is possible:

I am trying to develop a python module as wrapper where I call another 3rd party module with its .main() and provide the required parameter which I need to get from command line in my module. I need few parameter for my module too.

I am using argparse to parse command line for calling module and my module. The calling parameter list is huge (more than 40) which are optional but may require anytime who will use my module. Currently I have declared few important parameters in my module to parse but I need to expand with all the parameter.

I thought of providing all the parameter in my module without declaring in add_argument. I tried with parse_known_args which also require declaration of all parameter is required.

Is there any way where I can pass on all parameter to calling module without declaring in my module? If its possible please let me know how it can be done.

Thanks in advance,

like image 335
Chetan Avatar asked Jul 06 '26 11:07

Chetan


1 Answers

Thanks hpaulj, mguijarr and mike,

I was able to resolve the issue with all above inputs, like following:

My module:

import sys
import argparse
parser = argparse.ArgumentParser(description='something')
parser.add_argument('--my_env', help='my environment')

if __name__=='__main__':
    args,rest = parser.parse_known_args()
    rest_arg = ['calling_module.py']
    rest_arg.extend(rest)
    sys.argv = rest_arg
    import calling_module
    calling_module.main()
like image 51
Chetan Avatar answered Jul 11 '26 13:07

Chetan