Repository: pacak/bpaf
Pull request #34 (2022-08-23 13:38:26)

and a bit more cosmetics
preserve suggestion context when returning from a subcommand


Patch:
diff --git a/Cargo.toml b/Cargo.toml
index 1d6833f..66c0fd6 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
 [package]
 name = "bpaf"
-version = "0.5.2"
+version = "0.5.3"
 edition = "2021"
 categories = ["command-line-interface"]
 description = "A simple Command Line Argument Parser with parser combinators"
diff --git a/Changelog.md b/Changelog.md
index bc41404..989d5e5 100644
--- a/Changelog.md
+++ b/Changelog.md
@@ -1,5 +1,8 @@
 # Change Log
 
+## bpaf [0.5.3] - 2022-08-23
+and a bit more cosmetics - preserve suggestion context when returning from a subcommand
+
 ## bpaf [0.5.2] - 2022-08-23
 - fix a regression in error messaged caused by 0.5.1
 - guard now displays the problematic input if it's a single argument issue
diff --git a/src/info.rs b/src/info.rs
index 6edcef3..f93cb3b 100644
--- a/src/info.rs
+++ b/src/info.rs
@@ -205,15 +205,12 @@ impl<T> OptionParser<T> {
     /// Run subparser, implementation detail
     #[doc(hidden)]
     pub fn run_subparser(&self, args: &mut Args) -> Result<T, Error> {
+        let depth = args.depth;
         let err = match self.inner.eval(args) {
             Ok(r) => {
                 if let Err(err) = check_unexpected(args) {
                     err
                 } else {
-                    // <-
-                    // complain about any unused items - beats "not expected in this context"
-                    crate::meta_youmean::suggest(args, &self.inner.meta())?;
-                    // <-
                     return Ok(r);
                 }
             }
@@ -243,11 +240,10 @@ impl<T> OptionParser<T> {
             }
             Err(_) => {}
         }
-        // <-
-        if crate::meta_youmean::should_suggest(&err) {
+
+        if crate::meta_youmean::should_suggest(&err) && args.depth == depth {
             crate::meta_youmean::suggest(args, &self.inner.meta())?;
         }
-        // <-
 
         Err(err)
     }


Test patch:
diff --git a/src/tests.rs b/src/tests.rs
index 8c1b4b7..98988d1 100644
--- a/src/tests.rs
+++ b/src/tests.rs
@@ -1582,3 +1582,46 @@ fn cargo_show_asm_issue_unknown_switch() {
         .unwrap_stderr();
     assert_eq!(res, "No such flag: `--fla`, did you mean `--flag`?");
 }
+
+#[test]
+fn did_you_mean_inside_command() {
+    let a = long("flag").switch();
+    let b = long("parameter").switch();
+    let parser = construct!([a, b]).to_options().command("cmd").to_options();
+
+    let r = parser
+        .run_inner(Args::from(&["--fla"]))
+        .unwrap_err()
+        .unwrap_stderr();
+    assert_eq!(r, "No such flag: `--fla`, did you mean `cmd`?");
+
+    let r = parser
+        .run_inner(Args::from(&["cmd", "--fla"]))
+        .unwrap_err()
+        .unwrap_stderr();
+    assert_eq!(r, "No such flag: `--fla`, did you mean `--flag`?");
+
+    let r = parser
+        .run_inner(Args::from(&["cmd", "--flag", "--parametr"]))
+        .unwrap_err()
+        .unwrap_stderr();
+    assert_eq!(r, "No such flag: `--parametr`, did you mean `--parameter`?");
+
+    let r = parser
+        .run_inner(Args::from(&["cmd", "--parametr", "--flag"]))
+        .unwrap_err()
+        .unwrap_stderr();
+    assert_eq!(r, "No such flag: `--parametr`, did you mean `--parameter`?");
+
+    let r = parser
+        .run_inner(Args::from(&["cmd", "--parameter", "--flag"]))
+        .unwrap_err()
+        .unwrap_stderr();
+    assert_eq!(r, "--flag is not expected in this context");
+
+    let r = parser
+        .run_inner(Args::from(&["cmd", "--flag", "--parameter"]))
+        .unwrap_err()
+        .unwrap_stderr();
+    assert_eq!(r, "--parameter is not expected in this context");
+}