add final checks on option values
This commit is contained in:
+19
-2
@@ -15,6 +15,7 @@ type cliOptionBase struct {
|
||||
hidden bool
|
||||
alreadySeen bool
|
||||
specialValues map[string]SpecialValueFunc
|
||||
finalCheckFunc FinalCheckFunc
|
||||
incompatibleWith []string
|
||||
}
|
||||
|
||||
@@ -41,6 +42,10 @@ func (opt *cliOptionBase) SetHidden(hidden bool) {
|
||||
opt.hidden = hidden
|
||||
}
|
||||
|
||||
func (opt *cliOptionBase) OnFinalCheck(checkFunc FinalCheckFunc) {
|
||||
opt.finalCheckFunc = checkFunc
|
||||
}
|
||||
|
||||
func (opt *cliOptionBase) AddSpecialValue(cliValue string, specialFunc SpecialValueFunc) {
|
||||
if opt.specialValues == nil {
|
||||
opt.specialValues = make(map[string]SpecialValueFunc)
|
||||
@@ -67,8 +72,9 @@ func (opt *cliOptionBase) addIncompatibleOption(names ...string) {
|
||||
opt.incompatibleWith = append(opt.incompatibleWith, names...)
|
||||
}
|
||||
|
||||
func (opt *cliOptionBase) parse(parser cliParser, valuePtr *string) (err error) {
|
||||
return fmt.Errorf("unhandled option %q", opt.name)
|
||||
func (opt *cliOptionBase) parse(parser cliParser, argIndex int, valuePtr *string) (skipNextArg bool, value string, err error) {
|
||||
err = fmt.Errorf("unhandled option %q", opt.name)
|
||||
return
|
||||
}
|
||||
|
||||
func (opt *cliOptionBase) getDefaultValue() string {
|
||||
@@ -146,3 +152,14 @@ func (opt *cliOptionBase) fetchOptionValue(parser cliParser, argIndex int, value
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (opt *cliOptionBase) finalCheck(cliValue string) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (opt *cliOptionBase) checkValue(cliValue string, value any) (err error) {
|
||||
if opt.finalCheckFunc != nil {
|
||||
err = opt.finalCheckFunc(cliValue, value)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user