new /v2 subdirectory
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
const (
|
||||
boolTypeName = "bool"
|
||||
)
|
||||
|
||||
type cliOptionBool struct {
|
||||
cliOptionBase
|
||||
targetVar *bool
|
||||
}
|
||||
|
||||
func (opt *cliOptionBool) init() {
|
||||
if opt.targetVar != nil {
|
||||
*opt.targetVar = false
|
||||
}
|
||||
}
|
||||
|
||||
func (opt *cliOptionBool) getTargetVar() (any, string) {
|
||||
var value bool
|
||||
if opt.targetVar != nil {
|
||||
value = *opt.targetVar
|
||||
}
|
||||
return value, boolTypeName
|
||||
}
|
||||
|
||||
func (opt *cliOptionBool) isSet() bool {
|
||||
if opt.targetVar != nil {
|
||||
return *(opt.targetVar)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (opt *cliOptionBool) requiresValue() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (opt *cliOptionBool) getTemplate() (templ string) {
|
||||
if opt.shortAlias != "" {
|
||||
templ = fmt.Sprintf(`-%s, --%s`, opt.shortAlias, opt.name)
|
||||
} else {
|
||||
templ = fmt.Sprintf(`--%s`, opt.name)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (opt *cliOptionBool) parse(parser cliParser, argIndex int, valuePtr *string) (skipNextArg bool, optValue string, err error) {
|
||||
var boxedValue any
|
||||
optValue = "true"
|
||||
|
||||
if boxedValue, err = opt.getSpecialValue(parser, optValue, opt.targetVar); err == nil {
|
||||
if opt.targetVar != nil {
|
||||
if boxedValue != nil {
|
||||
if val, ok := boxedValue.(bool); ok {
|
||||
*(opt.targetVar) = val
|
||||
} else {
|
||||
err = errInvalidOptionValue(opt.name, boxedValue, "bool")
|
||||
}
|
||||
} else {
|
||||
*(opt.targetVar) = true
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (opt *cliOptionBool) finalCheck() (err error) {
|
||||
currentValue, _ := opt.getTargetVar()
|
||||
return opt.getBase().checkValue(currentValue)
|
||||
}
|
||||
|
||||
func (cli *CliParser) AddBoolOpt(name, short string, targetVar *bool, description string, aliases ...string) OptReference {
|
||||
if cli.optionExists(name, short, aliases) {
|
||||
panic(errOptionAlreadyDefined(name))
|
||||
}
|
||||
opt := &cliOptionBool{
|
||||
cliOptionBase: cliOptionBase{
|
||||
name: name,
|
||||
shortAlias: short,
|
||||
aliases: aliases,
|
||||
description: description,
|
||||
},
|
||||
targetVar: targetVar,
|
||||
}
|
||||
cli.options = append(cli.options, opt)
|
||||
return opt
|
||||
}
|
||||
Reference in New Issue
Block a user