package cli import "strconv" const ( intTypeName = "num" ) type cliOptionInt struct { cliOptionBase defaultValue int targetVar *int } func (opt *cliOptionInt) init() { if opt.targetVar != nil { *opt.targetVar = opt.defaultValue } } func (opt *cliOptionInt) getTargetVar() (any, string) { var value int if opt.targetVar != nil { value = *opt.targetVar } return value, intTypeName } func (opt *cliOptionInt) requiresValue() bool { return opt.targetVar != nil } func (opt *cliOptionInt) getDefaultValue() string { return strconv.Itoa(opt.defaultValue) } func (opt *cliOptionInt) getTemplate() string { return opt.makeOptTemplate(false, intTypeName) } func (opt *cliOptionInt) parse(parser cliParser, argIndex int, valuePtr *string) (skipNextArg bool, err error) { var value string if value, skipNextArg, err = opt.fetchOptionValue(parser, argIndex, valuePtr); err == nil { var boxedValue any if boxedValue, err = opt.getSpecialValue(parser, value, opt.targetVar); err == nil { if opt.targetVar != nil { if boxedValue != nil { if val, ok := boxedValue.(string); ok { *opt.targetVar, err = strconv.Atoi(val) } else { err = errInvalidOptionValue(opt.name, boxedValue, "int") } } else { *opt.targetVar, err = strconv.Atoi(value) } } } } return } func (cli *CliParser) AddIntOpt(name, short string, targetVar *int, defaultValue int, description string, aliases ...string) OptReference { if cli.optionExists(name, short, aliases) { panic(errOptionAlreadyDefined(name)) } opt := &cliOptionInt{ cliOptionBase: cliOptionBase{ name: name, shortAlias: short, aliases: aliases, description: description, }, targetVar: targetVar, defaultValue: defaultValue, } cli.options = append(cli.options, opt) return opt }