diff --git a/cli/tests/complete_word.rs b/cli/tests/complete_word.rs index be57e6c..230e3b6 100644 --- a/cli/tests/complete_word.rs +++ b/cli/tests/complete_word.rs @@ -70,6 +70,11 @@ fn complete_word_shebang() { assert_cmd("example.sh", &["--", "-"]).stdout("--bar\n--defaulted\n--foo\n"); } +#[test] +fn complete_word_arg_completer() { + assert_cmd("example.sh", &["--", "v"]).stdout("val-1\nval-2\nval-3\n"); +} + #[test] fn complete_word_mounted() { let mut path = env::split_paths(&env::var("PATH").unwrap()).collect::>(); diff --git a/examples/example.sh b/examples/example.sh index b518c17..e0e82bc 100755 --- a/examples/example.sh +++ b/examples/example.sh @@ -4,7 +4,8 @@ #USAGE flag "--foo" help="Flag value" #USAGE flag "--bar " help="Option value" #USAGE flag "--defaulted " default="mydefault" help="Defaulted value" -#USAGE arg "baz" help="Positional values" +#USAGE arg "baz" help="Positional values" default="mydefault" +#USAGE complete "baz" run="echo val-1; echo val-2; echo val-3" #USAGE min_usage_version "1" set -eo pipefail diff --git a/lib/src/parse.rs b/lib/src/parse.rs index b66ae85..15829f4 100644 --- a/lib/src/parse.rs +++ b/lib/src/parse.rs @@ -31,8 +31,27 @@ pub enum ParseValue { } pub fn parse(spec: &Spec, input: &[String]) -> Result { - let out = parse_partial(spec, input)?; + let mut out = parse_partial(spec, input)?; trace!("{out:?}"); + for arg in out.cmd.args.iter().skip(out.args.len()) { + if let Some(default) = arg.default.as_ref() { + out.args + .insert(arg.clone(), ParseValue::String(default.clone())); + } + } + for flag in out.available_flags.values() { + if out.flags.contains_key(flag) { + continue; + } + if let Some(default) = flag.default.as_ref() { + out.flags + .insert(flag.clone(), ParseValue::String(default.clone())); + } + if let Some(Some(default)) = flag.arg.as_ref().map(|a| &a.default) { + out.flags + .insert(flag.clone(), ParseValue::String(default.clone())); + } + } if let Some(err) = out.errors.iter().find(|e| matches!(e, UsageErr::Help(_))) { bail!("{err}"); } @@ -235,10 +254,7 @@ pub fn parse_partial(spec: &Spec, input: &[String]) -> Result Result