How to add optional or once arguments?
How can I add an argument that is optional and must not be specified multiple times? Valid: $ ./my.py $ ./my.py --arg MyArgValue Invalid: $ ./my.py --arg MyArgValue --arg ThisIsNotValid If I add an argument like: parser.add_argument('--arg', type=str) The invalid example results in a string ThisIsNotValid . I would expect a parser error. Create a custom action that raises an exception if the same argument is seen twice. When the parser catches the exception, it prints the usage and a nicely-formatted error message. import argparse class Highlander(argparse.Action): def __call__(self, parser,