Option types: bool int, int-array, string, string-array, string-map, file, dir
137 lines
3.3 KiB
Go
137 lines
3.3 KiB
Go
package cli
|
|
|
|
const (
|
|
stringTypeName = "string"
|
|
)
|
|
|
|
type cliOptionString struct {
|
|
cliOptionBase
|
|
defaultValue string
|
|
targetVar *string
|
|
}
|
|
|
|
func (opt *cliOptionString) init() {
|
|
if opt.targetVar != nil {
|
|
*opt.targetVar = opt.defaultValue
|
|
}
|
|
}
|
|
|
|
func (opt *cliOptionString) getTargetVar() (any, string) {
|
|
var value string
|
|
if opt.targetVar != nil {
|
|
value = *opt.targetVar
|
|
}
|
|
return value, stringTypeName
|
|
}
|
|
|
|
func (opt *cliOptionString) requiresValue() bool {
|
|
return opt.targetVar != nil
|
|
}
|
|
|
|
func (opt *cliOptionString) getDefaultValue() string {
|
|
return opt.defaultValue
|
|
}
|
|
|
|
func (opt *cliOptionString) getTemplate() string {
|
|
return opt.makeOptTemplate(false, stringTypeName)
|
|
}
|
|
|
|
func (opt *cliOptionString) 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, "string")
|
|
}
|
|
} else {
|
|
*(opt.targetVar) = value
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func (cli *CliParser) AddStringOpt(name, short string, targetVar *string, defaultValue string, description string, aliases ...string) OptReference {
|
|
if cli.optionExists(name, short, aliases) {
|
|
panic(errOptionAlreadyDefined(name))
|
|
}
|
|
opt := &cliOptionString{
|
|
cliOptionBase: cliOptionBase{
|
|
name: name,
|
|
shortAlias: short,
|
|
aliases: aliases,
|
|
description: description,
|
|
},
|
|
targetVar: targetVar,
|
|
defaultValue: defaultValue,
|
|
}
|
|
cli.options = append(cli.options, opt)
|
|
return opt
|
|
}
|
|
|
|
//---------------------- cliOptionDir ----------------------
|
|
|
|
type cliOptionDir struct {
|
|
cliOptionString
|
|
}
|
|
|
|
func (opt *cliOptionDir) getTemplate() string {
|
|
return opt.cliOptionBase.makeOptTemplate(false, "dir")
|
|
}
|
|
|
|
func (cli *CliParser) AddDirOpt(name, short string, targetVar *string, defaultValue string, description string, aliases ...string) OptReference {
|
|
if cli.optionExists(name, short, aliases) {
|
|
panic(errOptionAlreadyDefined(name))
|
|
}
|
|
opt := &cliOptionDir{
|
|
cliOptionString: cliOptionString{
|
|
cliOptionBase: cliOptionBase{
|
|
name: name,
|
|
shortAlias: short,
|
|
aliases: aliases,
|
|
description: description,
|
|
},
|
|
targetVar: targetVar,
|
|
defaultValue: defaultValue,
|
|
},
|
|
}
|
|
cli.options = append(cli.options, opt)
|
|
return opt
|
|
}
|
|
|
|
//---------------------- cliOptionFile ----------------------
|
|
|
|
type cliOptionFile struct {
|
|
cliOptionString
|
|
}
|
|
|
|
func (opt *cliOptionFile) getTemplate() string {
|
|
return opt.cliOptionBase.makeOptTemplate(false, "file")
|
|
}
|
|
|
|
func (cli *CliParser) AddFileOpt(name, short string, targetVar *string, defaultValue string, description string, aliases ...string) OptReference {
|
|
if cli.optionExists(name, short, aliases) {
|
|
panic(errOptionAlreadyDefined(name))
|
|
}
|
|
opt := &cliOptionFile{
|
|
cliOptionString: cliOptionString{
|
|
cliOptionBase: cliOptionBase{
|
|
name: name,
|
|
shortAlias: short,
|
|
aliases: aliases,
|
|
description: description,
|
|
},
|
|
targetVar: targetVar,
|
|
defaultValue: defaultValue,
|
|
},
|
|
}
|
|
cli.options = append(cli.options, opt)
|
|
return opt
|
|
}
|