My program has no command LINE options, but it parses and dispatches user commands typed as input. Can I use (say) argparse within the program to parse and dispatch commands typed as input? If not argparse, is there another suitable library for this? TIA
You theoretically could, but be aware that argparse will try to terminate the program at times.
>>> try:
... argparse.ArgumentParser().parse_args(["", "--help"])
... except BaseException as e:
... print("Caught:", type(e))
...
usage: [-h]
options:
-h, --help show this help message and exit
Caught: <class 'SystemExit'>
So if you want the full handling of --help
, --foo=bar
, etc, etc, etc, then argparse may be a decent choice, but make sure you wrap it in a good boundary layer.