python - Accessing the choices passed to argument in argparser? -


is possible access tuple of choices passed argument? if so, how go it

for example if have

parser = argparse.argumentparser(description='choose location') parser.add_argument(     "--location",     choices=('here', 'there', 'anywhere') ) args = parser.parse_args() 

can access tuple ('here', 'there', 'anywhere')?

it turns out parser.add_argument returns associated action. can pick choices off of that:

>>> import argparse >>> parser = argparse.argumentparser(description='choose location') >>> action = parser.add_argument( ...     "--location", ...     choices=('here', 'there', 'anywhere') ... ) >>> action.choices ('here', 'there', 'anywhere') 

note (afaik) isn't documented anywhere , may considered "implementation detail" , therefore subject change without notice, etc. etc.

there isn't publicly accessible way @ actions stored on argumentparser after they've been added. believe available parser._actions if you're willing go mucking implementation details (and assume risks involved that)...


your best bet create constant location choices , use in code:

location_choices = ('here', 'there', 'anywhere')  parser = argparse.argumentparser(description='choose location') parser.add_argument(     "--location",     choices=location_choices ) args = parser.parse_args()  # use location_choices down here... 

Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -