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

Missing required subcommand does not print error #520

Closed
babysnakes opened this issue Jan 10, 2022 · 3 comments
Closed

Missing required subcommand does not print error #520

babysnakes opened this issue Jan 10, 2022 · 3 comments

Comments

@babysnakes
Copy link

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
Copy link
Contributor

epage commented Jan 10, 2022

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
Copy link
Contributor

epage commented Jan 10, 2022

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).

@babysnakes
Copy link
Author

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 🙂).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants