Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple subparsers leads to recursive variables #72

Closed
lcannon opened this issue Jan 14, 2014 · 5 comments
Closed

Multiple subparsers leads to recursive variables #72

lcannon opened this issue Jan 14, 2014 · 5 comments

Comments

@lcannon
Copy link

lcannon commented Jan 14, 2014

#!/usr/bin/python
# PYTHON_ARGCOMPLETE_OK
import argparse
import argcomplete
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest='command')
parser1 = subparsers.add_parser('local')
parser1.add_argument('name', choices=['zzz', 'aaa'])
parser2 = subparsers.add_parser('lookup')
parser2.add_argument('dest', choices=['a', 'b', 'c'])
sub2 = parser2.add_subparsers(dest='type')
parser3 = sub2.add_parser('env')
parser3.add_argument('name', choices=['zzz', 'aaa'])
parser4 = sub2.add_parser('etc')
parser4.add_argument('file', choices=['my.cnf', 'apache.conf'])

argcomplete.autocomplete(parser)
data = parser.parse_args()

If you go one subparser deep, into the local branch, this works as expected. Once you fill out zzz or aaa, you're no longer offered autocomplete choices.

$ ./testcase.py local zzz [tab][tab]
$

If you go into the lookup branch and hit the second subparser, the subparser choice is presented to you as many variables deep as you care to go.

e.g.

$ ./testcase.py lookup env env env [tab][tab]
env     etc     -h      --help 
$

Interestingly, if a non subparser argument is added before the inital one, you get offered the correct choices mostly (when you hit a,b,c you get env and etc in the options as well, if you pick a, b, or c completion works properly from then on, but if you choose etc or env, it goes back into looping forever).

@lcannon
Copy link
Author

lcannon commented Jan 14, 2014

Forgot to say, I just upgraded to the latest and greatest from pip, 0.6.7,

@kislyuk
Copy link
Owner

kislyuk commented Jan 15, 2014

Yeah, nested subparsers behave weirdly. When I looked into it, I found that figuring out which subparsers are eligible to receive the next word is really hard, so for now I just throw all subparsers that I can find into the mix of active parsers. If you have a better way to determine active subparsers, by all means please submit a patch. The relevant code is in https://github.com/kislyuk/argcomplete/blob/master/argcomplete/__init__.py#L189.

@bbejot
Copy link

bbejot commented Jan 11, 2017

I tried this out and it seems to be fixed. I used version 1.6.0

$ ./testcase.py lookup [tab][tab]
a b c -h --help
$ ./testcase.py lookup a [tab][tab]
env etc -h --help
$ ./testcase.py lookup a env [tab][tab]
aaa -h --help zzz
$ ./testcase.py lookup a env aaa [tab][tab]
-h --help

This looks like correct behavior to me and seems to be inline with what argparse is expecting.

@kislyuk kislyuk closed this as completed Jan 11, 2017
@kislyuk
Copy link
Owner

kislyuk commented Jan 11, 2017

Thanks for verifying, @bbejot.

@evanunderscore
Copy link
Collaborator

@bbejot Would you like to submit a PR for a test case that verifies this? If not I'll add one when I get around to it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants