Skip to content

TrailingVarArg with hyphens #558

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

Closed
marcbowes opened this issue Jul 1, 2016 · 5 comments
Closed

TrailingVarArg with hyphens #558

marcbowes opened this issue Jul 1, 2016 · 5 comments
Labels
A-parsing Area: Parser's logic and needs it changed somehow. C-bug Category: bug
Milestone

Comments

@marcbowes
Copy link

marcbowes commented Jul 1, 2016

#278 shows how to use TrailingVarArg and a multiple arg to capture extra args. This works great, but I can't seem to get it working with args that start with hyphens:

[dependencies]
clap = "2.2.4"
extern crate clap;

#[cfg(test)]
mod tests {
    use clap::*;

    #[test]
    fn test_vararg_1() {
        let m = App::new("myprog")
            .setting(AppSettings::TrailingVarArg)
            .subcommand(SubCommand::with_name("sub")
                .setting(AppSettings::TrailingVarArg)
                .arg(Arg::from_usage("<cmd>... 'commands to run'")))
            .get_matches_from(vec!["myprog", "sub", "arg1", "-r", "val1"]);

        let trail: Vec<&str> =
            m.subcommand_matches("sub").unwrap().values_of("cmd").unwrap().collect();
        assert_eq!(trail, ["arg1", "-r", "val1"]);
    }

    #[test]
    fn test_vararg_2() {
        let m = App::new("myprog")
            .setting(AppSettings::TrailingVarArg)
            .subcommand(SubCommand::with_name("sub")
                .setting(AppSettings::TrailingVarArg)
                .arg(Arg::from_usage("<cmd>... 'commands to run'")))
            .get_matches_from(vec!["myprog", "sub", "--arg1", "-r", "val1"]);
       // ^ ERROR due to --arg1
        let trail: Vec<&str> =
            m.subcommand_matches("sub").unwrap().values_of("cmd").unwrap().collect();
        assert_eq!(trail, ["--arg1", "-r", "val1"]);
    }
}
cargo test -- test_vararg_1
     Running target/debug/vararg-e311bd86d802e1d9

running 1 test
test tests::test_vararg_1 ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
cargo test -- test_vararg_2 
     Running target/debug/vararg-e311bd86d802e1d9

running 1 test
error: Found argument '--arg1' which wasn't expected, or isn't valid in this context

USAGE:
    myprog sub <cmd>...

For more information try --help
@clux
Copy link

clux commented Jul 1, 2016

TrailingVarArg must be set inside the subcommand, or you have to make TrailingVarArg a global setting.

@marcbowes
Copy link
Author

Sorry, I messed up the description. Updated.

@marcbowes marcbowes changed the title SubCommand using TrailingVarArg TrailingVarArg with hyphens Jul 1, 2016
@kbknapp
Copy link
Member

kbknapp commented Jul 1, 2016

This appears to be a bug, thanks for filing! Like @clux said, the setting should be on the subcommand it affects. You can also use global_setting to set it on the parent command and propagate it down through all the child subcommands. Just an option 😉

I'm looking into this bug now as to why it only works when the first argument doesn't start with a hyphen. I'll post back here with what I find.

@kbknapp kbknapp added C-bug Category: bug P2: need to have A-parsing Area: Parser's logic and needs it changed somehow. labels Jul 1, 2016
@kbknapp kbknapp added this to the 2.9.0 milestone Jul 1, 2016
@kbknapp
Copy link
Member

kbknapp commented Jul 1, 2016

#560 fixes this.

Also, part of the problem is you need to add AppSettings::AllowLeadingHyphen, because it's also possible to use proper long options and flags with commands that accept TrailingVarArgs, so this distinguishes them.

This should be a passing test once the PR merges:

#[test]
    fn test_vararg_2() {
        let m = App::new("myprog")
            .subcommand(SubCommand::with_name("sub")
                .setting(AppSettings::TrailingVarArg)
                .setting(AppSettings::AllowLeadingHyphen)   // <-- Only change
                .arg(Arg::from_usage("<cmd>... 'commands to run'")))
            .get_matches_from(vec!["myprog", "sub", "--arg1", "-r", "val1"]);
        let trail: Vec<&str> =
            m.subcommand_matches("sub").unwrap().values_of("cmd").unwrap().collect();
        assert_eq!(trail, ["--arg1", "-r", "val1"]);
    }

@marcbowes
Copy link
Author

That was fast! Thanks!

Looking forward to the completion work you've done in #560.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-parsing Area: Parser's logic and needs it changed somehow. C-bug Category: bug
Projects
None yet
Development

No branches or pull requests

3 participants