Skip to content

Fix the order of help command groups for the default help #1697

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:"))
}
}

}