argcomplete
argcomplete copied to clipboard
In a REPL environment, patched parsers incorrectly assume subparsers are patched.
I say a REPL environment, but this really applies to any environment where an ArgumentParser is used for both parsing and completion during a single run of the program.
When parsing (not completing), newly encountered subparsers are not patched, but the parser that calls the subparser may have been patched by an earlier completion. This means that if a parsing error occurs in the subparser, the patched parser attempts to combine the subparser's _argcomplete_namespace
member (which doesn't exist) with the parent namespace, causing an uncaught AttributeError.
As a workaround I could create two different ArgumentParsers, one for parsing and one for completion, but that seems a bit messy.
Thanks for the report. Could you provide a minimal example that demonstrates the failure you're seeing?
Sure, here's one.
import argparse
import argcomplete
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
subparsers.add_parser('test').add_argument('value', choices=['valid'])
completer = argcomplete.CompletionFinder(parser)
# Complete in the main parser scope so it gets patched
i = 0
while True:
completion = completer.rl_complete('tes', i)
if completion is None:
break
print completion
i += 1
# Error inside the unpatched subparser, throws an unhandled exception
parser.parse_args(['test', 'invalid'])