Structopt: Subcommand with multiple "names"

Created on 9 Apr 2018  Â·  2Comments  Â·  Source: TeXitoi/structopt

Hello!

I would like to know if it is possible to add alternatives to invoke the same Subcommand.

Take this for example:

#[derive(StructOpt, Debug)]
pub enum Subcommand {
    #[structopt(name = "action")]
    Action {
        #[structopt(name = "something")]
        something: String,
    }
}

In order to use the subcommand, the user should type ./myapp action. I would like to "shorten" it to ./myapp a or even ./myapp act.

Is it possible to achieve something like this?

Thanks and keep up the awesome work!

question

Most helpful comment

An example with all such functionnalities with link to clap documentation: https://github.com/TeXitoi/structopt/blob/master/examples/subcommand_aliases.rs

The resulting binary:

gpinot@gpinot:~/dev/structopt$ ./target/debug/examples/subcommand_aliases f
error: The subcommand 'f' wasn't recognized

USAGE:
    subcommand_aliases help <subcommands>...

For more information try --help
gpinot@gpinot:~/dev/structopt$ ./target/debug/examples/subcommand_aliases fo
Foo
gpinot@gpinot:~/dev/structopt$ ./target/debug/examples/subcommand_aliases foo
Foo
gpinot@gpinot:~/dev/structopt$ ./target/debug/examples/subcommand_aliases foobar
Foo
gpinot@gpinot:~/dev/structopt$ ./target/debug/examples/subcommand_aliases fi
Bar
gpinot@gpinot:~/dev/structopt$ ./target/debug/examples/subcommand_aliases b
Bar
gpinot@gpinot:~/dev/structopt$ ./target/debug/examples/subcommand_aliases bar
Bar

Does it answer all your questions?

All 2 comments

An example with all such functionnalities with link to clap documentation: https://github.com/TeXitoi/structopt/blob/master/examples/subcommand_aliases.rs

The resulting binary:

gpinot@gpinot:~/dev/structopt$ ./target/debug/examples/subcommand_aliases f
error: The subcommand 'f' wasn't recognized

USAGE:
    subcommand_aliases help <subcommands>...

For more information try --help
gpinot@gpinot:~/dev/structopt$ ./target/debug/examples/subcommand_aliases fo
Foo
gpinot@gpinot:~/dev/structopt$ ./target/debug/examples/subcommand_aliases foo
Foo
gpinot@gpinot:~/dev/structopt$ ./target/debug/examples/subcommand_aliases foobar
Foo
gpinot@gpinot:~/dev/structopt$ ./target/debug/examples/subcommand_aliases fi
Bar
gpinot@gpinot:~/dev/structopt$ ./target/debug/examples/subcommand_aliases b
Bar
gpinot@gpinot:~/dev/structopt$ ./target/debug/examples/subcommand_aliases bar
Bar

Does it answer all your questions?

Absolutely! Thanks!

On Mon, Apr 9, 2018 at 2:16 AM, Guillaume P. notifications@github.com
wrote:

An example with all such functionnalities with link to clap documentation:
https://github.com/TeXitoi/structopt/blob/master/
examples/subcommand_aliases.rs

The resulting binary:

gpinot@gpinot:~/dev/structopt$ ./target/debug/examples/subcommand_aliases f
error: The subcommand 'f' wasn't recognized

USAGE:
subcommand_aliases help ...

For more information try --help
gpinot@gpinot:~/dev/structopt$ ./target/debug/examples/subcommand_aliases fo
Foo
gpinot@gpinot:~/dev/structopt$ ./target/debug/examples/subcommand_aliases foo
Foo
gpinot@gpinot:~/dev/structopt$ ./target/debug/examples/subcommand_aliases foobar
Foo
gpinot@gpinot:~/dev/structopt$ ./target/debug/examples/subcommand_aliases fi
Bar
gpinot@gpinot:~/dev/structopt$ ./target/debug/examples/subcommand_aliases b
Bar
gpinot@gpinot:~/dev/structopt$ ./target/debug/examples/subcommand_aliases bar
Bar

Does it answer all your questions?

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/TeXitoi/structopt/issues/91#issuecomment-379688173,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AFwP62FWxn0OsyxmKbfNvYJamc7AfuUwks5tmycDgaJpZM4TL6AA
.

--
- Giulio

Was this page helpful?
0 / 5 - 0 ratings

Related issues

epage picture epage  Â·  7Comments

wez picture wez  Â·  7Comments

coder543 picture coder543  Â·  9Comments

swfsql picture swfsql  Â·  3Comments

gnzlbg picture gnzlbg  Â·  5Comments