Closed vser1 closed 3 years ago
The completer is only considered when you're completing; it has no impact when you are actually running the program. If you want to enforce a restriction on the passed value, you will need to do so within argparse, or by validating the parsed arguments before you use them.
As an example (not tested):
import argcomplete
import argparse
def get_choices(**kwargs):
# Imagine this is something that takes a long time to compute.
return ('http', 'https', 'ssh', 'rsync', 'wss')
parser = argparse.ArgumentParser()
parser.add_argument('--proto').completer = get_choices
argcomplete.autocomplete(parser)
args = parser.parse_args()
if args.proto not in get_choices():
parser.error('invalid proto')
In the given example:
compare (assuming the resulting script is called foo):
Is there a way to raise such an error with completer (as for #137, one of my completion takes a lot of time to complete…) ?