Open aspitarl opened 2 years ago
I think what's happening here is that the check for "is this required argument present" runs before the type
function executes, so we never have the chance to intercept the argparse behaviour when it's required. I'm not sure, but I don't think there's any easy way to solve this without either monkey patching argparse (which wouldn't be good), or else rewriting argparse itself.
In your case I recommend setting the argument to optional, but then pass in a type function that checks that it's not null and throws a ArgumentTypeError, TypeError, or ValueError if it is. This should work well enough, although the error message might appear slightly different to base argparse in this case.
Actually this solution could easily be pushed up into the repo:
required
argument.None
-nessNone
, we throw one of the above errors
I have a script with a required argument 'datestr' that I usually provide as a positional argument
This does not work with PromptParser giving
error: the following arguments are required: datestr
. However, this does work when changing to an 'optional' argument.Where it works as expected.
It seems like the behavior of both arguments should be the same with PromptParser. Unless I'm missing something, I'm guessing this is just not implemented yet. This seems like it would be useful as positional arguments are usually required.