-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArgHintProvider.scala
47 lines (36 loc) · 1.23 KB
/
ArgHintProvider.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package decline_derive
import quoted.*
private[decline_derive] case class ArgHintProvider(e: Expr[Seq[ArgHint]]):
inline def getHint[T: Type](
inline f: PartialFunction[ArgHint, T]
)(using Quotes): Expr[Option[T]] =
'{ $e.collectFirst(f) }
inline def getHintOption[T: Type](
inline f: PartialFunction[ArgHint, Option[T]]
)(using Quotes): Expr[Option[T]] =
'{ $e.collectFirst(f).flatten }
def name(using Quotes) =
getHint:
case ArgHint.Name(value) => value
def flag(using Quotes) =
getHint:
case ArgHint.FlagDefault(value) => value
def short(using Quotes) =
getHint:
case ArgHint.Short(value) => value
def help(using Quotes) =
getHint:
case ArgHint.Help(value) => value
def envName(using Quotes) =
getHint:
case ArgHint.Env(name, _) => name
def envHelp(using Quotes) =
getHintOption:
case ArgHint.Env(_, help: String) => Some(help)
case ArgHint.Env(_, help: Option[String]) => help
def isArgument(using Quotes) =
getHint:
case ArgHint.Positional(metavar: String) => Some(metavar)
case ArgHint.Positional(None) => None
case ArgHint.Positional(Some(metavar)) => Some(metavar)
end ArgHintProvider