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.
As noted in the prior issue, I've been looking into integrating argcomplete into https://github.com/ipython/traitlets/pull/811.
Just wanted to jot down a couple of things that were done there, which perhaps could be upstreamed to argcomplete:
--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 theArgumentParser
instance. (I didn't want "--Class." itself to be directly completable, becauseargcomplete
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.sys.argv
and done independently fromargparse
. Sinceargcomplete
doesn't pass along any argv, I copied overargcomplete
's logic to parsecomp_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 thecomp_words
parsing logic to helper methods so that can be used by other scripts which do some manipulation ofsys.argv
prior to callingargparse
, and have a path for scripts to modify the current state ofcomp_words
.