Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 456
akkidukes Avatar asked Sep 03 '26 12:09

akkidukes


1 Answers

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']
like image 162
Marcin Avatar answered Sep 06 '26 02:09

Marcin