TeXitoi / structopt

Parse command line arguments by defining a struct.
Other
2.7k stars 149 forks source link

Missing required subcommand does not print error #520

Closed babysnakes closed 2 years ago

babysnakes commented 2 years ago

First, just let me thank you for this amazing library 😄. It really is amazing.

I'm not sure whether this is caused by structopt or by clap, but I'm posting it here since this is what I use.

I created main.rs based on one of the examples in the docs:

use structopt::StructOpt;

#[derive(StructOpt, Debug)]
struct MakeCookie {
    #[structopt(name = "supervisor", default_value = "Puck", long = "supervisor")]
    supervising_faerie: String,
    /// The faerie tree this cookie is being made in.
    tree: Option<String>,
    #[structopt(subcommand)] // Note that we mark a field as a subcommand
    cmd: Command,
}

#[derive(StructOpt, Debug)]
enum Command {
    /// Pound acorns into flour for cookie dough.
    Pound {
        acorns: u32,
    },
    /// Add magical sparkles -- the secret ingredient!
    Sparkle {
        #[structopt(short, parse(from_occurrences))]
        magicality: u64,
        #[structopt(short)]
        color: String,
    },
    Finish(Finish),
}

// Subcommand can also be externalized by using a 1-uple enum variant
#[derive(StructOpt, Debug)]
struct Finish {
    #[structopt(short)]
    time: u32,
    #[structopt(subcommand)] // Note that we mark a field as a subcommand
    finish_type: FinishType,
}

// subsubcommand!
#[derive(StructOpt, Debug)]
enum FinishType {
    Glaze {
        applications: u32,
    },
    Powder {
        flavor: String,
        dips: u32,
    }
}

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

Running this example without providing subcommand does result in an error but does not print any error:

~\Code\tmp\rust-cmd master ❯ cargo build
    Finished dev [unoptimized + debuginfo] target(s) in 0.04s                         
~\Code\tmp\rust-cmd master ❯ .\target\debug\rust-cmd.exe -h
rust-cmd 0.1.0

USAGE:
    rust-cmd.exe [OPTIONS] [tree] <SUBCOMMAND>

FLAGS:
    -h, --help       Prints help information
    -V, --version    Prints version information

OPTIONS:
        --supervisor <supervisor>     [default: Puck]

ARGS:
    <tree>    The faerie tree this cookie is being made in

SUBCOMMANDS:
    finish
    help       Prints this message or the help of the given subcommand(s)
    pound      Pound acorns into flour for cookie dough
    sparkle    Add magical sparkles -- the secret ingredient!                                          
~\Code\tmp\rust-cmd master ❯ .\target\debug\rust-cmd.exe tree
rust-cmd 0.1.0

USAGE:
    rust-cmd.exe [OPTIONS] [tree] <SUBCOMMAND>

FLAGS:
    -h, --help       Prints help information
    -V, --version    Prints version information

OPTIONS:
        --supervisor <supervisor>     [default: Puck]

ARGS:
    <tree>    The faerie tree this cookie is being made in

SUBCOMMANDS:
    finish
    help       Prints this message or the help of the given subcommand(s)
    pound      Pound acorns into flour for cookie dough
    sparkle    Add magical sparkles -- the secret ingredient!
~\Code\tmp\rust-cmd master ❯ echo $?
False                                                                                                     

If I'm missing a regular positional argument it does prints the error:

~\Code\tmp\rust-cmd master ❯ .\target\debug\rust-cmd.exe pound
error: The following required arguments were not provided:
    <acorns>

USAGE:
    rust-cmd.exe pound <acorns>

For more information try --help

Is this a normal behavior?

Versions: rustc 1.56.1 (59eed8a2a 2021-11-01) structopt v0.3.25

Thanks

Haim

epage commented 2 years ago

This is because structopt (and clap_derive which is a fork of structopt) use SubcommandRequiredElseHelp. This makes it so its both an error and a shortcut to show help.

epage commented 2 years ago

btw in case you didn't know, clap 3 is out which integrates structopt directly into clap. structopt is now in maintenance mode (see https://github.com/TeXitoi/structopt/issues/516).

babysnakes commented 2 years ago

btw in case you didn't know, clap 3 is out which integrates structopt directly into clap. structopt is now in maintenance mode (see #516).

Oh cool, I didn't know it. I'll check it out. (also thanks for the explenation 🙂).