Replace the first parse_args
with
args,rest = parser.parse_known_args()
Now parser
handles input
, but ignores the rest (actually puts it in rest
).
However, conditionally setting the default after you are done with argparse
should work just as well. Conceptually it can be simpler.
Think about what you want to see when there's an error, or you ask for help. Does the default value need to be set?