Skip to content

external_subcommand doesn't work with only one arg #359

Description

@scrogson

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugThis is a BUG. The fix may be released in a patch version even if considered breaking

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions