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

min_values when using Parser not respected #3259

Closed
2 tasks done
jarrodsfarrell opened this issue Jan 5, 2022 · 6 comments · Fixed by #3728
Closed
2 tasks done

min_values when using Parser not respected #3259

jarrodsfarrell opened this issue Jan 5, 2022 · 6 comments · Fixed by #3728
Labels
A-docs Area: documentation, including docs.rs, readme, examples, etc... C-bug Category: Updating dependencies E-easy Call for participation: Experience needed to fix: Easy / not much

Comments

@jarrodsfarrell
Copy link

jarrodsfarrell commented Jan 5, 2022

Please complete the following tasks

  • I have searched the discussions
  • I have searched the existing issues

Rust Version

rustc 1.57.0 (f1edd0429 2021-11-29)

Clap Version

3.0.4

Minimal reproducible code

[dependencies.clap]
version = "3.0.1"
features = ["derive"]
use clap::{App, Parser};

#[derive(Parser, Debug)]
struct Args {
    #[clap(short, min_values = 1)]
    test: Vec<String>,
}
fn main() {
    let args = Args::parse();
    println!("{:#?}", args);
}

Steps to reproduce the bug with the above code

No arguments

Actual Behaviour

App executes, no error.

Expected Behaviour

An error should occur since there are not enough values for --test

Additional Context

No response

Debug Output

    Finished dev [unoptimized + debuginfo] target(s) in 0.02s
     Running `target/debug/clap-test`
[            clap::build::app]  App::_do_parse
[            clap::build::app]  App::_build
[            clap::build::app]  App::_propagate:clap-test
[            clap::build::app]  App::_check_help_and_version
[            clap::build::app]  App::_check_help_and_version: Removing generated version
[            clap::build::app]  App::_propagate_global_args:clap-test
[            clap::build::app]  App::_derive_display_order:clap-test
[clap::build::app::debug_asserts]       App::_debug_asserts
[clap::build::arg::debug_asserts]       Arg::_debug_asserts:help
[clap::build::arg::debug_asserts]       Arg::_debug_asserts:test
[         clap::parse::parser]  Parser::get_matches_with
[         clap::parse::parser]  Parser::_build
[         clap::parse::parser]  Parser::_verify_positionals
[      clap::parse::validator]  Validator::validate
[         clap::parse::parser]  Parser::add_defaults
[         clap::parse::parser]  Parser::add_defaults:iter:test:
[         clap::parse::parser]  Parser::add_value: doesn't have conditional defaults
[         clap::parse::parser]  Parser::add_value:iter:test: doesn't have default vals
[         clap::parse::parser]  Parser::add_value:iter:test: doesn't have default missing vals
[      clap::parse::validator]  Validator::validate_conflicts
[      clap::parse::validator]  Validator::validate_exclusive
[      clap::parse::validator]  Validator::validate_required: required=ChildGraph([])
[      clap::parse::validator]  Validator::gather_requirements
[      clap::parse::validator]  Validator::validate_required_unless
[      clap::parse::validator]  Validator::validate_matched_args
[    clap::parse::arg_matcher]  ArgMatcher::get_global_values: global_arg_vec=[help]
Args {
    test: [],
}
@jarrodsfarrell jarrodsfarrell added the C-bug Category: Updating dependencies label Jan 5, 2022
@jarrodsfarrell
Copy link
Author

max_values seems to be okay.

use clap::Parser;

#[derive(Parser, Debug)]
struct Args {
    #[clap(short, max_values = 2)]
    test: Vec<String>,
}
fn main() {
    let args = Args::parse();
    println!("{:#?}", args);
}

cargo run -- -t too -t many -t bees

[...]
error: The value 'bees' was provided to '-t <TEST>...' but it wasn't expecting any more values

@jarrodsfarrell
Copy link
Author

jarrodsfarrell commented Jan 5, 2022

Turns out to make it work correctly is having a required = true as well. So min_value needs to inherently be required as well.

use clap::Parser;

#[derive(Parser, Debug)]
struct Args {
    #[clap(short, min_values = 2, required = true)]
    test: Vec<String>,
}
fn main() {
    let args = Args::parse();
    println!("{:#?}", args);
}

.. -t need Errors correctly
.. -t need -t bees Program runs

@jarrodsfarrell
Copy link
Author

jarrodsfarrell commented Jan 5, 2022

Actually this might be of design; I can figure a reason why someone would need to not require an arg's Vec<T> to contain values when not given but when it is given then it must have X values of some kind.

@epage
Copy link
Member

epage commented Jan 5, 2022

Currently, we only do min/max value checks against present arguments.

I think keeping it that way makes sense but a caveat should be added to the documentation.

min_values does imply multiple_values and they are assumed to be used together.

@epage epage added A-docs Area: documentation, including docs.rs, readme, examples, etc... E-easy Call for participation: Experience needed to fix: Easy / not much labels Jan 5, 2022
malthesr added a commit to malthesr/winsfs that referenced this issue Mar 1, 2022
`min_values` is not respected without `required = True`, see also
github.com/clap-rs/clap/issues/3259
@AndreasBackx
Copy link
Contributor

AndreasBackx commented May 14, 2022

@epage What about making min_values(1) implicitly specify required(true) and min_values(0) implicitly specify required(false) with a note in the documentation so these human errors are prevented without hopefully a trip to the documentation?

@epage
Copy link
Member

epage commented May 14, 2022

Any changes along those lines would be a breaking change which won't happen until 4.0 at least but I am hoping to change a lot about how validation works in 4.0 between #2688 and #3476. I expect the only value count function that will exist will be for the number of values per occurrence and everything else will be handled through plugins, whether included with clap or written by end users. My hope is we'll be at a point where those plugins can opt arguments into being required or not, so that type of implication can be done.

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... C-bug Category: Updating dependencies E-easy Call for participation: Experience needed to fix: Easy / not much
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants