Skip to content

modify existing subcommands in Command struct #3881

@emersonford

Description

@emersonford

Please complete the following tasks

Clap Version

3.2.6

Describe your use case

I'm writing a Clap CLI that will eventually replace an existing Python CLI we have. The goal is to have this new CLI be the default one used and have it support fallback to the Python CLI for subcommands that are not yet implemented. Ideally, I'd like to dynamically generate the fallback subcommands as to not require any code changes / manually syncing between the Python version and the Clap/Rust version.

While .allow_external_subcommands gets me sort of there, but it gets messy in this instance:

  1. Python CLI has command foo generate bar and foo read bar
  2. New Clap CLI has command foo generate baz and foo read baz but not foo generate bar or foo read bar.

Currently, we'd have to add .allow_external_subcommands to both the generate subcommand and read subcommand, and implement duplicate matching logic for both. This also can't be done dynamically and we'd have to update the Rust/Clap CLI if there are any changes to the Python CLI.

What I'd ideally like is, traverse an existing Command we have (i.e. made with the Derive API) and insert subcommands found from the Python CLI command tree (which I can already generate) that are not in our current Command struct. This part is currently not possible as there's no way to modify existing subcommands as far as I'm aware. I can then have logic with get_matches() to detect when a fallback subcommand is used.

Describe the solution you'd like

There's already an API to modify existing args, mut_arg so I think the best solution would just be to duplicate this logic in a mut_subcommand method.

Alternatives, if applicable

No response

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory: Raise on the bar on expectations

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions