Use argparse's choices option to restrict an argument to a defined collection of allowed values. argparse validates the input before program processing continues. section 8.3.1 section 8.6
Restricting a vowel argument
A vowel-substitution program can accept only lowercase vowels by declaring choices=list('aeiou'). This produces the same list as ['a', 'e', 'i', 'o', 'u']; an assertion can verify the equivalence. section 8.3.1
parser.add_argument('-v',
'--vowel',
help='The vowel to substitute'',
metavar='str',
type=str,
default='a',
choices=list('aeiou'))
The argument declaration shows choices alongside type, default, and metavar settings. section 8.3.1
The same validation approach handles Tic-Tac-Toe options such as player and cell, alongside checks that require both options or neither. section 21.2.1
