3

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?

2
  • Python supports multiple way to parse command line arguments. Which module or library you are using? Commented Feb 5, 2015 at 6:16
  • i am using python built in import argeparse module. Commented Feb 5, 2015 at 6:18

2 Answers 2

3

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']
Sign up to request clarification or add additional context in comments.

Comments

2

You want the 'append' action to add_argument. This will accumulate values into a list -- once for each time the command line argument is present. e.g.

parser = argparse.ArgumentParser()  # yadda yadda
parser.add_argument('-g', action='append')

fake_args_for_demo = '-g Linux -g ESX -g Windows'.split()
namespace = parser.parse_args(fake_args_for_demo)

print(namespace.g)  # ['Linux', 'ESX', 'Windows']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.