[Mono.Options] Improve error message for badly bundled args.
authorJonathan Pryor <jonpryor@vt.edu>
Thu, 5 Dec 2013 20:39:05 +0000 (15:39 -0500)
committerJonathan Pryor <jonpryor@vt.edu>
Thu, 5 Dec 2013 20:43:35 +0000 (15:43 -0500)
mono(1) supports a "joined" verbose flag, `mono -v-v-v-v`, which
Mono.Options doesn't support. Attempting to use it, however, resulted
in a "weird" error message:

$ csharp -r:Mono.Options.dll
csharp> using Mono.Options;
csharp> var p = new OptionSet { { "v", v => {} } };
csharp> p.Parse (new[]{"-v-v-v"});
Mono.Options.OptionException: Cannot bundle unregistered option '--'.
...

This looks very weird to users because they never used a "--" option,
and if they had then `--` would have disabled further option
processing (as documented and implemented in OptionSet.Parse()).

Confusion all around.

Improve the error message so that it instead generates:

Mono.Options.OptionException: Cannot use unregistered option '-' in bundle '-v-v-v'.

That way the user gets the full context of where this "unregistered
option" came from.

mcs/class/Mono.Options/Mono.Options/Options.cs
mcs/class/Mono.Options/Test/Mono.Options/OptionSetTest.cs

index 5af7fe0f387f9b18f0e788d613eb3f29084c7a43..0fd21ab5a93e47bb56286dff21edd86d17171242 100644 (file)
@@ -1135,7 +1135,7 @@ namespace Mono.Options
                                        if (i == 0)
                                                return false;
                                        throw new OptionException (string.Format (localizer (
-                                                                       "Cannot bundle unregistered option '{0}'."), opt), opt);
+                                                                       "Cannot use unregistered option '{0}' in bundle '{1}'."), rn, f + n), null);
                                }
                                p = this [rn];
                                switch (p.OptionValueType) {
index 666bad54d5b8d90f021a32011c1b3c8f4eab3c0e..086c6d9c8c24a6f76a4463ea00e16881c9d8daf7 100644 (file)
@@ -193,7 +193,7 @@ namespace Tests.Mono.Options
                        Assert.AreEqual (libs [1], null);
 
                        Utils.AssertException (typeof(OptionException), 
-                                       "Cannot bundle unregistered option '-V'.",
+                                       "Cannot use unregistered option 'V' in bundle '-EVALUENOTSUP'.",
                                        p, v => { v.Parse (_("-EVALUENOTSUP")); });
                }
 
@@ -370,7 +370,7 @@ namespace Tests.Mono.Options
 
                        // try to bundle with an option requiring a value
                        Utils.AssertException (typeof(OptionException), 
-                                       "Cannot bundle unregistered option '-z'.", 
+                                       "Cannot use unregistered option 'z' in bundle '-cz'.",
                                        p, v => { v.Parse (_("-cz", "extra")); });
 
                        Utils.AssertException (typeof(ArgumentNullException), 
@@ -880,6 +880,22 @@ namespace Tests.Mono.Options
                        Assert.AreEqual (formats ["baz"][0], "e");
                        Assert.AreEqual (formats ["baz"][1], "f");
                }
+
+               [Test]
+               public void ReportInvalidDuplication ()
+               {
+                       int verbosity = 0;
+                       var p = new OptionSet () {
+                               { "v", v => verbosity = v != null ? verbosity + 1 : verbosity },
+                       };
+                       try {
+                               p.Parse (new []{"-v-v-v"});
+                               Assert.Fail ("Should not be reached.");
+                       } catch (OptionException e) {
+                               Assert.AreEqual (null, e.OptionName);
+                               Assert.AreEqual ("Cannot use unregistered option '-' in bundle '-v-v-v'.", e.Message);
+                       }
+               }
        }
 }