Skip to content

Commit 26bd940

Browse files
authored
Fix the order of help command groups for the default help (#1697)
1 parent 2bb0a40 commit 26bd940

File tree

3 files changed

+66
-49
lines changed

3 files changed

+66
-49
lines changed

modules/cli/src/main/scala/scala/cli/commands/ScalaCommand.scala

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import scala.build.errors.BuildException
1717
import scala.build.internal.{Constants, Runner}
1818
import scala.build.options.{BuildOptions, Scope}
1919
import scala.build.{Artifacts, Logger, Positioned, ReplArtifacts}
20-
import scala.cli.commands.shared.{HasLoggingOptions, ScalacOptions, SharedOptions}
20+
import scala.cli.commands.shared.{HasLoggingOptions, ScalaCliHelp, ScalacOptions, SharedOptions}
2121
import scala.cli.commands.util.CommandHelpers
2222
import scala.cli.commands.util.ScalacOptionsUtil.*
2323
import scala.cli.{CurrentParams, ScalaCli}
@@ -234,49 +234,7 @@ abstract class ScalaCommand[T <: HasLoggingOptions](implicit myParser: Parser[T]
234234
sys.exit(exitCode.orExit(logger))
235235
}
236236

237-
override def helpFormat: HelpFormat =
238-
HelpFormat.default().copy(
239-
sortedGroups = Some(Seq(
240-
"Help",
241-
"Scala",
242-
"Java",
243-
"Repl",
244-
"Package",
245-
"Metabrowse server",
246-
"Logging",
247-
"Runner"
248-
)),
249-
sortedCommandGroups = Some(Seq(
250-
"Main",
251-
"Miscellaneous",
252-
""
253-
)),
254-
hiddenGroups = Some(Seq(
255-
"Scala.js",
256-
"Scala Native"
257-
)),
258-
terminalWidthOpt =
259-
if (Properties.isWin)
260-
if (coursier.paths.Util.useJni())
261-
Try(coursier.jniutils.WindowsAnsiTerminal.terminalSize()).toOption.map(
262-
_.getWidth
263-
).orElse {
264-
val fallback = 120
265-
if (java.lang.Boolean.getBoolean("scala.cli.windows-terminal.verbose"))
266-
System.err.println(s"Could not get terminal width, falling back to $fallback")
267-
Some(fallback)
268-
}
269-
else None
270-
else
271-
// That's how Ammonite gets the terminal width, but I'd rather not spawn a sub-process upfront in Scala CLI…
272-
// val pathedTput = if (os.isFile(os.Path("/usr/bin/tput"))) "/usr/bin/tput" else "tput"
273-
// val width = os.proc("sh", "-c", s"$pathedTput cols 2>/dev/tty").call(stderr = os.Pipe).out.trim().toInt
274-
// Some(width)
275-
// Ideally, we should do an ioctl, like jansi does here:
276-
// https://github.com/fusesource/jansi/blob/09722b7cccc8a99f14ac1656db3072dbeef34478/src/main/java/org/fusesource/jansi/AnsiConsole.java#L344
277-
// This requires writing our own minimal JNI library, that publishes '.a' files too for static linking in the executable of Scala CLI.
278-
None
279-
)
237+
override def helpFormat: HelpFormat = ScalaCliHelp.helpFormat
280238

281239
/** @param options
282240
* command-specific [[T]] options

modules/cli/src/main/scala/scala/cli/commands/shared/ScalaCliHelp.scala

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,56 @@ package scala.cli.commands.shared
22

33
import caseapp.core.help.HelpFormat
44

5+
import scala.util.{Properties, Try}
6+
57
object ScalaCliHelp {
6-
val helpFormat = HelpFormat.default()
8+
val helpFormat: HelpFormat = HelpFormat.default()
9+
.copy(
10+
sortedGroups = Some(
11+
Seq(
12+
"Help",
13+
"Scala",
14+
"Java",
15+
"Repl",
16+
"Package",
17+
"Metabrowse server",
18+
"Logging",
19+
"Runner"
20+
)
21+
),
22+
sortedCommandGroups = Some(
23+
Seq(
24+
"Main",
25+
"Miscellaneous",
26+
""
27+
)
28+
),
29+
hiddenGroups = Some(
30+
Seq(
31+
"Scala.js",
32+
"Scala Native"
33+
)
34+
),
35+
terminalWidthOpt =
36+
if (Properties.isWin)
37+
if (coursier.paths.Util.useJni())
38+
Try(coursier.jniutils.WindowsAnsiTerminal.terminalSize()).toOption.map(
39+
_.getWidth
40+
).orElse {
41+
val fallback = 120
42+
if (java.lang.Boolean.getBoolean("scala.cli.windows-terminal.verbose"))
43+
System.err.println(s"Could not get terminal width, falling back to $fallback")
44+
Some(fallback)
45+
}
46+
else None
47+
else
48+
// That's how Ammonite gets the terminal width, but I'd rather not spawn a sub-process upfront in Scala CLI…
49+
// val pathedTput = if (os.isFile(os.Path("/usr/bin/tput"))) "/usr/bin/tput" else "tput"
50+
// val width = os.proc("sh", "-c", s"$pathedTput cols 2>/dev/tty").call(stderr = os.Pipe).out.trim().toInt
51+
// Some(width)
52+
// Ideally, we should do an ioctl, like jansi does here:
53+
// https://github.com/fusesource/jansi/blob/09722b7cccc8a99f14ac1656db3072dbeef34478/src/main/java/org/fusesource/jansi/AnsiConsole.java#L344
54+
// This requires writing our own minimal JNI library, that publishes '.a' files too for static linking in the executable of Scala CLI.
55+
None
56+
)
757
}

modules/integration/src/test/scala/scala/cli/integration/HelpTests.scala

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,20 @@ package scala.cli.integration
33
import com.eed3si9n.expecty.Expecty.expect
44

55
class HelpTests extends ScalaCliSuite {
6-
test("help command") {
7-
for { helpOption <- Seq("help", "-help", "--help") } {
8-
val help = os.proc(TestUtil.cli, helpOption).call(check = false)
6+
for { helpOption <- Seq("help", "-help", "--help") } {
7+
val help = os.proc(TestUtil.cli, helpOption).call(check = false)
8+
val helpOutput = help.out.trim()
9+
test(s"$helpOption works correctly") {
910
assert(help.exitCode == 0, clues(helpOption, help.out.text(), help.err.text(), help.exitCode))
10-
expect(help.out.text().contains("Usage:"))
11+
expect(helpOutput.contains("Usage:"))
12+
}
13+
14+
test(s"$helpOption output command groups are ordered correctly") {
15+
val mainCommandsIndex = helpOutput.indexOf("Main commands:")
16+
val miscellaneousIndex = helpOutput.indexOf("Miscellaneous commands:")
17+
expect(mainCommandsIndex < miscellaneousIndex)
18+
expect(miscellaneousIndex < helpOutput.indexOf("Other commands:"))
1119
}
1220
}
21+
1322
}

0 commit comments

Comments
 (0)