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

external_subcommand doesn't work with only one arg #359

Closed
scrogson opened this issue Mar 13, 2020 · 3 comments · Fixed by #361
Closed

external_subcommand doesn't work with only one arg #359

scrogson opened this issue Mar 13, 2020 · 3 comments · Fixed by #361
Labels
bug This is a BUG. The fix may be released in a patch version even if considered breaking

Comments

@scrogson
Copy link

scrogson commented Mar 13, 2020

I was excited to see #314 land, but I've not been able to get it working in my project. In order to sanity check myself I copied out a bare minimum based on the examples and tests in that PR but I'm still getting the same runtime panics.

use structopt::StructOpt;

#[derive(Debug, PartialEq, StructOpt)]
struct Opt {
    #[structopt(subcommand)]
    sub: Subcommands,
}

#[derive(Debug, PartialEq, StructOpt)]
enum Subcommands {
    Add,

    #[structopt(external_subcommand)]
    Other(Vec<String>),
}

fn main() {
    let opts = Opt::from_args();
    println!("{:?}", opts);
}
λ cargo run -q -- add
Opt { sub: Add }
λ cargo run -q -- blah
thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', src/main.rs:14:15
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Here are the relevant backtrace lines:

λ env RUST_BACKTRACE=1 cargo run -q -- blah
thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', src/main.rs:14:15
stack backtrace:
...
  14: core::option::Option<T>::unwrap
             at /rustc/b8cedc00407a4c56a3bda1ed605c6fc166655447/src/libcore/macros/mod.rs:10
  15: <external::Subcommands as structopt::StructOptInternal>::from_subcommand
             at src/main.rs:14
  16: <external::Opt as structopt::StructOpt>::from_clap
             at src/main.rs:5
  17: structopt::StructOpt::from_args
             at /home/scrogson/.cargo/registry/src/github.com-1ecc6299db9ec823/structopt-0.3.11/src/lib.rs:1073
  18: external::main
             at src/main.rs:18
...

It's unclear to me if I'm doing something wrong and there doesn't seem to be a working example in the examples directory.

Thanks in advance for your help!

@CreepySkeleton
Copy link
Collaborator

Yes, you are doing it wrong; and yes, we do have a bug in structopt.

Listen, digest, and memorize; for I am speaking the sacred truth.

What's wrong with your input?

  • One: Subcommands must go before the --! Could you please describe what gave you the idea that subcommands must go after --? I'm writing a FAQ guide, and if I get to understanding what drives people forward the common mistakes, it will contain much less profanity and expletive language in general.

  • Two: you're using the -q option before the subcommand. Put it simply, the option is global, it doesn't belong to the external subcommand, and your app doesn't know anything about this option. Introduce it explicitly, or put it after foo.

    It also gives me impression that what you really want is Arg::raw, as described there, courtesy of Me the Magnificent and Exceptionally Handsome .

And the bug is, specifically? It doesn't handle the case with only one parameter correctly, e.g cargo run -- foo will fire. I'm working on it... Fuck, so many things to work on... I really do need a time machine.

@CreepySkeleton CreepySkeleton changed the title Unable to implement external subcommands successfully external_subcommand doesn't work with only one arg Mar 14, 2020
@CreepySkeleton CreepySkeleton added the bug This is a BUG. The fix may be released in a patch version even if considered breaking label Mar 14, 2020
@scrogson
Copy link
Author

The -q option is for cargo run to suppress the compilation output. And the -- is (AFAIK) to end the options for cargo run and process those as options for my CLI. 🤷‍♂

@CreepySkeleton
Copy link
Collaborator

Then you are doing it right, and it's just we with the bug deep inside. Awful.

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
2 participants