add final checks on option values

This commit is contained in:
2026-03-05 22:30:07 +01:00
parent b5f8d9eaab
commit 95fae40d5f
15 changed files with 219 additions and 37 deletions
+19 -2
View File
@@ -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
}