Skip to content

Commit

Permalink
Print help for '-help' too (#310)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexarchambault authored Jul 23, 2021
1 parent 0e6db57 commit 1b90b2a
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 12 deletions.
8 changes: 6 additions & 2 deletions core/shared/src/main/scala/caseapp/core/help/Help.scala
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,14 @@ object Help {

private def optionsTable(args: Seq[Arg], format: HelpFormat, nameFormatter: Formatter[Name], showHidden: Boolean): Seq[Seq[fansi.Str]] =
for (arg <- args if showHidden || !arg.noHelp) yield {
val sortedNames = (arg.name +: arg.extraNames).groupBy(_.name.length).toVector.sortBy(_._1).flatMap(_._2)
val sortedNames = (arg.name +: arg.extraNames)
.map(name => format.option(name.option(nameFormatter)))
.groupBy(_.length)
.toVector
.sortBy(_._1)
.flatMap(_._2)
val options = sortedNames
.iterator
.map(name => format.option(name.option(nameFormatter)))
.zip(Iterator.continually(", ": fansi.Str))
.flatMap { case (a, b) => Iterator(a, b) }
.toVector
Expand Down
63 changes: 61 additions & 2 deletions core/shared/src/main/scala/caseapp/core/help/WithHelp.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package caseapp.core.help

import caseapp.core.Error
import caseapp.{ExtraName, Group, HelpMessage, Recurse}
import caseapp.{ExtraName, Group, Help, HelpMessage, Parser, Recurse}
import caseapp.core.parser.{Argument, NilParser, StandardArgument}
import caseapp.core.{Arg, Error}
import caseapp.core.parser.{EitherParser, RecursiveConsParser}
import caseapp.core.util.Formatter

import shapeless.{HNil, :: => :*:}

/**
* Helper to add `--usage` and `--help` options to an existing type `T`.
Expand All @@ -25,3 +30,57 @@ final case class WithHelp[T](
def map[U](f: T => U): WithHelp[U] =
copy(baseOrError = baseOrError.map(f))
}

object WithHelp {

implicit def parser[T, D](implicit underlying: Parser.Aux[T, D]): Parser.Aux[WithHelp[T], Option[Boolean] :*: Option[Boolean] :*: D :*: HNil] = {

val usageArgument = StandardArgument[Boolean](
Arg("usage")
.withGroup(Some(Group("Help")))
.withOrigin(Some("WithHelp"))
.withHelpMessage(Some(HelpMessage("Print usage and exit")))
.withIsFlag(true)
).withDefault(() => Some(false))

val baseHelpArgument = StandardArgument[Boolean](
Arg("help")
.withExtraNames(Seq(ExtraName("h"), ExtraName("-help")))
.withGroup(Some(Group("Help")))
.withOrigin(Some("WithHelp"))
.withHelpMessage(Some(HelpMessage("Print help message and exit")))
.withIsFlag(true)
).withDefault(() => Some(false))

// accept "-help" too (single dash)
val helpArgument: Argument[Boolean] =
new Argument[Boolean] {
def arg = baseHelpArgument.arg
def withDefaultOrigin(origin: String) =
this
def init = baseHelpArgument.init
def step(
args: List[String],
d: Option[Boolean],
nameFormatter: Formatter[ExtraName]
): Either[(Error, List[String]), Option[(Option[Boolean], List[String])]] =
args match {
case "-help" :: rem => Right(Some((Some(true), rem)))
case _ => baseHelpArgument.step(args, d, nameFormatter)
}
def get(d: Option[Boolean], nameFormatter: Formatter[ExtraName]) =
baseHelpArgument.get(d, nameFormatter)
}

val either = EitherParser[T, D](underlying)

val p = usageArgument ::
helpArgument ::
RecursiveConsParser(either, NilParser)

p.to[WithHelp[T]]
}

implicit def help[T, D](implicit parser: Parser.Aux[T, D], underlying: Help[T]): Help[WithHelp[T]] =
Help.derive[WithHelp[T]]
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ import dataclass.data
f(l.head) :: l.tail
}

def ::[A](argument: Argument[A]): ConsParser[A, H :: T, D] =
ConsParser[A, H :: T, D](argument, this)

def withDefaultOrigin(origin: String): Parser.Aux[H :: T, D] =
withHeadParser(headParser.withDefaultOrigin(origin))
.withTailParser(tailParser.withDefaultOrigin(origin))
Expand Down
1 change: 1 addition & 0 deletions core/shared/src/main/scala/caseapp/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package object caseapp {

type ExtraName = Name
val ExtraName = Name

type Parser[T] = core.parser.Parser[T]
val Parser = core.parser.Parser
Expand Down
13 changes: 13 additions & 0 deletions tests/shared/src/test/scala/caseapp/CaseAppTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,19 @@ object CaseAppTests extends TestSuite {
assert(messageOpt.contains("Unrecognized argument: --help"))
}

test("accept -help") {
val res =
Parser[FewArgs].withHelp.detailedParse(
Seq("-help")
)

val expectedRes =
Right(
(WithHelp(false, true, Right(FewArgs())), RemainingArgs(List(), List()))
)
assert(res == expectedRes)
}

}

}
16 changes: 8 additions & 8 deletions tests/shared/src/test/scala/caseapp/HelpTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ object HelpTests extends TestSuite {
"""Usage: foo <COMMAND> [options]
|
|Help options:
| --usage Print usage and exit
| -h, --help Print help message and exit
| --usage Print usage and exit
| -h, -help, --help Print help message and exit
|
|Other options:
| -f, --foo string
Expand All @@ -283,8 +283,8 @@ object HelpTests extends TestSuite {
"""Usage: foo <COMMAND> [options]
|
|Help options:
| --usage Print usage and exit
| -h, --help Print help message and exit
| --usage Print usage and exit
| -h, -help, --help Print help message and exit
|
|Other options:
| -f, --foo string
Expand Down Expand Up @@ -312,8 +312,8 @@ object HelpTests extends TestSuite {
"""Usage: foo <COMMAND> [options]
|
|Help options:
| --usage Print usage and exit
| -h, --help Print help message and exit
| --usage Print usage and exit
| -h, -help, --help Print help message and exit
|
|Other options:
| -f, --foo string
Expand All @@ -333,8 +333,8 @@ object HelpTests extends TestSuite {
"""Usage: foo <COMMAND> [options]
|
|Help options:
| --usage Print usage and exit
| -h, --help Print help message and exit
| --usage Print usage and exit
| -h, -help, --help Print help message and exit
|
|Other options:
| -f, --foo string
Expand Down

0 comments on commit 1b90b2a

Please sign in to comment.