Calling a function that uses argparse

how do I call a function that uses argparse through another function.

def func():
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('env_name', type=str)
    parser.add_argument('--exp_name', type=str, default='vpg')
    args = parser.parse_args()

    ~~~ other code here that uses 'args'~~~

def wrap_func()
    func(ARGUMENT_PASSING_NOT_VIA_CMD)

wrap_func()

I want to debug it as well so os.system(...) isn’t good for me.

Answer

In your example, you can just add a list of arguments to your func function which is then passed on to parse.parse_args(). And maybe you should move the import argparse to the beginning of the script with other imports.

def func(arg_list):
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('env_name', type=str)
    parser.add_argument('--exp_name', type=str, default='vpg')
    args = parser.parse_args(arg_list)

    ~~~ other code here that uses 'args'~~~

def wrap_func()
    func(list_of_arguments_you_want_to_pass)

wrap_func()

You can find some examples here.

Leave a Reply

Your email address will not be published. Required fields are marked *