New option type 'multi'. Its value is the number of occurences of an option
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
package cli
|
||||
|
||||
import "strconv"
|
||||
|
||||
const (
|
||||
multiTypeName = "num"
|
||||
)
|
||||
|
||||
type cliOptionMulti struct {
|
||||
cliOptionBase
|
||||
defaultValue int
|
||||
targetVar *int
|
||||
}
|
||||
|
||||
func (opt *cliOptionMulti) init() {
|
||||
if opt.targetVar != nil {
|
||||
*opt.targetVar = opt.defaultValue
|
||||
}
|
||||
}
|
||||
|
||||
func (opt *cliOptionMulti) getTargetVar() (any, string) {
|
||||
var value int
|
||||
if opt.targetVar != nil {
|
||||
value = *opt.targetVar
|
||||
}
|
||||
return value, multiTypeName
|
||||
}
|
||||
|
||||
func (opt *cliOptionMulti) requiresValue() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (opt *cliOptionMulti) getDefaultValue() string {
|
||||
return strconv.Itoa(opt.defaultValue)
|
||||
}
|
||||
|
||||
func (opt *cliOptionMulti) getTemplate() string {
|
||||
return opt.makeOptTemplate(false, multiTypeName)
|
||||
}
|
||||
|
||||
func (opt *cliOptionMulti) parse(parser cliParser, argIndex int, valuePtr *string) (skipNextArg bool, err error) {
|
||||
if opt.targetVar != nil {
|
||||
*opt.targetVar++
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (cli *CliParser) AddMultiOpt(name, short string, targetVar *int, defaultValue int, description string, aliases ...string) OptReference {
|
||||
if cli.optionExists(name, short, aliases) {
|
||||
panic(errOptionAlreadyDefined(name))
|
||||
}
|
||||
opt := &cliOptionMulti{
|
||||
cliOptionBase: cliOptionBase{
|
||||
name: name,
|
||||
shortAlias: short,
|
||||
aliases: aliases,
|
||||
description: description,
|
||||
},
|
||||
targetVar: targetVar,
|
||||
defaultValue: defaultValue,
|
||||
}
|
||||
cli.options = append(cli.options, opt)
|
||||
return opt
|
||||
}
|
||||
Reference in New Issue
Block a user