-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #435 from alexarchambault/fix-full-help
Ensure WithFullHelp args have a non-empty origin field
- Loading branch information
Showing
6 changed files
with
133 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 56 additions & 12 deletions
68
core/shared/src/main/scala-2/caseapp/core/help/WithFullHelpCompanion.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,63 @@ | ||
package caseapp.core.help | ||
|
||
import caseapp.core.parser.Parser | ||
import caseapp.{ExtraName, HelpMessage} | ||
import caseapp.{ExtraName, Group, HelpMessage, Parser} | ||
import caseapp.core.parser.{Argument, NilParser, StandardArgument} | ||
import caseapp.core.{Arg, Error} | ||
import caseapp.core.parser.RecursiveConsParser | ||
import caseapp.core.util.Formatter | ||
|
||
import shapeless.{HNil, :: => :*:} | ||
|
||
abstract class WithFullHelpCompanion { | ||
|
||
implicit def parser[T, D](implicit underlying: Parser.Aux[T, D]): Parser[WithFullHelp[T]] = | ||
Parser.nil | ||
.addAll[WithHelp[T]].apply | ||
.add[Boolean]( | ||
"helpFull", | ||
default = Some(false), | ||
extraNames = Seq(ExtraName("fullHelp")), | ||
helpMessage = Some(HelpMessage("Print help message, including hidden options, and exit")) | ||
) | ||
.as[WithFullHelp[T]] | ||
implicit def parser[T, D](implicit | ||
underlying: Parser.Aux[T, D] | ||
): Parser.Aux[ | ||
WithFullHelp[T], | ||
(Option[Boolean] :*: Option[Boolean] :*: D :*: HNil) :*: Option[Boolean] :*: HNil | ||
] = { | ||
|
||
val baseHelpArgument = StandardArgument[Boolean]( | ||
Arg("helpFull") | ||
.withExtraNames(Seq( | ||
ExtraName("fullHelp"), | ||
ExtraName("-help-full"), | ||
ExtraName("-full-help") | ||
)) | ||
.withGroup(Some(Group("Help"))) | ||
.withOrigin(Some("WithFullHelp")) | ||
.withHelpMessage(Some( | ||
HelpMessage("Print help message, including hidden options, 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], | ||
index: Int, | ||
d: Option[Boolean], | ||
nameFormatter: Formatter[ExtraName] | ||
): Either[(Error, List[String]), Option[(Option[Boolean], List[String])]] = | ||
args match { | ||
case ("-help-full" | "-full-help") :: rem => Right(Some((Some(true), rem))) | ||
case _ => baseHelpArgument.step(args, index, d, nameFormatter) | ||
} | ||
def get(d: Option[Boolean], nameFormatter: Formatter[ExtraName]) = | ||
baseHelpArgument.get(d, nameFormatter) | ||
} | ||
|
||
val withHelpParser = WithHelp.parser[T, D](underlying) | ||
|
||
val p = RecursiveConsParser(withHelpParser, helpArgument :: NilParser) | ||
|
||
p.to[WithFullHelp[T]] | ||
} | ||
|
||
} |
64 changes: 52 additions & 12 deletions
64
core/shared/src/main/scala-3/caseapp/core/help/WithFullHelpCompanion.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,59 @@ | ||
package caseapp.core.help | ||
|
||
import caseapp.core.parser.Parser | ||
import caseapp.{ExtraName, HelpMessage} | ||
import caseapp.{ExtraName, Group, HelpMessage, Parser} | ||
import caseapp.core.Scala3Helpers.* | ||
import caseapp.core.parser.{Argument, NilParser, StandardArgument} | ||
import caseapp.core.{Arg, Error} | ||
import caseapp.core.parser.RecursiveConsParser | ||
import caseapp.core.util.Formatter | ||
|
||
abstract class WithFullHelpCompanion { | ||
|
||
implicit def parser[T: Parser]: Parser[WithFullHelp[T]] = | ||
Parser.nil | ||
.addAll[WithHelp[T]](using WithHelp.parser[T]) | ||
.add[Boolean]( | ||
"helpFull", | ||
default = Some(false), | ||
extraNames = Seq(ExtraName("fullHelp")), | ||
helpMessage = Some(HelpMessage("Print help message, including hidden options, and exit")) | ||
) | ||
.as[WithFullHelp[T]] | ||
implicit def parser[T](implicit | ||
underlying: Parser[T] | ||
): Parser[WithFullHelp[T]] = { | ||
|
||
val baseHelpArgument = StandardArgument[Boolean]( | ||
Arg("helpFull") | ||
.withExtraNames(Seq( | ||
ExtraName("fullHelp"), | ||
ExtraName("-help-full"), | ||
ExtraName("-full-help") | ||
)) | ||
.withGroup(Some(Group("Help"))) | ||
.withOrigin(Some("WithFullHelp")) | ||
.withHelpMessage(Some( | ||
HelpMessage("Print help message, including hidden options, 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], | ||
index: Int, | ||
d: Option[Boolean], | ||
nameFormatter: Formatter[ExtraName] | ||
): Either[(Error, List[String]), Option[(Option[Boolean], List[String])]] = | ||
args match { | ||
case ("-help-full" | "-full-help") :: rem => Right(Some((Some(true), rem))) | ||
case _ => baseHelpArgument.step(args, index, d, nameFormatter) | ||
} | ||
def get(d: Option[Boolean], nameFormatter: Formatter[ExtraName]) = | ||
baseHelpArgument.get(d, nameFormatter) | ||
} | ||
|
||
val withHelpParser = WithHelp.parser[T](underlying) | ||
|
||
val p = RecursiveConsParser(withHelpParser, helpArgument :: NilParser) | ||
|
||
p.to[WithFullHelp[T]] | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters