-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
How to express "either ... or ..." relationship #3121
Comments
Comment by TeXitoi You might want to look at https://github.com/TeXitoi/structopt/blob/master/examples/group.rs and TeXitoi/structopt#126 |
Comment by vbrandl I found that one already but I think it would be cool if we were able to describe groups using enums. That way it is impossible to represent an illegal state and you could simply match over the group variants. Is it possible to implement this for structopt? |
Comment by TeXitoi That's a new feature, that's why I've tagged this issue enhancement. Then, we can design the details here, and anyone can implement that and do a PR to add it. I will not implement this feature in short term, as I have others priorities, in this repo and in others. But I encourage anyone interested to contribute, and I'll try to answer any questions as fast as possible. |
Comment by vbrandl Just a few thoughts:
#[derive(Debug, StructOpt)]
#[structopt(type = "subcommand")] // this should be implicit
enum Opt {
#[structopt(name = "login")]
Login { param: LoginCredentials },
#[structopt(name = "logout")]
Logout,
}
#[derive(Debug, StructOpt)]
#[structopt(type = "group")]
enum LoginCredentials {
UserPass { loginid: String, password: String },
File { path: PathBuf },
}
#[derive(Debug, StructOpt)]
#[structopt(type = "group")]
enum Group1 {
Var1 { a: String, b: String },
Var2 { c: String, d: String }, // this should fail
}
#[derive(Debug, StructOpt)]
#[structopt(type = "group")]
enum Group2 {
Var1 {
#[structopt(short = "a")]
a: String,
b: String
},
Var2 { c: String, d: String },
} // this should work since the variants are distingushable |
I believe this is covered by #2621 If there is something I'm missing though, let me know! |
Issue by vbrandl
Monday Oct 08, 2018 at 14:58 GMT
Originally opened as TeXitoi/structopt#141
I want to express the following relationship using structopt: The positional parameter should either be two strings (username and password) or a PathBuf (from where the credentials will be read).
I tried the following:
But this will create another subcommand for the
login
subcommand. What would be the right way to express this relationship? Is there even a way to do so?The text was updated successfully, but these errors were encountered: