Skip to content

Groups within subcommands not working as expected #151

Description

@j-browne

I am trying to get a program with subcommands, one of which requires at least one flag. So, for example, I would want the following behavior:

  • cargo run -- a fails
  • cargo run -- a -c succeeds
  • cargo run -- a -d succeeds
  • cargo run -- a -cd succeeds

I can get it working using a clap::ArgGroup without subcommands, but not with subcommands.
For example, given the following

use structopt::clap::ArgGroup;
use structopt::StructOpt;

fn arg_group() -> ArgGroup<'static> {
    ArgGroup::with_name("group").required(true).multiple(true)
}

#[derive(StructOpt, Debug)]
#[structopt(raw(group = "arg_group()"))]
struct Sub {
    #[structopt(short = "c", group = "group")]
    c: bool,
    #[structopt(short = "d", group = "group")]
    d: bool,
}

#[derive(StructOpt, Debug)]
enum Opt {
    #[structopt(name = "a", raw(group = "arg_group()"))]
    A (Sub),
    #[structopt(name = "b")]
    B {},
}

this works (though not as a subcommand)

fn main() {
    let opt = Sub::from_args();
    println!("{:?}", opt);
}

but this does not

fn main() {
    let opt = Opt::from_args();
    println!("{:?}", opt);
}

Instead, I get the following behavior:

  • cargo run -- a fails
  • cargo run -- a -c succeeds
  • cargo run -- a -d succeeds
  • cargo run -- a -cd fails

So I'm not sure if I'm doing something wrong or if perhaps its a bug in structopt.

I'm using structopt 0.2.13 and rust nightly (6acbb5b65 2018-11-25).

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugThis is a BUG. The fix may be released in a patch version even if considered breakinghelp wantedYour help is needed! Feel free to participate in the discussion or send a PR

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions