Structopt: Ignore the first argument / cargo-subcommand mode

Created on 7 Feb 2018  路  2Comments  路  Source: TeXitoi/structopt

When writing a cargo subcommand, e.g., cargo foo, the first argument that the subcommand gets passed is its name, as a string (foo) in this case. So if I want to write a sub-command foo that takes one string as argument and call it like cargo foo bar, right now I need to write

#[derive(StructOpt, Debug)]
#[structopt(name = "cargo foo", about = "fooing every day")]
struct Options {
    _void: String,
    /// First arg
    #[structopt(help = "First arg")]
    first: String,
}

and this gets shown as:

USAGE:
    cargo foo <_void> <first>

For more information try --help

It would be cool if structopt could be used to write cargo subcommands easily.

question

All 2 comments

Basically, you what a mandatory subcommand, thus

#[derive(StructOpt, Debug)]
#[structopt(bin_name = "cargo")]
enum Opt {
    #[structopt(name = "foo")]
    Foo {
        /// First arg
        first: String,
        // ...
    }
}

Does it correspond to your use case?

@TeXitoi yes, this is what I wanted, thanks! I had an ergonomic problem while moving to this pattern, I've filled #59 for that.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tathanhdinh picture tathanhdinh  路  6Comments

coder543 picture coder543  路  9Comments

wez picture wez  路  7Comments

sphynx picture sphynx  路  9Comments

jackjen picture jackjen  路  7Comments