Repository: clap-rs/clap (language: rust)

Issue:
`min_values` when using Parser not respected
### Please complete the following tasks

- [X] I have searched the [discussions](https://github.com/clap-rs/clap/discussions)
- [X] I have searched the existing issues

### Rust Version

rustc 1.57.0 (f1edd0429 2021-11-29)

### Clap Version

3.0.4

### Minimal reproducible code

```toml
[dependencies.clap]
version = "3.0.1"
features = ["derive"]
```

```rust
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: [],
}
```


Discussion / hints:
`max_values` seems to be okay.
```rs
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
````
Turns out to make it work _correctly_ is having a `required = true` as well. So `min_value` needs to inherently be required as well.

```rs
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
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.
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 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?

Fix patch:
diff --git a/src/builder/arg.rs b/src/builder/arg.rs
index 91fe5b4a9be..07ece27ac57 100644
--- a/src/builder/arg.rs
+++ b/src/builder/arg.rs
@@ -1295,6 +1295,11 @@ impl<'help> Arg<'help> {
     /// [`Arg::multiple_occurrences(true)`] because there is no way to determine the difference between multiple
     /// occurrences and multiple values.
     ///
+    /// **NOTE:** Passing a non-zero value is not the same as specifying [`Arg::required(true)`].
+    /// This is due to min and max validation only being performed for present arguments,
+    /// marking them as required will thus perform validation and a min value of 1
+    /// is unnecessary, ignored if not required.
+    ///
     /// # Examples
     ///
     /// ```rust
@@ -1340,6 +1345,7 @@ impl<'help> Arg<'help> {
     /// assert_eq!(res.unwrap_err().kind(), ErrorKind::TooFewValues);
     /// ```
     /// [`Arg::multiple_occurrences(true)`]: Arg::multiple_occurrences()
+    /// [`Arg::required(true)`]: Arg::required()
     #[inline]
     #[must_use]
     pub fn min_values(mut self, qty: usize) -> Self {


Test patch: