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

Integration with traitlets #394

Open
azjps opened this issue Dec 17, 2022 · 0 comments
Open

Integration with traitlets #394

azjps opened this issue Dec 17, 2022 · 0 comments

Comments

@azjps
Copy link

azjps commented Dec 17, 2022

As noted in the prior issue, I've been looking into integrating argcomplete into ipython/traitlets#811.

Just wanted to jot down a couple of things that were done there, which perhaps could be upstreamed to argcomplete:

  • Dynamically adding options: traitlets supports reading arbitrary arguments of the form --Class.trait=value. It does this lazily (since there can be a lot of classes, some classes may not known at init time, etc). To handle this, I overrode _get_options_completions() to add additional "--Class." completions, and once there was only one class that could be completed, then overrode _get_completions to dynamically add the corresponding "--Class.trait" arguments to the ArgumentParser instance. (I didn't want "--Class." itself to be directly completable, because argcomplete would then automatically add a space afterwards.) This was mostly fine but perhaps could be nice to have dedicated entry points to modify these behaviors.
  • Parsing input words and changing which word to start from: for traitlets subcommand handling, its again evaluated lazily from the contents of sys.argv and done independently from argparse. Since argcomplete doesn't pass along any argv, I copied over argcomplete's logic to parse comp_words and passed that along as argv. After traitlets was able to then determine what subcommand was being used, I had to "tell" argcomplete to skip over the subcommand token, which was done a bit hackily via incrementing $_ARGCOMPLETE. I know this is pretty custom to traitlets, but it might be nice to refactor out some of the comp_words parsing logic to helper methods so that can be used by other scripts which do some manipulation of sys.argv prior to calling argparse, and have a path for scripts to modify the current state of comp_words.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant