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

Accessing subsubcommand matches #766

Closed
kirillbobyrev opened this issue Dec 5, 2016 · 3 comments
Closed

Accessing subsubcommand matches #766

kirillbobyrev opened this issue Dec 5, 2016 · 3 comments
Labels
A-docs Area: documentation, including docs.rs, readme, examples, etc... E-medium Call for participation: Experience needed to fix: Medium / intermediate

Comments

@kirillbobyrev
Copy link

kirillbobyrev commented Dec 5, 2016

From what I have seen it is not clear how a sub-subcommand matches should be accessed.

What I expect to see is something like

let matches = App::from_yaml(yml).get_matches();
let subcommand_matches = matches.subcommand_matches(matches.subcommand_name().unwrap());
let subsubcommand_matches = subcommand_matches.subcommand_matches(matches.subcommand_name().unwrap());

However, I get

error: no method named `subcommand_matches` found for type `std::option::Option<&clap::ArgMatches<'_>>` in the current scope

With

rustc 1.13.0
clap-rs 2.19.1

Am I just missing something or is such kind of subsubcommand extraction not supported?

@kbknapp
Copy link
Member

kbknapp commented Dec 5, 2016

It's not the typical way to do it, but you can change your code to work below:

let matches = App::from_yaml(yml).get_matches();

// subcommand_matches returns an option
let subcommand_matches = matches.subcommand_matches(matches.subcommand_name().unwrap()).unwrap();

let subsubcommand_matches = subcommand_matches.subcommand_matches(matches.subcommand_name().unwrap()).unwrap();

The more common way to do this is:

match matches.subcommand() {
    ("foo", Some(foo_matches)) => {
        // Handle the myprog foo subcommand
    },
    ("bar", Some(bar_matches)) => {
        // Handle myprog bar subcommand
        match bar_matches.subcommand() {
            ("baz", Some(baz_matches)) => {
                // handle myprog bar baz subcommand
             },
             _ => unreachabe!()
        }
    },
    _ => unreachabe!()
}

etc.

@kirillbobyrev
Copy link
Author

Great, thank you once again and apologies for a newbie question.

Do you think it's worth adding such example to examples/? Even though there are great examples, I would actually love to see even more as a newcomer.

@kbknapp
Copy link
Member

kbknapp commented Dec 5, 2016

For sure! We can always use more examples. I'll add it on my next doc sweep unless someone else beats me to it.

@kbknapp kbknapp added A-docs Area: documentation, including docs.rs, readme, examples, etc... D: easy E-medium Call for participation: Experience needed to fix: Medium / intermediate labels Dec 5, 2016
kbknapp added a commit that referenced this issue Dec 7, 2016
kbknapp added a commit that referenced this issue Dec 8, 2016
kbknapp added a commit that referenced this issue Dec 8, 2016
homu added a commit that referenced this issue Dec 8, 2016
docs(Examples): adds subcommand examples

Closes #766

cc @omtcyfz
homu added a commit that referenced this issue Dec 8, 2016
docs(Examples): adds subcommand examples

Closes #766

cc @omtcyfz
@homu homu closed this as completed in #768 Dec 8, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: documentation, including docs.rs, readme, examples, etc... E-medium Call for participation: Experience needed to fix: Medium / intermediate
Projects
None yet
Development

No branches or pull requests

2 participants