Complete worked example
python
import argparse
class InputParser(argparse.ArgumentParser):
def error(self, message):
raise ValueError(message)
def options(arguments):
parser = InputParser(add_help=False, allow_abbrev=False)
parser.add_argument("--country", type=str.upper, choices=["US", "IL", "UA", "CA"], default="US")
parser.add_argument("--limit", type=int, default=2)
result = parser.parse_args(arguments)
if not 1 <= result.limit <= 5:
raise ValueError("limit must be 1..5")
return {"country": result.country, "limit": result.limit}- Reasoning
- Parse predictable flags without hiding unknown arguments or terminating the calling process on validation errors. Compare the stated values and failure boundary before changing the implementation.