Repository: jacobdeichert/mask (language: rust)

Issue:
Allow entering negative numbers
Clap by default doesn't allow arg values to start with a hyphen.

This becomes problematic for number values...

```sh
$ mask add 2 -3
error: Found argument '-3' which wasn't expected, or isn't valid in this context

USAGE:
    mask add [FLAGS] <a> <b>

For more information try --help
```


There's two settings:
* for a [specific arg](https://docs.rs/clap/2.33.0/clap/enum.ArgSettings.html#variant.AllowLeadingHyphen)
* for the [entire app](https://docs.rs/clap/2.33.0/clap/enum.AppSettings.html#variant.AllowLeadingHyphen)

And this is the caveat:
> NOTE: Use this setting with caution as it silences certain circumstances which would otherwise be an error (such as accidentally forgetting to specify a value for leading option). It is preferred to set this on a per argument basis, via `Arg::allow_hyphen_values`

Maybe after #3 is done, we can enable this option only for number args?



Discussion / hints:
Oh... i completely missed [`AllowNegativeNumbers`](https://docs.rs/clap/2.33.0/clap/enum.AppSettings.html#variant.AllowNegativeNumbers)...

That's the fix.

Fix patch:
diff --git a/src/main.rs b/src/main.rs
index 5fd006f..905da3e 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -10,6 +10,7 @@ use mask::executor::execute_command;
 fn main() {
     let cli_app = App::new(crate_name!())
         .setting(AppSettings::VersionlessSubcommands)
+        .setting(AppSettings::AllowNegativeNumbers)
         .setting(AppSettings::SubcommandRequired)
         .setting(AppSettings::ColoredHelp)
         .version(crate_version!())
@@ -87,7 +88,8 @@ fn build_subcommands<'a, 'b>(
     for c in subcommands {
         let mut subcmd = SubCommand::with_name(&c.name)
             .about(c.desc.as_ref())
-            .setting(AppSettings::ColoredHelp);
+            .setting(AppSettings::ColoredHelp)
+            .setting(AppSettings::AllowNegativeNumbers);
         if !c.subcommands.is_empty() {
             subcmd = build_subcommands(subcmd, &c.subcommands);
             // If this parent command has no script source, require a subcommand.


Test patch:
diff --git a/tests/subcommands_test.rs b/tests/subcommands_test.rs
index bce53fb..f3bbcf8 100644
--- a/tests/subcommands_test.rs
+++ b/tests/subcommands_test.rs
@@ -55,6 +55,57 @@ fn exits_with_error_when_missing_subcommand() {
         .failure();
 }
 
+mod when_entering_negative_numbers {
+    use super::*;
+
+    #[test]
+    fn allows_entering_negative_numbers_as_values() {
+        let (_temp, maskfile_path) = common::maskfile(
+            r#"
+## math
+### math add (a) (b)
+~~~bash
+echo $(($a + $b))
+~~~
+"#,
+        );
+
+        common::run_mask(&maskfile_path)
+            .cli("math add -1 -3")
+            .assert()
+            .stdout(contains("-4"))
+            .success();
+    }
+
+    #[test]
+    fn allows_entering_negative_numbers_as_flag_values() {
+        let (_temp, maskfile_path) = common::maskfile(
+            r#"
+## math
+### math add
+
+**OPTIONS**
+* a
+    * flags: --a
+    * type: string
+* b
+    * flags: --b
+    * type: string
+
+~~~bash
+echo $(($a + $b))
+~~~
+"#,
+        );
+
+        common::run_mask(&maskfile_path)
+            .cli("math add --a -33 --b 17")
+            .assert()
+            .stdout(contains("-16"))
+            .success();
+    }
+}
+
 mod when_command_has_no_source {
     use super::*;