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

Validate interaction between args and subcommands #3135

Open
epage opened this issue Dec 9, 2021 · 3 comments
Open

Validate interaction between args and subcommands #3135

epage opened this issue Dec 9, 2021 · 3 comments
Labels
A-validators Area: ArgMatches validation logi C-enhancement Category: Raise on the bar on expectations S-waiting-on-design Status: Waiting on user-facing design to be resolved before implementing

Comments

@epage
Copy link
Member

epage commented Dec 9, 2021

Issue by JayiceZ
Monday Dec 06, 2021 at 10:56 GMT
Originally opened as TeXitoi/structopt#515


Hi, I am new to structopt. And I am not sure is it a structopt issue or a clap issue? Here is the situation I met:

I defined it, like:

struct Opt {
    #[structopt(long)]
    cat: Option<String>,

    #[structopt(long)]
    dog: Option<String>,

    #[structopt(long)]
    host: Option<String>,

    #[structopt(subcommand)]
    cmd: Option<Cmd>,
}

enum Cmd {
    Test {
        #[structopt(long)]
        dir: Option<String>,
    },
    Debug{
        #[structopt(long)]
        remote: Option<String>,
    },
}

And what I want is: When Test SubCommand is set, one of cat and dog should be set(or it means that they are in a Group, and the group is required when Test is set); And when Debug is set, host should be set.

I have read the doc and examples, but I am not sure how to implement this function? 😭

@epage
Copy link
Member Author

epage commented Dec 9, 2021

Comment by epage
Monday Dec 06, 2021 at 12:46 GMT


From what I've seen so far, clap allows validation of relationship of args and groups but subcommands are effectively in another namespace, preventing them from interacting with that system.

In clap3, there is App::error to create clap-like errors for custom validation. There is also interest in generalizing clap's mechanisms so people are less likely to run into hurdles like this.

@epage epage added A-validators Area: ArgMatches validation logi C-enhancement Category: Raise on the bar on expectations labels Dec 9, 2021
@epage epage changed the title Question: how the fields are required by subcommand Validate interaction between args and subcommands Dec 9, 2021
@epage
Copy link
Member Author

epage commented Dec 13, 2021

This is also related to #1204

@epage epage added the S-waiting-on-design Status: Waiting on user-facing design to be resolved before implementing label Dec 13, 2021
@epage
Copy link
Member Author

epage commented Apr 15, 2022

See #3008 which should allow users to do this themselves

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-validators Area: ArgMatches validation logi C-enhancement Category: Raise on the bar on expectations S-waiting-on-design Status: Waiting on user-facing design to be resolved before implementing
Projects
None yet
Development

No branches or pull requests

1 participant