iterative / shtab

↔️ Automagic shell tab completion for Python CLI applications
https://docs.iterative.ai/shtab
Other
362 stars 35 forks source link

[question] Subcommand completion support for proper parsers #154

Open nicholasjng opened 10 months ago

nicholasjng commented 10 months ago

Hey, thanks for creating this!

I wanted to start using it in a CLI project of my own, but before adoption, I am facing a usability question, which I was not able to find an immediate answer to.

Does shtab support the "one ArgumentParser per command class" use case?

Briefly explained, in this model, each CLI command inherits from a Command base class, which defines a parser for that specific argument only. Resolving the subcommand can then happen in an argument parser for the bare command. An example of a project using this is pip, with the base command implemented here.

Whereas for dvc, each command gets a subparser added to the single main dvc command parser, and the completion is added to the base parser, if I understood correctly.

The completion I am looking for would then do, say for two available subcommands bb and bbcc,

prog b<TAB>
# bb bbcc

Is this possible to do in current shtab if both of my bb and bbcc subcommands have their own argument parsers, as well as the base prog parser?

Thanks!

casperdcl commented 10 months ago

pip ultimately relies on the deprecated optparse^pip-optparse rather than the stdlib argparse^argparse, so wouldn't work with shtab.

However in general if you use argparse or argparse.ArgumentParser-like parsers, things should work fine.

nicholasjng commented 10 months ago

Sweet, thank you for the sources! For clarification, in order to achieve my subcommand completion example, would I need to add a subcommand argument to the main parser, with all available subcommands as choices, and then use shtab.CHOICE on that?