TeXitoi / structopt

Parse command line arguments by defining a struct.
Other
2.7k stars 149 forks source link

Question: how the fields are required by subcommand #515

Closed JayiceZ closed 2 years ago

JayiceZ commented 2 years ago

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 commented 2 years ago

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.

TeXitoi commented 2 years ago

This is an enhancement, and structopt is now feature frozen.