summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSom Snytt <som.snytt@gmail.com>2016-06-26 00:43:06 -0400
committerSom Snytt <som.snytt@gmail.com>2016-10-11 22:23:57 -0700
commit12fb6fee44177742c68be6b4ef46709d0dd6db06 (patch)
treeb582b2bbabd30b14c37a19aff9cabe8546af7981
parent823b2d9b130296576d91a86a45e7f6c7d7d94b48 (diff)
downloadscala-12fb6fee44177742c68be6b4ef46709d0dd6db06.tar.gz
scala-12fb6fee44177742c68be6b4ef46709d0dd6db06.tar.bz2
scala-12fb6fee44177742c68be6b4ef46709d0dd6db06.zip
SI-9832 -Xlint:help shows default
Conclude help method with the default list. Extra words are supplied for underscore.
-rw-r--r--src/compiler/scala/tools/nsc/settings/MutableSettings.scala18
-rw-r--r--test/junit/scala/tools/nsc/settings/SettingsTest.scala63
2 files changed, 76 insertions, 5 deletions
diff --git a/src/compiler/scala/tools/nsc/settings/MutableSettings.scala b/src/compiler/scala/tools/nsc/settings/MutableSettings.scala
index b4987e1240..11cde935f2 100644
--- a/src/compiler/scala/tools/nsc/settings/MutableSettings.scala
+++ b/src/compiler/scala/tools/nsc/settings/MutableSettings.scala
@@ -744,11 +744,19 @@ class MutableSettings(val errorFn: String => Unit)
def isHelping: Boolean = sawHelp
def help: String = {
- val choiceLength = choices.map(_.length).max + 1
- val formatStr = s" %-${choiceLength}s %s"
- choices.zipAll(descriptions, "", "").map {
- case (arg, descr) => formatStr.format(arg, descr)
- } mkString (f"$descr%n", f"%n", "")
+ val describe: ((String, String)) => String = {
+ val choiceWidth = choices.map(_.length).max + 1
+ val formatStr = s" %-${choiceWidth}s %s"
+ locally {
+ case (choice, description) => formatStr.format(choice, description)
+ }
+ }
+ val verboseDefault = default match {
+ case Some("_" :: Nil) => Some("All choices are enabled by default." :: Nil)
+ case _ => default
+ }
+ val orelse = verboseDefault.map(_.mkString(f"%nDefault: ", ", ", f"%n")).getOrElse("")
+ choices.zipAll(descriptions, "", "").map(describe).mkString(f"${descr}%n", f"%n", orelse)
}
def clear(): Unit = {
diff --git a/test/junit/scala/tools/nsc/settings/SettingsTest.scala b/test/junit/scala/tools/nsc/settings/SettingsTest.scala
index 96f83c4c2f..3fdf758619 100644
--- a/test/junit/scala/tools/nsc/settings/SettingsTest.scala
+++ b/test/junit/scala/tools/nsc/settings/SettingsTest.scala
@@ -180,4 +180,67 @@ class SettingsTest {
assertThrows[IllegalArgumentException](check(expected = "2.11", "-Xsource", "2.11"), _ == "-Xsource requires an argument, the syntax is -Xsource:<version>")
assertThrows[IllegalArgumentException](check(expected = "2.11", "-Xsource:2.invalid"), _ contains "There was a problem parsing 2.invalid")
}
+
+ @Test def helpHasDefault(): Unit = {
+ val s = new MutableSettings(msg => throw new IllegalArgumentException(msg))
+ object mChoices extends s.MultiChoiceEnumeration {
+ val a = Choice("a", "help a")
+ val b = Choice("b", "help b")
+ val c = Choice("c", "help c")
+ }
+ val m = s.MultiChoiceSetting("-m", "args", "magic sauce", mChoices, Some(List("b")))
+
+ def check(args: String*)(t: s.MultiChoiceSetting[mChoices.type] => Boolean): Boolean = {
+ m.clear()
+ val (ok, rest) = s.processArguments(args.toList, processAll = true)
+ assert(rest.isEmpty)
+ t(m)
+ }
+
+ import mChoices._
+
+ assertTrue(check("-m")(_.value == Set(b)))
+ assertTrue(check("-m") { _ =>
+ assertEquals(
+ """magic sauce
+ | a help a
+ | b help b
+ | c help c
+ |Default: b
+ |""".stripMargin,
+ m.help)
+ true
+ })
+ }
+ @Test def helpHasDefaultAll(): Unit = {
+ val s = new MutableSettings(msg => throw new IllegalArgumentException(msg))
+ object mChoices extends s.MultiChoiceEnumeration {
+ val a = Choice("a", "help a")
+ val b = Choice("b", "help b")
+ val c = Choice("c", "help c")
+ }
+ val m = s.MultiChoiceSetting("-m", "args", "magic sauce", mChoices, Some(List("_")))
+
+ def check(args: String*)(t: s.MultiChoiceSetting[mChoices.type] => Boolean): Boolean = {
+ m.clear()
+ val (ok, rest) = s.processArguments(args.toList, processAll = true)
+ assert(rest.isEmpty)
+ t(m)
+ }
+
+ import mChoices._
+
+ assertTrue(check("-m")(_.value == Set(a, b, c)))
+ assertTrue(check("-m") { _ =>
+ assertEquals(
+ """magic sauce
+ | a help a
+ | b help b
+ | c help c
+ |Default: All choices are enabled by default.
+ |""".stripMargin,
+ m.help)
+ true
+ })
+ }
}