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,49 @@
|
||||
package cli
|
||||
|
||||
// -------------- argStringArray ----------------
|
||||
type argStringArray struct {
|
||||
base argBase
|
||||
targetVar *[]string
|
||||
}
|
||||
|
||||
func (arg *argStringArray) getBase() *argBase {
|
||||
return &arg.base
|
||||
}
|
||||
|
||||
func (spec *argStringArray) parse(cli *CliParser, specIndex int, args []string, argIndex int) (consumedArgs int, err error) {
|
||||
consumedArgs = 1
|
||||
if spec.targetVar != nil {
|
||||
if argIndex < len(args) {
|
||||
remainingSpecs := len(cli.argSpecs) - specIndex - 1
|
||||
availableArgs := len(args) - remainingSpecs
|
||||
if availableArgs > 0 {
|
||||
*spec.targetVar = args[argIndex : argIndex+availableArgs]
|
||||
consumedArgs = availableArgs
|
||||
} else {
|
||||
err = errTooFewArguments(len(cli.argSpecs))
|
||||
}
|
||||
} else if spec.base.required {
|
||||
err = errMissingRequiredArg(spec.base.name)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (cli *CliParser) AddStringArrayArg(name string, required bool, targetVar *[]string, description string) {
|
||||
// todo: check if arg already exists
|
||||
if len(cli.argSpecs) > 0 {
|
||||
lastArg := cli.argSpecs[len(cli.argSpecs)-1].getBase()
|
||||
if lastArg.repeat {
|
||||
panic(errRepeatArgAlreadyDefined(lastArg.name))
|
||||
}
|
||||
}
|
||||
cli.argSpecs = append(cli.argSpecs, &argStringArray{
|
||||
base: argBase{
|
||||
name: name,
|
||||
description: description,
|
||||
required: required,
|
||||
repeat: true,
|
||||
},
|
||||
targetVar: targetVar,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user