Repository: jacobdeichert/mask (language: rust)

Issue:
Subcommand not recognized if followed by a # HEADER line
For example, `mask foo --help` on 

~~~
# Header 1

## foo

### foo A

```sh
echo A
```

### foo B

```sh
echo B
```

# foo B is not found as a subcommand if this line is present
~~~


prints 

~~~
mask-foo 


USAGE:
    mask foo <SUBCOMMAND>

FLAGS:
    -h, --help    Prints help information

SUBCOMMANDS:
    A       
    help    Prints this message or the help of the given subcommand(s)
~~~



Discussion / hints:

Fix patch:
diff --git a/src/parser.rs b/src/parser.rs
index ba53ead..76ca13c 100644
--- a/src/parser.rs
+++ b/src/parser.rs
@@ -21,9 +21,12 @@ pub fn build_command_structure(maskfile_contents: String) -> Command {
                 match tag {
                     Tag::Header(heading_level) => {
                         // Add the last command before starting a new one.
-                        // Don't add the first command during the first iteration.
+                        // Don't add commands for level 1 heading blocks (the title).
                         if heading_level > 1 {
                             commands.push(current_command.build());
+                        } else if heading_level == 1 && commands.len() > 0 {
+                            // Found another level 1 heading block, so quit parsing.
+                            break;
                         }
                         current_command = Command::new(heading_level as u8);
                     }


Test patch:
diff --git a/tests/subcommands_test.rs b/tests/subcommands_test.rs
index 94f8baa..714b68b 100644
--- a/tests/subcommands_test.rs
+++ b/tests/subcommands_test.rs
@@ -131,3 +131,52 @@ echo "Start all services"
             .success();
     }
 }
+
+mod when_another_level_1_heading_is_found {
+    use super::*;
+
+    #[test]
+    fn subcommands_above_level_1_work() {
+        let (_temp, maskfile_path) = common::maskfile(
+            r#"
+## start
+~~~bash
+echo "start"
+~~~
+
+# invalid level 1 heading
+"#,
+        );
+
+        common::run_mask(&maskfile_path)
+            .cli("start")
+            .assert()
+            .stdout(contains("start"))
+            .success();
+    }
+
+    #[test]
+    fn subcommands_below_level_1_are_ignored() {
+        let (_temp, maskfile_path) = common::maskfile(
+            r#"
+## start
+~~~bash
+echo "start"
+~~~
+
+# invalid level 1 heading
+
+## ignored
+~~~bash
+echo "ignored"
+~~~
+"#,
+        );
+
+        common::run_mask(&maskfile_path)
+            .cli("ignored")
+            .assert()
+            .stderr(contains("error: Found argument 'ignored' which wasn't expected, or isn't valid in this context"))
+            .failure();
+    }
+}