user4723845
user4723845

Reputation: 151

Python ArgParse

I want to use Python ArgParse to accept only certain inputs from the user.

So in below example let's say I want to accept 'type1/type2/type3' as argument only. Is that possible?

parser.add_argument('-t', '--type', type = str, help = 'type1/type2/type3')

Upvotes: 2

Views: 333

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121494

Use the choices argument to limit the input to a limited set of choices:

parser.add_argument('-t', '--type', choices=('type1', 'type2', 'type3'), 
                    help='type1/type2/type3')

Upvotes: 3

Related Questions