Repository: clap-rs/clap (Rust)

Issue:
clap_derive ignores the value_delimiter
### 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.54.0 (a178d0322 2021-07-26)

### Clap Version

3.0.0-beta.4

### Minimal reproducible code

```rust
#[derive(Clap)]
pub struct Config {
   #[clap(
        long,
        default_value = "first;second",
        value_delimiter = ';'
    )]
    pub values: Vec<String>,

}

#[test]
fn should_split_values() {
    let args: Vec<std::ffi::OsString> = vec![];
    let config = Config::parse_from(args);
    assert_eq!(vec!["first".to_owned(), "second".to_owned()], config.values);
}
```


### Steps to reproduce the bug with the above code

Execute the `should_split_values()` test

### Actual Behaviour

The test fails because the `default_value` was not split using the provided `value_delimiter`. 
This is the test error:
```
'should_split_values' panicked at 'assertion failed: `(left == right)`
  left: `["first", "second"]`,
 right: `["first;second"]`',
```


### Expected Behaviour

The test should pass and the `Config.values` field should contain the two values `"first"` and ` "second"`

### Additional Context

This code works as expected with structopt 0.3



Discussion:
Looks like this is specifically with handling of defaults, regardless of which API is used
```rust
use clap::{App, Arg, Clap};

#[derive(Clap)]
pub struct Config {
    #[clap(long, default_value = "first;second", value_delimiter = ';')]
    pub values: Vec<String>,
}

#[test]
fn should_split_default() {
    let args: Vec<std::ffi::OsString> = vec!["".into()];
    let config = Config::parse_from(args);
    assert_eq!(vec!["first".to_owned(), "second".to_owned()], config.values);
}

#[test]
fn should_split_args() {
    let args: Vec<std::ffi::OsString> = vec!["".into(), "--values=two;three".into()];
    let config = Config::parse_from(args);
    assert_eq!(vec!["two".to_owned(), "three".to_owned()], config.values);
}

#[test]
fn builder_should_split_default() {
    let m = App::new("multiple_values")
        .arg(
            Arg::new("option")
                .long("option")
                .about("multiple options")
                .value_delimiter(';')
                .default_value("first;second"),
        )
        .try_get_matches_from(vec![""]);

    assert!(m.is_ok());
    let m = m.unwrap();

    assert_eq!(
        m.values_of("option").unwrap().collect::<Vec<_>>(),
        ["first", "second"]
    );
}

#[test]
fn builder_should_split_args() {
    let m = App::new("multiple_values")
        .arg(
            Arg::new("option")
                .long("option")
                .about("multiple options")
                .value_delimiter(';')
                .default_value("first;second"),
        )
        .try_get_matches_from(vec!["", "--option=two;three"]);

    assert!(m.is_ok());
    let m = m.unwrap();

    assert_eq!(
        m.values_of("option").unwrap().collect::<Vec<_>>(),
        ["two", "three"]
    );
}
```
```
running 4 tests
test builder_should_split_args ... ok
test should_split_args ... ok
test should_split_default ... FAILED
test builder_should_split_default ... FAILED

failures:

---- should_split_default stdout ----
thread 'should_split_default' panicked at 'assertion failed: `(left == right)`
  left: `["first", "second"]`,
 right: `["first;second"]`', tests/repo.rs:13:5

---- builder_should_split_default stdout ----
thread 'builder_should_split_default' panicked at 'assertion failed: `(left == right)`
  left: `["first;second"]`,
 right: `["first", "second"]`', tests/repo.rs:38:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
```



Patch:
diff --git a/src/parse/parser.rs b/src/parse/parser.rs
index 5135fbadfda..572045bf4c8 100644
--- a/src/parse/parser.rs
+++ b/src/parse/parser.rs
@@ -1685,6 +1685,21 @@ impl<'help, 'app> Parser<'help, 'app> {
             debug!("Parser::add_value: doesn't have conditional defaults");
         }
 
+        fn process_default_vals(arg: &Arg<'_>, default_vals: &[&OsStr]) -> Vec<OsString> {
+            if let Some(delim) = arg.val_delim {
+                let mut vals = vec![];
+                for val in default_vals {
+                    let val = RawOsStr::new(val);
+                    for val in val.split(delim) {
+                        vals.push(val.to_os_str().into_owned());
+                    }
+                }
+                vals
+            } else {
+                default_vals.iter().map(OsString::from).collect()
+            }
+        }
+
         if !arg.default_vals.is_empty() {
             debug!("Parser::add_value:iter:{}: has default vals", arg.name);
             if matcher.get(&arg.id).is_some() {
@@ -1695,7 +1710,7 @@ impl<'help, 'app> Parser<'help, 'app> {
 
                 self.add_multiple_vals_to_arg(
                     arg,
-                    arg.default_vals.iter().map(OsString::from),
+                    process_default_vals(arg, &arg.default_vals).into_iter(),
                     matcher,
                     ty,
                     false,
@@ -1723,7 +1738,7 @@ impl<'help, 'app> Parser<'help, 'app> {
                     );
                     self.add_multiple_vals_to_arg(
                         arg,
-                        arg.default_missing_vals.iter().map(OsString::from),
+                        process_default_vals(arg, &arg.default_missing_vals).into_iter(),
                         matcher,
                         ty,
                         false,


Test patch:
diff --git a/tests/default_vals.rs b/tests/default_vals.rs
index aaa9c03fe57..d9fb6b46ce0 100644
--- a/tests/default_vals.rs
+++ b/tests/default_vals.rs
@@ -651,3 +651,38 @@ fn required_args_with_default_values() {
         .arg(Arg::new("arg").required(true).default_value("value"))
         .try_get_matches();
 }
+
+#[test]
+fn with_value_delimiter() {
+    let app = App::new("multiple_values").arg(
+        Arg::new("option")
+            .long("option")
+            .about("multiple options")
+            .value_delimiter(';')
+            .default_value("first;second"),
+    );
+
+    let matches = app.get_matches_from(vec![""]);
+
+    assert_eq!(
+        matches.values_of("option").unwrap().collect::<Vec<_>>(),
+        ["first", "second"]
+    );
+}
+
+#[test]
+fn missing_with_value_delimiter() {
+    let app = App::new("program").arg(
+        Arg::new("option")
+            .long("option")
+            .value_delimiter(';')
+            .default_missing_values(&["value1;value2;value3", "value4;value5"]),
+    );
+
+    let matches = app.get_matches_from(vec!["program", "--option"]);
+
+    assert_eq!(
+        matches.values_of("option").unwrap().collect::<Vec<_>>(),
+        ["value1", "value2", "value3", "value4", "value5"]
+    );
+}