Question Details

No question body available.

Tags

python argparse

Answers (2)

January 31, 2026 Score: 0 Rep: 192,007 Quality: Medium Completeness: 80%

Argparse does not have a built-in argument type that supports what you want, but it can be extended with a custom argument type to add support. For example,

from argparse import ArgumentTypeError

def myargumentconverter(arg): allowed = 'ARLQTNZCFXPB' for c in arg: if c not in allowed: raise ArgumentTypeError(f'{c} is not among "{allowed}"', help='Control Options')

return arg

parser.register('type', 'options type', myargumentconverter) parser.add_argument('-o', '--options', type='options type')

That will validate the argument against your string of allowed options, and store the whole string if it passes. If you prefer, it could perform additional validations, and / or it could return a different value to store in the argument namespace, such as a list of the individual options.

January 31, 2026 Score: -1 Rep: 21,497 Quality: Low Completeness: 50%

The code you wrote expects the user to enter -o A -o C .

If accepting args like BCA and ABC is a Requirement for your app, then you might prefer to do custom app-specific error checking outside of what argparse offers, and raise a diagnostic error upon seeing a letter not in "ABCFLNPQRTXZ" .

One big advantage of using argparse is you get instructional --help "for free". That won't be the case if there's a project requirement to support the example inputs you mentioned.