The Python Oracle

Allowing specific values for an Argparse argument

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Horror Game Menu Looping

--

Chapters
00:00 Question
00:47 Accepted answer (Score 330)
01:09 Thank you

--

Full question
https://stackoverflow.com/questions/1583...

Question links:
[argparse]: https://docs.python.org/library/argparse...
[print_help()]: https://docs.python.org/library/argparse...

Accepted answer links:
[choices]: https://docs.python.org/library/argparse...
[docs]: https://docs.python.org/library/argparse...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #argparse

#avk47



ACCEPTED ANSWER

Score 367


An argparse argument can be limited to specific values with the choices parameter:

...
parser.add_argument('--val',
                    choices=['a', 'b', 'c'],
                    help='Special testing value')

args = parser.parse_args(sys.argv[1:])

See the docs for more details.