Skip to content

Commit

Permalink
Allow to hide group in help message (#354)
Browse files Browse the repository at this point in the history
  • Loading branch information
lwronski authored Dec 13, 2021
1 parent bddd3ce commit 81ae633
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ abstract class CommandsEntryPoint extends PlatformCommandsMethods {

def progName: String
def description: String = ""
def summaryDesc: String = ""

def help: RuntimeCommandsHelp =
RuntimeCommandsHelp(
progName,
Some(description).filter(_.nonEmpty),
defaultCommand.map(_.finalHelp: Help[_]).getOrElse(Help[Unit]()),
commands.map(cmd => RuntimeCommandHelp(cmd.names, cmd.finalHelp, cmd.group, cmd.hidden))
commands.map(cmd => RuntimeCommandHelp(cmd.names, cmd.finalHelp, cmd.group, cmd.hidden)),
Some(summaryDesc).filter(_.nonEmpty)
)

def helpFormat: HelpFormat =
Expand Down
2 changes: 2 additions & 0 deletions core/shared/src/main/scala/caseapp/core/help/Help.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import caseapp.HelpMessage
helpMessage: Option[HelpMessage] = Help.DefaultHelpMessage
) {

def nonEmpty = args.nonEmpty

/** One-line usage message for `T` */
def usage: String =
usage(HelpFormat.default())
Expand Down
8 changes: 6 additions & 2 deletions core/shared/src/main/scala/caseapp/core/help/HelpFormat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import dataclass._
@since("2.1.0")
sortGroups: Option[Seq[String] => Seq[String]] = None,
sortedGroups: Option[Seq[String]] = None,
hiddenGroups: Option[Seq[String]] = None,
@since("2.1.0")
sortCommandGroups: Option[Seq[String] => Seq[String]] = None,
sortedCommandGroups: Option[Seq[String]] = None,
Expand All @@ -25,8 +26,8 @@ import dataclass._
sortGroups: Option[Seq[String] => Seq[String]],
sortedGroups: Option[Seq[String]],
elems: Seq[(String, T)]
): Seq[(String, T)] =
sortGroups match {
): Seq[(String, T)] = {
val sortedGroups0 = sortGroups match {
case None =>
sortedGroups match {
case None =>
Expand All @@ -39,6 +40,9 @@ import dataclass._
val sorted = sort(elems.map(_._1)).zipWithIndex.toMap
elems.sortBy { case (group, _) => sorted.getOrElse(group, Int.MaxValue) }
}
sortedGroups0.filter { case (group, _) => hiddenGroups.forall(!_.contains(group)) }
}

def sortGroupValues[T](elems: Seq[(String, T)]): Seq[(String, T)] =
sortValues(sortGroups, sortedGroups, elems)
def sortCommandGroupValues[T](elems: Seq[(String, T)]): Seq[(String, T)] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import dataclass._
progName: String,
description: Option[String],
defaultHelp: Help[_],
commands: Seq[RuntimeCommandHelp[_]]
commands: Seq[RuntimeCommandHelp[_]],
summaryDesc: Option[String]
) {

def help(): String =
Expand Down Expand Up @@ -47,16 +48,23 @@ import dataclass._
format.terminalWidthOpt.getOrElse(Int.MaxValue)
)

b.append(format.newLine)

defaultHelp.printOptions(b, format, showHidden)
if (defaultHelp.nonEmpty) {
b.append(format.newLine)
defaultHelp.printOptions(b, format, showHidden)
b.append(format.newLine)
}

if (commands.nonEmpty) {
b.append(format.newLine)
b.append(format.newLine)
printCommands(b, format, showHidden)
}

for (argName <- summaryDesc) {
b.append(format.newLine)
b.append(format.newLine)
b.append(argName)
}

b.result()
}

Expand Down
2 changes: 1 addition & 1 deletion project/Mima.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import scala.sys.process._
object Mima {

def binaryCompatibilityVersions: Set[String] =
Seq("git", "tag", "--merged", "HEAD^", "--contains", "9077d4c5067b68f2202ba7004846a894e56f2829")
Seq("git", "tag", "--merged", "HEAD^", "--contains", "bddd3cef13d7fa8d4aeeaddfccfb9a1d932bc157")
.!!
.linesIterator
.map(_.trim)
Expand Down
89 changes: 88 additions & 1 deletion tests/shared/src/test/scala/caseapp/HelpTests.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package caseapp

import caseapp.core.app.CommandsEntryPoint
import caseapp.core.help.{Help, HelpFormat}
import caseapp.core.help.{Help, HelpFormat, RuntimeCommandHelp, RuntimeCommandsHelp}
import utest._

object HelpTests extends TestSuite {
Expand Down Expand Up @@ -318,7 +318,94 @@ object HelpTests extends TestSuite {

assert(help == expected)
}
test("empty help in help message") {
val entryPoint = new CommandsEntryPoint {
def progName = "foo"
override def defaultCommand = Some(CommandGroups.First)
def commands = Seq(CommandGroups.First, CommandGroups.Second, CommandGroups.Third)

override def help: RuntimeCommandsHelp = new RuntimeCommandsHelp(
progName,
None,
Help[Unit](),
commands.map(cmd =>
new RuntimeCommandHelp(cmd.names, cmd.finalHelp, cmd.group, cmd.hidden)
),
None
)
}
val help = entryPoint.help.help(format)
val expected =
"""Usage: foo <COMMAND>
|
|Aa commands:
| first
| third Third help message
|
|Bb commands:
| second""".stripMargin

assert(help == expected)
}
test("help message with summary description") {
val entryPoint = new CommandsEntryPoint {
def progName = "foo"
override def defaultCommand = Some(CommandGroups.First)
def commands = Seq(CommandGroups.First, CommandGroups.Second, CommandGroups.Third)

override def help: RuntimeCommandsHelp = new RuntimeCommandsHelp(
progName,
Some("Description"),
Help[Unit](),
commands.map(cmd =>
new RuntimeCommandHelp(cmd.names, cmd.finalHelp, cmd.group, cmd.hidden)
),
Some("Summary Description")
)
}
val help = entryPoint.help.help(format)
val expected =
"""Usage: foo <COMMAND>
|Description
|
|Aa commands:
| first
| third Third help message
|
|Bb commands:
| second
|
|Summary Description""".stripMargin

assert(help == expected)
}
test("help message with hidden group") {
val entryPoint = new CommandsEntryPoint {
def progName = "foo"
override def defaultCommand = Some(CommandGroups.First)
def commands = Seq(CommandGroups.First, CommandGroups.Second, CommandGroups.Third)
}
val formatWithHiddenGroup = format.withHiddenGroups(Some(Seq(
CommandGroups.First.group,
CommandGroups.Third.group
)))
val help = entryPoint.help.help(formatWithHiddenGroup)
val expected =
"""Usage: foo <COMMAND> [options]
|
|Help options:
| --usage Print usage and exit
| -h, -help, --help Print help message and exit
|
|Other options:
| -f, --foo string
| --bar int
|
|Bb commands:
| second""".stripMargin

assert(help == expected)
}
test("hidden commands in help message") {
val entryPoint = new CommandsEntryPoint {
def progName = "foo"
Expand Down

0 comments on commit 81ae633

Please sign in to comment.