1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-30 07:28:57 +01:00
gojekyll/commands/option_utils.go

63 lines
1.3 KiB
Go
Raw Permalink Normal View History

2017-07-19 22:06:43 +02:00
package commands
2017-07-01 20:55:50 +02:00
import (
2017-07-07 21:28:51 +02:00
"strconv"
2017-07-01 20:55:50 +02:00
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
2017-07-02 15:38:00 +02:00
func boolVar(name string, ptr **bool) kingpin.Action {
2017-07-01 20:55:50 +02:00
return func(pc *kingpin.ParseContext) error {
value := lookupKingpinValue(name, pc)
2017-07-07 21:28:51 +02:00
if value != nil {
arg := *value == "true"
*ptr = &arg
}
return nil
}
}
func intVar(name string, ptr **int) kingpin.Action {
return func(pc *kingpin.ParseContext) error {
value := lookupKingpinValue(name, pc)
if value != nil {
n, err := strconv.Atoi(*value)
if err != nil {
panic(err)
}
2017-07-08 16:29:24 +02:00
*ptr = &n
2017-07-01 20:55:50 +02:00
}
return nil
}
}
2017-07-02 15:38:00 +02:00
func stringVar(name string, ptr **string) kingpin.Action {
2017-07-01 20:55:50 +02:00
return func(pc *kingpin.ParseContext) error {
value := lookupKingpinValue(name, pc)
if value != nil {
2017-07-07 21:28:51 +02:00
arg := *value
*ptr = &arg
2017-07-01 20:55:50 +02:00
}
return nil
}
}
// From https://github.com/alecthomas/kingpin/issues/184
// Replace w/ kingpin.v3 API when available
func lookupKingpinValue(name string, pc *kingpin.ParseContext) *string {
for _, el := range pc.Elements {
switch el.Clause.(type) {
case *kingpin.ArgClause:
if (el.Clause).(*kingpin.ArgClause).Model().Name == name {
return el.Value
}
case *kingpin.FlagClause:
if (el.Clause).(*kingpin.FlagClause).Model().Name == name {
return el.Value
}
}
}
def := ""
return &def
}