Skip to content

Commit

Permalink
Fix the order of help command groups for the default help
Browse files Browse the repository at this point in the history
  • Loading branch information
Gedochao committed Dec 15, 2022
1 parent 213afb3 commit 7a64d26
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 49 deletions.
46 changes: 2 additions & 44 deletions modules/cli/src/main/scala/scala/cli/commands/ScalaCommand.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import scala.build.errors.BuildException
import scala.build.internal.{Constants, Runner}
import scala.build.options.{BuildOptions, Scope}
import scala.build.{Artifacts, Logger, Positioned, ReplArtifacts}
import scala.cli.commands.shared.{HasLoggingOptions, ScalacOptions, SharedOptions}
import scala.cli.commands.shared.{HasLoggingOptions, ScalaCliHelp, ScalacOptions, SharedOptions}
import scala.cli.commands.util.CommandHelpers
import scala.cli.commands.util.ScalacOptionsUtil.*
import scala.cli.{CurrentParams, ScalaCli}
Expand Down Expand Up @@ -234,49 +234,7 @@ abstract class ScalaCommand[T <: HasLoggingOptions](implicit myParser: Parser[T]
sys.exit(exitCode.orExit(logger))
}

override def helpFormat: HelpFormat =
HelpFormat.default().copy(
sortedGroups = Some(Seq(
"Help",
"Scala",
"Java",
"Repl",
"Package",
"Metabrowse server",
"Logging",
"Runner"
)),
sortedCommandGroups = Some(Seq(
"Main",
"Miscellaneous",
""
)),
hiddenGroups = Some(Seq(
"Scala.js",
"Scala Native"
)),
terminalWidthOpt =
if (Properties.isWin)
if (coursier.paths.Util.useJni())
Try(coursier.jniutils.WindowsAnsiTerminal.terminalSize()).toOption.map(
_.getWidth
).orElse {
val fallback = 120
if (java.lang.Boolean.getBoolean("scala.cli.windows-terminal.verbose"))
System.err.println(s"Could not get terminal width, falling back to $fallback")
Some(fallback)
}
else None
else
// That's how Ammonite gets the terminal width, but I'd rather not spawn a sub-process upfront in Scala CLI…
// val pathedTput = if (os.isFile(os.Path("/usr/bin/tput"))) "/usr/bin/tput" else "tput"
// val width = os.proc("sh", "-c", s"$pathedTput cols 2>/dev/tty").call(stderr = os.Pipe).out.trim().toInt
// Some(width)
// Ideally, we should do an ioctl, like jansi does here:
// https://github.com/fusesource/jansi/blob/09722b7cccc8a99f14ac1656db3072dbeef34478/src/main/java/org/fusesource/jansi/AnsiConsole.java#L344
// This requires writing our own minimal JNI library, that publishes '.a' files too for static linking in the executable of Scala CLI.
None
)
override def helpFormat: HelpFormat = ScalaCliHelp.helpFormat

/** @param options
* command-specific [[T]] options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,56 @@ package scala.cli.commands.shared

import caseapp.core.help.HelpFormat

import scala.util.{Properties, Try}

object ScalaCliHelp {
val helpFormat = HelpFormat.default()
val helpFormat: HelpFormat = HelpFormat.default()
.copy(
sortedGroups = Some(
Seq(
"Help",
"Scala",
"Java",
"Repl",
"Package",
"Metabrowse server",
"Logging",
"Runner"
)
),
sortedCommandGroups = Some(
Seq(
"Main",
"Miscellaneous",
""
)
),
hiddenGroups = Some(
Seq(
"Scala.js",
"Scala Native"
)
),
terminalWidthOpt =
if (Properties.isWin)
if (coursier.paths.Util.useJni())
Try(coursier.jniutils.WindowsAnsiTerminal.terminalSize()).toOption.map(
_.getWidth
).orElse {
val fallback = 120
if (java.lang.Boolean.getBoolean("scala.cli.windows-terminal.verbose"))
System.err.println(s"Could not get terminal width, falling back to $fallback")
Some(fallback)
}
else None
else
// That's how Ammonite gets the terminal width, but I'd rather not spawn a sub-process upfront in Scala CLI…
// val pathedTput = if (os.isFile(os.Path("/usr/bin/tput"))) "/usr/bin/tput" else "tput"
// val width = os.proc("sh", "-c", s"$pathedTput cols 2>/dev/tty").call(stderr = os.Pipe).out.trim().toInt
// Some(width)
// Ideally, we should do an ioctl, like jansi does here:
// https://github.com/fusesource/jansi/blob/09722b7cccc8a99f14ac1656db3072dbeef34478/src/main/java/org/fusesource/jansi/AnsiConsole.java#L344
// This requires writing our own minimal JNI library, that publishes '.a' files too for static linking in the executable of Scala CLI.
None
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@ package scala.cli.integration
import com.eed3si9n.expecty.Expecty.expect

class HelpTests extends ScalaCliSuite {
test("help command") {
for { helpOption <- Seq("help", "-help", "--help") } {
val help = os.proc(TestUtil.cli, helpOption).call(check = false)
for { helpOption <- Seq("help", "-help", "--help") } {
val help = os.proc(TestUtil.cli, helpOption).call(check = false)
val helpOutput = help.out.trim()
test(s"$helpOption works correctly") {
assert(help.exitCode == 0, clues(helpOption, help.out.text(), help.err.text(), help.exitCode))
expect(help.out.text().contains("Usage:"))
expect(helpOutput.contains("Usage:"))
}

test(s"$helpOption output command groups are ordered correctly") {
val mainCommandsIndex = helpOutput.indexOf("Main commands:")
val miscellaneousIndex = helpOutput.indexOf("Miscellaneous commands:")
expect(mainCommandsIndex < miscellaneousIndex)
expect(miscellaneousIndex < helpOutput.indexOf("Other commands:"))
}
}

}

0 comments on commit 7a64d26

Please sign in to comment.