Search code examples
pythonargument-passing

How to parse same argument twice or more than once to python script?


I have written a python script which uses argeparse module for handling arguments. e.g.

Test_Dual.py -g Linux

Now, I want to give it two options for same argument, like

Test_Dual.py -g Linux -g ESX -g Windows

How can I do that?


Solution

  • You can do as follows:

    import argparse
    
    parser = argparse.ArgumentParser(description=("Program desciption"))
    
    
    parser.add_argument("-g", "--full-name-of-g", action='append',
                        help="what is g option for")
    
    args = vars(parser.parse_args())
    
    print(args['full_name_of_g'])
    

    Prints:

    ['Linux', 'ESX', 'Windows']