Skip to content

Commit

Permalink
Add support for filtering individual options in help output (#451)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gedochao authored Jan 26, 2023
1 parent 1acab44 commit 5ca4fd9
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 3 deletions.
3 changes: 3 additions & 0 deletions core/shared/src/main/scala-3/caseapp/core/Scala3Helpers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ object Scala3Helpers {
helpFormat.copy(sortedGroups = sortedGroups)
def withHiddenGroups(hiddenGroups: Option[Seq[String]]): HelpFormat =
helpFormat.copy(hiddenGroups = hiddenGroups)

def withFilterArgs(filterArgs: Option[Arg => Boolean]): HelpFormat =
helpFormat.copy(filterArgs = filterArgs)
}

implicit class OptionParserWithOps[T](private val parser: OptionParser[T]) {
Expand Down
3 changes: 2 additions & 1 deletion core/shared/src/main/scala/caseapp/core/help/Help.scala
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ import caseapp.HelpMessage

def printOptions(b: StringBuilder, format: HelpFormat, showHidden: Boolean): Unit =
if (args.nonEmpty) {
val groupedArgs = args.groupBy(_.group.fold("")(_.name))
val filteredArgs = format.filterArgs.map(args.filter).getOrElse(args)
val groupedArgs = filteredArgs.groupBy(_.group.fold("")(_.name))
val groups = format.sortGroupValues(groupedArgs.toVector)
val sortedGroups = groups.filter(_._1.nonEmpty) ++ groupedArgs.get("").toSeq.map("" -> _)
for {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package caseapp.core.help

import caseapp.core.Arg
import caseapp.core.Scala3Helpers._
import caseapp.core.util.fansi
import dataclass._
Expand All @@ -15,7 +16,8 @@ import dataclass._
sortCommandGroups: Option[Seq[String] => Seq[String]] = None,
sortedCommandGroups: Option[Seq[String]] = None,
hidden: fansi.Attrs = fansi.Attrs.Empty,
terminalWidthOpt: Option[Int] = None
terminalWidthOpt: Option[Int] = None,
@since filterArgs: Option[Arg => Boolean] = None
) {
private def sortValues[T](
sortGroups: Option[Seq[String] => Seq[String]],
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", "5a653bdd41972587bd3fb3d9751c0e0cf11c1e74")
Seq("git", "tag", "--merged", "HEAD^", "--contains", "1acab44cf68aeebb575bd1c920f96397519a18d0")
.!!
.linesIterator
.map(_.trim)
Expand Down
1 change: 1 addition & 0 deletions tests/shared/src/test/scala/caseapp/HelpDefinitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package caseapp
object HelpDefinitions {
case class FirstOptions(
@ExtraName("f")
@Tag("foo")
foo: String = "",
bar: Int = 0
)
Expand Down
31 changes: 31 additions & 0 deletions tests/shared/src/test/scala/caseapp/HelpTests.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package caseapp

import caseapp.core.Arg
import caseapp.core.app.CommandsEntryPoint
import caseapp.core.help.{Help, HelpFormat, RuntimeCommandHelp, RuntimeCommandsHelp}
import caseapp.core.Scala3Helpers._
Expand Down Expand Up @@ -399,6 +400,36 @@ object HelpTests extends TestSuite {

assert(help == expected)
}
test("help message with filtered args") {
val entryPoint: CommandsEntryPoint = new CommandsEntryPoint {
def progName = "foo"

override def defaultCommand = Some(CommandGroups.First)

def commands = Seq(CommandGroups.First, CommandGroups.Second, CommandGroups.Third)
}
val filterArgsFunction = (a: Arg) => !a.tags.exists(_.name == "foo")
val formatWithHiddenGroup = format.withFilterArgs(Some(filterArgsFunction))
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:
| --bar int
|
|Aa commands:
| first
| third Third help message
|
|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 5ca4fd9

Please sign in to comment.