cli.go: fixed an out-of-bound error on cli.argSpecs array
This commit is contained in:
parent
ac8d5fa3a9
commit
b5f8d9eaab
23
cli.go
23
cli.go
@ -163,6 +163,7 @@ func (cli *CliParser) Parse() (err error) {
|
|||||||
|
|
||||||
cli.addHelpAndVersion()
|
cli.addHelpAndVersion()
|
||||||
|
|
||||||
|
// first parse options and collect argument in the args array
|
||||||
skipNext := false
|
skipNext := false
|
||||||
for i, arg = range cli.cliArgs[1:] {
|
for i, arg = range cli.cliArgs[1:] {
|
||||||
if skipNext {
|
if skipNext {
|
||||||
@ -182,12 +183,16 @@ func (cli *CliParser) Parse() (err error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// then parse collected arguments
|
||||||
if err == nil {
|
if err == nil {
|
||||||
var argSpec argSpec
|
var n int
|
||||||
var n, specIndex int
|
|
||||||
i = 0
|
i = 0
|
||||||
|
|
||||||
for specIndex, argSpec = range cli.argSpecs {
|
// acquire arguments as required by the argSpecs
|
||||||
|
specIndex := -1
|
||||||
|
for index, argSpec := range cli.argSpecs {
|
||||||
|
specIndex = index
|
||||||
if n, err = argSpec.parse(cli, specIndex, args, i); err != nil {
|
if n, err = argSpec.parse(cli, specIndex, args, i); err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -196,16 +201,22 @@ func (cli *CliParser) Parse() (err error) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check if there are remaining arg-specs that require a value
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if i < len(args) {
|
if i < len(args) {
|
||||||
err = fmt.Errorf("too many arguments: %d allowed", i)
|
err = fmt.Errorf("too many arguments: %d allowed", i)
|
||||||
} else {
|
} else {
|
||||||
specIndex++
|
specIndex++
|
||||||
for _, spec := range cli.argSpecs[specIndex:] {
|
if specIndex < len(cli.argSpecs) {
|
||||||
if !spec.getBase().required {
|
// skip all non required args
|
||||||
specIndex++
|
for _, spec := range cli.argSpecs[specIndex:] {
|
||||||
|
if !spec.getBase().required {
|
||||||
|
specIndex++
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// return error if there are remaining arg-specs that require a value
|
||||||
if specIndex < len(cli.argSpecs) {
|
if specIndex < len(cli.argSpecs) {
|
||||||
err = errTooFewArguments(len(cli.argSpecs))
|
err = errTooFewArguments(len(cli.argSpecs))
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user