how to pass argparse arguments to another python program in unchanged condition?
Question: I am working on a script under which some sub program will run. for example
**test.py** is the main program and under that **test1.py** , **test2.py** ,
**test3.pl** will run and I need to pass the arguments from the main
program(test.py) to test1.py and test2.py program also.the arguments should be
in unchanged condition. while passing to another program.

code: **test.py**

    
    
    import argparse
    import subprocess
    import os
    
    commandLineArgumentParser = argparse.ArgumentParser()
    commandLineArgumentParser.add_argument("-fname", "--fname",help="first name")
    commandLineArgumentParser.add_argument("-lname","--lname", help="last name")
    commandLineArgumentParser.add_argument("-age","--age", help="age")
    commandLineArguments = commandLineArgumentParser.parse_args()
    
    fname = commandLineArguments.fname
    lname = commandLineArguments.lname
    age = commandLineArguments.age
    
    print "%s%s%s" %(fname,lname,age)
    

I am running the program by the bellow commands :

    
    
    python test.py -fname=abc -age=22 -lname='test a'
    or 
    python test.py -fname="abc test" lname='val' -age=30
    or
    python test.py -age=45 -lname='abc aa' fname="abc"
    or 
    python test.py -age=45 -lname="test"
    

now I want to grab the argument part in unchanged condition and put in one
variable then we can easily pass the arguments to program easily.

For the first command the variable will hold

    
    
    -fname=abc -age=22 -lname='test a'
    

for 2nd command

    
    
    -fname="abc test" lname='val' -age=30
    

I was trying to grab the arguments using the bellow code but the quotas are
missing by the script.

    
    
    my_argu=''
    if len(sys.argv) >1:
        for x in sys.argv:
                if x == sys.argv[0]:
                        continue
                if x == sys.argv[len(sys.argv)-1]:
                        my_argu =my_argu+x
                else:
                        my_argu = my_argu+x+' '
    print "%s" %(my_argu)
    

for the

    
    
    python test.py -fname="abc test" lname='val' -age=30
    

the output is :

    
    
    abc testval30
    
    -fname=abc test lname=val -age=30
    

as you can see quotas are missing. So I need help to solve it


Answer: Seems like you should pull these all together in one wrapper and call that
instead.

    
    
    # wrapper.py
    
    import test, test1, test2, test3
    import argparse
    
    parser = argparse.ArgumentParser()
    parser.add_argument("-fname", "--fname",help="first name")
    parser.add_argument("-lname","--lname", help="last name")
    parser.add_argument("-age","--age", help="age")
    cli_args = parser.parse_args()
    
    test.run(cli_args)
    test1.run(cli_args)
    test2.run(cli_args)
    test3.run(cli_args)
    

Then in each of your `testN.py`s...

    
    
    # test.py
    
    def run(cli_args):
        print("Your name is {} {}".format(cli_args.fname, cli_args.lname))
        # or whatever your program does....
    

Then do:

    
    
    $ python wrapper.py --fname Adam --lname Smith
    

