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

[question] Subcommand completion support for proper parsers #154

Open
nicholasjng opened this issue Oct 20, 2023 · 2 comments
Open

[question] Subcommand completion support for proper parsers #154

nicholasjng opened this issue Oct 20, 2023 · 2 comments

Comments

@nicholasjng
Copy link

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
Copy link
Collaborator

casperdcl commented Oct 20, 2023

pip ultimately relies on the deprecated optparse12 rather than the stdlib argparse3, so wouldn't work with shtab.

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

Footnotes

  1. https://github.com/pypa/pip/blob/5e7cc16c3b4442055a4a9892e9231758b6714e28/src/pip/_internal/cli/parser.py#L132

  2. https://docs.python.org/3/library/optparse.html

  3. https://docs.python.org/3/library/argparse.html#argumentparser-objects

@nicholasjng
Copy link
Author

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?

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

2 participants