|
1 | 1 | package caseapp.core.help
|
2 | 2 |
|
3 |
| -import caseapp.core.parser.Parser |
4 |
| -import caseapp.{ExtraName, HelpMessage} |
| 3 | +import caseapp.{ExtraName, Group, HelpMessage, Parser} |
| 4 | +import caseapp.core.parser.{Argument, NilParser, StandardArgument} |
| 5 | +import caseapp.core.{Arg, Error} |
| 6 | +import caseapp.core.parser.RecursiveConsParser |
| 7 | +import caseapp.core.util.Formatter |
| 8 | + |
| 9 | +import shapeless.{HNil, :: => :*:} |
5 | 10 |
|
6 | 11 | abstract class WithFullHelpCompanion {
|
7 | 12 |
|
8 |
| - implicit def parser[T, D](implicit underlying: Parser.Aux[T, D]): Parser[WithFullHelp[T]] = |
9 |
| - Parser.nil |
10 |
| - .addAll[WithHelp[T]].apply |
11 |
| - .add[Boolean]( |
12 |
| - "helpFull", |
13 |
| - default = Some(false), |
14 |
| - extraNames = Seq(ExtraName("fullHelp")), |
15 |
| - helpMessage = Some(HelpMessage("Print help message, including hidden options, and exit")) |
16 |
| - ) |
17 |
| - .as[WithFullHelp[T]] |
| 13 | + implicit def parser[T, D](implicit |
| 14 | + underlying: Parser.Aux[T, D] |
| 15 | + ): Parser.Aux[ |
| 16 | + WithFullHelp[T], |
| 17 | + (Option[Boolean] :*: Option[Boolean] :*: D :*: HNil) :*: Option[Boolean] :*: HNil |
| 18 | + ] = { |
| 19 | + |
| 20 | + val baseHelpArgument = StandardArgument[Boolean]( |
| 21 | + Arg("helpFull") |
| 22 | + .withExtraNames(Seq( |
| 23 | + ExtraName("fullHelp"), |
| 24 | + ExtraName("-help-full"), |
| 25 | + ExtraName("-full-help") |
| 26 | + )) |
| 27 | + .withGroup(Some(Group("Help"))) |
| 28 | + .withOrigin(Some("WithFullHelp")) |
| 29 | + .withHelpMessage(Some( |
| 30 | + HelpMessage("Print help message, including hidden options, and exit") |
| 31 | + )) |
| 32 | + .withIsFlag(true) |
| 33 | + ).withDefault(() => Some(false)) |
| 34 | + |
| 35 | + // accept "-help" too (single dash) |
| 36 | + val helpArgument: Argument[Boolean] = |
| 37 | + new Argument[Boolean] { |
| 38 | + def arg = baseHelpArgument.arg |
| 39 | + def withDefaultOrigin(origin: String) = |
| 40 | + this |
| 41 | + def init = baseHelpArgument.init |
| 42 | + def step( |
| 43 | + args: List[String], |
| 44 | + index: Int, |
| 45 | + d: Option[Boolean], |
| 46 | + nameFormatter: Formatter[ExtraName] |
| 47 | + ): Either[(Error, List[String]), Option[(Option[Boolean], List[String])]] = |
| 48 | + args match { |
| 49 | + case ("-help-full" | "-full-help") :: rem => Right(Some((Some(true), rem))) |
| 50 | + case _ => baseHelpArgument.step(args, index, d, nameFormatter) |
| 51 | + } |
| 52 | + def get(d: Option[Boolean], nameFormatter: Formatter[ExtraName]) = |
| 53 | + baseHelpArgument.get(d, nameFormatter) |
| 54 | + } |
| 55 | + |
| 56 | + val withHelpParser = WithHelp.parser[T, D](underlying) |
| 57 | + |
| 58 | + val p = RecursiveConsParser(withHelpParser, helpArgument :: NilParser) |
| 59 | + |
| 60 | + p.to[WithFullHelp[T]] |
| 61 | + } |
18 | 62 |
|
19 | 63 | }
|
0 commit comments