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

Using number_of_values and default_value together #1050

Closed
Frederick888 opened this issue Sep 19, 2017 · 2 comments
Closed

Using number_of_values and default_value together #1050

Frederick888 opened this issue Sep 19, 2017 · 2 comments
Labels
A-parsing Area: Parser's logic and needs it changed somehow. C-bug Category: Updating dependencies E-medium Call for participation: Experience needed to fix: Medium / intermediate
Milestone

Comments

@Frederick888
Copy link

Frederick888 commented Sep 19, 2017

Found this issue with clap 2.26.2

It seems that currently the default value is implemented by appending it to the existing matches. But it causes issues if a combination of number_of_values and default_value is used.

For instance,

extern crate clap;

use clap::{App, Arg};

fn main() {
    let m = App::new("hello_world")
        .arg(
            Arg::with_name("exit-code")
                .long("exit-code")
                .required(true)
                .takes_value(true)
                .number_of_values(1)
                .default_value("0"),
        )
        .get_matches();
    println!(
        "{:?}",
        m.values_of("exit-code").unwrap().collect::<Vec<_>>()
    );
}
⇒  ./hello_world --exit-code
["0"]
⇒  ./hello_world --exit-code 1
error: The argument '--exit-code <exit-code>' requires 1 values, but 2 were provided

USAGE:
    hello_world --exit-code <exit-code>

For more information try --help

Changing number_of_values(1) to number_of_values(2) would make it work if the argument is correctly given but the error message is still off when it's not provided:

⇒  ./hello_world --exit-code  
error: The argument '--exit-code <exit-code> <exit-code>' requires 2 values, but 1 was provided

USAGE:
    hello_world --exit-code <exit-code> <exit-code>

For more information try --help
@Frederick888
Copy link
Author

Frederick888 commented Sep 20, 2017

diff --git a/src/app/parser.rs b/src/app/parser.rs
index 6ec72034..35eb3f9b 100644
--- a/src/app/parser.rs
+++ b/src/app/parser.rs
@@ -1733,15 +1733,8 @@ impl<'a, 'b> Parser<'a, 'b>
                 if let Some(ref val) = $a.v.default_val {
                     if $m.get($a.b.name).map(|ma| ma.vals.len()).map(|len| len == 0).unwrap_or(false) {
                         $_self.add_val_to_arg($a, OsStr::new(val), $m)?;
 
-                        if $_self.cache.map_or(true, |name| name != $a.name()) {
-                            arg_post_processing!($_self, $a, $m);
-                            $_self.cache = Some($a.name());
-                        }
-                    } else {
-                        $_self.add_val_to_arg($a, OsStr::new(val), $m)?;
-
                         if $_self.cache.map_or(true, |name| name != $a.name()) {
                             arg_post_processing!($_self, $a, $m);
                             $_self.cache = Some($a.name());
                         }

...fixes it for me. But I'm not really clear about the purpose of the codes here.

@kbknapp
Copy link
Member

kbknapp commented Oct 4, 2017

Yep, I just saw this when going through #1056

I'm not sure why it's doing the same thing in both branches 😕

@kbknapp kbknapp added A-parsing Area: Parser's logic and needs it changed somehow. D: easy E-medium Call for participation: Experience needed to fix: Medium / intermediate C-bug Category: Updating dependencies labels Oct 4, 2017
@kbknapp kbknapp added this to the 2.27.0 milestone Oct 12, 2017
@kbknapp kbknapp mentioned this issue Nov 13, 2017
87 tasks
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: Updating dependencies E-medium Call for participation: Experience needed to fix: Medium / intermediate
Projects
None yet
Development

No branches or pull requests

2 participants