kislyuk / argcomplete

Python and tab completion, better together.
https://kislyuk.github.io/argcomplete/
Apache License 2.0
1.39k stars 129 forks source link

Completions for subparsers? #402

Closed excited-bore closed 1 year ago

excited-bore commented 1 year ago

Not to be too much of a rerun of Issue#114, but I'm failing miserably at integrating completions for subparsers.

For example, this is a rewrite of the example code mentioned in the README For me, the arguments "add" and the subparsing arguments "--member and --organization" show up when running this, but the completer for "--member" fails. Sorry in advance if this just doesn't make sense.

#!/usr/bin/env python
# PYTHON_ARGCOMPLETE_OK
import argcomplete, argparse, requests, pprint

def github_org_members(prefix, parsed_args, **kwargs):
    resource = "https://api.github.com/orgs/{org}/members".format(org=parsed_args.organization)
    return (member['login'] for member in requests.get(resource).json() if member['login'].startswith(prefix))

parser = argparse.ArgumentParser()
subparser = parser.add_subparsers()
add = subparser.add_parser("add")
add.add_argument("--organization", help="GitHub organization")
add.add_argument("--member", help="GitHub member").completer = github_org_members

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

pprint.pprint(requests.get("https://api.github.com/users/{m}".format(m=args.member)).json())

As a sidenote, i'm running Python 3.10.9 .
Also many thanks for making this awesome thing.

kislyuk commented 1 year ago

Thanks for your interest in argcomplete. Your script works fine for me on all supported versions of Python. Are you sure you have requests and argcomplete installed in the Python that is active in your shell?

I'm going to close this issue for now. If you continue to encounter it, please feel free to reopen or open a new issue with complete steps to reproduce.