first commit status: option and argument parsing, short aliases grouping, special values, hidden options
Option types: bool int, int-array, string, string-array, string-map, file, dir
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
stringArrayTypeName = "string-array"
|
||||
)
|
||||
|
||||
type cliOptionStringArray struct {
|
||||
cliOptionBase
|
||||
defaultValue []string
|
||||
targetVar *[]string
|
||||
}
|
||||
|
||||
func (opt *cliOptionStringArray) init() {
|
||||
opt.isArray = true
|
||||
if opt.targetVar != nil {
|
||||
*opt.targetVar = opt.defaultValue
|
||||
}
|
||||
}
|
||||
|
||||
func (opt *cliOptionStringArray) getTargetVar() (any, string) {
|
||||
var value []string
|
||||
if opt.targetVar != nil {
|
||||
value = *opt.targetVar
|
||||
}
|
||||
return value, stringArrayTypeName
|
||||
}
|
||||
|
||||
func (opt *cliOptionStringArray) requiresValue() bool {
|
||||
return opt.targetVar != nil
|
||||
}
|
||||
|
||||
func (opt *cliOptionStringArray) getDefaultValue() string {
|
||||
return strings.Join(opt.defaultValue, ",")
|
||||
}
|
||||
|
||||
func (opt *cliOptionStringArray) getTemplate() string {
|
||||
return opt.makeOptTemplate(true, "string")
|
||||
}
|
||||
|
||||
// parse retrieves the option value from the parser and updates the target variable.
|
||||
// It handles comma-separated values and special values if configured.
|
||||
func (opt *cliOptionStringArray) 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 = val
|
||||
} else {
|
||||
err = errInvalidOptionValue(opt.name, boxedValue, "array of string")
|
||||
}
|
||||
} else {
|
||||
*opt.targetVar = strings.Split(value, ",")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// AddStringArrayOpt adds a new string array option to the CLI parser.
|
||||
// It takes the name, short alias, target variable, default value, description, and optional aliases.
|
||||
// It returns a reference to the created option.
|
||||
func (cli *CliParser) AddStringArrayOpt(name, short string, targetVar *[]string, defaultValue []string, description string, aliases ...string) OptReference {
|
||||
aliases = cli.checkAlreadyUsedNames(name, short, aliases)
|
||||
opt := &cliOptionStringArray{
|
||||
cliOptionBase: cliOptionBase{
|
||||
name: name,
|
||||
shortAlias: short,
|
||||
aliases: aliases,
|
||||
description: description,
|
||||
isArray: true,
|
||||
},
|
||||
targetVar: targetVar,
|
||||
defaultValue: defaultValue,
|
||||
}
|
||||
cli.options = append(cli.options, opt)
|
||||
return opt
|
||||
}
|
||||
Reference in New Issue
Block a user