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

Flattened enum panics when no subcommand is supplied #103

Closed
Jokler opened this issue Apr 28, 2018 · 5 comments
Closed

Flattened enum panics when no subcommand is supplied #103

Jokler opened this issue Apr 28, 2018 · 5 comments
Labels
bug This is a BUG. The fix may be released in a patch version even if considered breaking

Comments

@Jokler
Copy link

Jokler commented Apr 28, 2018

#[derive(StructOpt, Debug)]
struct Opt {
    #[structopt(short = "f")]
    foo: Option<String>,
    #[structopt(flatten)]
    sub_opt: SubOpt,
}

#[derive(StructOpt, Debug)]
enum SubOpt {
    Foo,
    Bar,
}

fn main() {
    let opt = Opt::from_args();
    println!("{:#?}", opt);
}
$ cargo run -- Foo
    Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs
     Running `target\debug\structopt-test.exe Foo`
Opt {
    foo: None,
    sub_opt: Foo
}
$ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs
     Running `target\debug\structopt-test.exe`
thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', libcore\option.rs:335:21
note: Run with `RUST_BACKTRACE=1` for a backtrace.
error: process didn't exit successfully: `target\debug\structopt-test.exe` (exit code: 101)

In clap I used AppSettings::SubcommandRequiredElseHelp for the behaviour I wanted and I expected something similar when I wrote this.

@Jokler Jokler changed the title Flattened enum panics without subcommand Flattened enum panics when no subcommand is supplied Apr 28, 2018
@TeXitoi TeXitoi added the bug This is a BUG. The fix may be released in a patch version even if considered breaking label Apr 28, 2018
@TeXitoi
Copy link
Owner

TeXitoi commented Apr 28, 2018

This is a bug as it should never panic, but using flatten on an enum is quite meaningless. What do you want to do? isn't more subcommand that you want instead of flatten?

@TeXitoi
Copy link
Owner

TeXitoi commented Apr 28, 2018

Fixed in v0.2.8

@Jokler
Copy link
Author

Jokler commented Apr 28, 2018

Yes, I do want subcommands but also the other Option.
Is there a better way to accomplish this?

Good to hear that it is fixed. :)

@TeXitoi
Copy link
Owner

TeXitoi commented Apr 28, 2018

In your example, using subcommand instead of flatten will do what you want. Now, with the corrected version, flatten and subcommand will do the exact same thing.

@Jokler
Copy link
Author

Jokler commented Apr 28, 2018

Ok, I see what you mean - thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This is a BUG. The fix may be released in a patch version even if considered breaking
Projects
None yet
Development

No branches or pull requests

2 participants