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

How to express "either ... or ..." relationship #141

Closed
vbrandl opened this issue Oct 8, 2018 · 6 comments
Closed

How to express "either ... or ..." relationship #141

vbrandl opened this issue Oct 8, 2018 · 6 comments
Labels
enhancement We would love to have this feature! Feel free to supply a PR need-design The concrete desing is uncertain, please get involved in the discussion before sending a PR

Comments

@vbrandl
Copy link

vbrandl commented Oct 8, 2018

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:

#[derive(Debug, StructOpt)]
enum Opt {
    #[structopt(name = "login")]
    Login { param: LoginCredentials },
    #[structopt(name = "logout")]
    Logout,
}
#[derive(Debug, StructOpt)]
enum LoginCredentials {
    UserPass { loginid: String, password: String },
    File { path: PathBuf },
}

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?

@TeXitoi
Copy link
Owner

TeXitoi commented Oct 11, 2018

@TeXitoi TeXitoi added the enhancement We would love to have this feature! Feel free to supply a PR label Oct 11, 2018
@vbrandl
Copy link
Author

vbrandl commented Oct 11, 2018

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?

@TeXitoi
Copy link
Owner

TeXitoi commented Oct 11, 2018

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.

@vbrandl
Copy link
Author

vbrandl commented Oct 11, 2018

Just a few thoughts:

  • How this might be implemented
#[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 },
}
  • Problem: Enums with variants that contain the same types and should be parsed as positional parameters cannot be distinguished and these cases should be compile errors
#[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

@TeXitoi TeXitoi added the need-design The concrete desing is uncertain, please get involved in the discussion before sending a PR label Oct 12, 2018
@TeXitoi
Copy link
Owner

TeXitoi commented Oct 12, 2018

This is similar to #104

@TeXitoi
Copy link
Owner

TeXitoi commented Jan 18, 2022

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

@TeXitoi TeXitoi closed this as completed Jan 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement We would love to have this feature! Feel free to supply a PR need-design The concrete desing is uncertain, please get involved in the discussion before sending a PR
Projects
None yet
Development

No branches or pull requests

2 participants