Option types: bool int, int-array, string, string-array, string-map, file, dir
37 lines
859 B
Go
37 lines
859 B
Go
package cli
|
|
|
|
// -------------- argString ----------------
|
|
type argString struct {
|
|
base argBase
|
|
targetVar *string
|
|
}
|
|
|
|
func (arg *argString) getBase() *argBase {
|
|
return &arg.base
|
|
}
|
|
|
|
func (spec *argString) parse(cli *CliParser, specIndex int, args []string, argIndex int) (consumedArgs int, err error) {
|
|
consumedArgs = 1
|
|
if spec.targetVar != nil {
|
|
if argIndex < len(args) {
|
|
*spec.targetVar = args[argIndex]
|
|
} else if spec.base.required {
|
|
err = errMissingRequiredArg(spec.base.name)
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func (cli *CliParser) AddStringArg(name string, required bool, targetVar *string, description string) {
|
|
// todo: check if arg already exists
|
|
cli.argSpecs = append(cli.argSpecs, &argString{
|
|
base: argBase{
|
|
name: name,
|
|
description: description,
|
|
required: required,
|
|
repeat: false,
|
|
},
|
|
targetVar: targetVar,
|
|
})
|
|
}
|