Skip to content

Commit

Permalink
fix: bug with completing default args/flags (#185)
Browse files Browse the repository at this point in the history
* fix: bug with completing default args/flags

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
jdx and autofix-ci[bot] authored Dec 9, 2024
1 parent 650f5fb commit d29ec77
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
5 changes: 5 additions & 0 deletions cli/tests/complete_word.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>();
Expand Down
3 changes: 2 additions & 1 deletion examples/example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
#USAGE flag "--foo" help="Flag value"
#USAGE flag "--bar <bar>" help="Option value"
#USAGE flag "--defaulted <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

Expand Down
37 changes: 23 additions & 14 deletions lib/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,27 @@ pub enum ParseValue {
}

pub fn parse(spec: &Spec, input: &[String]) -> Result<ParseOutput, miette::Error> {
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}");
}
Expand Down Expand Up @@ -235,10 +254,7 @@ pub fn parse_partial(spec: &Spec, input: &[String]) -> Result<ParseOutput, miett
}

for arg in out.cmd.args.iter().skip(out.args.len()) {
if let Some(default) = &arg.default {
out.args
.insert(arg.clone(), ParseValue::String(default.clone()));
} else if arg.required {
if arg.required && arg.default.is_none() {
out.errors.push(UsageErr::MissingArg(arg.name.clone()));
}
}
Expand All @@ -247,15 +263,8 @@ pub fn parse_partial(spec: &Spec, input: &[String]) -> Result<ParseOutput, miett
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 flag.required {
let has_default = flag.default.is_some() || flag.arg.iter().any(|a| a.default.is_some());
if flag.required && !has_default {
out.errors.push(UsageErr::MissingFlag(flag.name.clone()));
}
}
Expand Down

0 comments on commit d29ec77

Please sign in to comment.