Solvedstructopt Support subcommand

The idea would be to create StructOpt struct for each subcommand, and then dispatching on an enum on the principal StructOpt.

19 Answers

✔️Accepted Answer

Added nested subcommands in williamyaoh@d5f029a
An example of nested subcommands + global flags:

#[derive(StructOpt)]
#[structopt(name = "foo-compiler")]
struct Args {
    #[structopt(short = "v", long = "verbose")]
    verbose: bool,
    #[structopt(subcommand)]
    cmd: Sub
}

#[derive(StructOpt)]
enum Sub {
    #[structopt(name = "compile")]
    Compile {
        #[structopt(short = "O", long = "opt")]
        optimization_level: u64,
        module: String,
        #[structopt(subcommand)]
        target: TargetType
    },
    #[structopt(name = "test")]
    Test {
        #[structopt(long = "iterations", default_value = "5")]
        iterations: u32,
        #[structopt(subcommand)]
        target: TargetType
    }
}

#[derive(StructOpt)]
enum TargetType {
    #[structopt(name = "debug")]
    Debug {},
    #[structopt(name = "release")]
    Release {},
    #[structopt(name = "doc")]
    Doc {}
}

Examples of allowed input:

  • foo-compiler --verbose compile -OOO some-module release
  • foo-compiler test --iterations 10 doc
  • foo-compiler compile another-module debug

I'll pretty it up and write some documentation, then open a PR for it. But for now, sleep...

Other Answers:

This is the biggest feature I'd like.

Related Issues:

5
structopt Support subcommand
Added nested subcommands in williamyaoh@d5f029a An example of nested subcommands + global flags: Exa...