Reverse order of flag searching

Flags specified later can override flags specified earlier.
This commit is contained in:
Sasha Koshka 2024-12-30 23:19:54 -05:00
parent 6842224b37
commit ce84a374cc

6
cli.go
View File

@ -169,7 +169,8 @@ func (this *Cli) ParseOrExit (args []string) *Cli {
// was found, it returns true. If it was not found, it returns nil, false.
func (this *Cli) ShortFlag (short rune) (*Flag, bool) {
if short == 0 { return nil, false }
for _, flag := range this.Flags {
for index := len(this.Flags) - 1; index >= 0; index -- {
flag := this.Flags[index]
if flag.Short == short { return flag, true }
}
return nil, false
@ -179,7 +180,8 @@ func (this *Cli) ShortFlag (short rune) (*Flag, bool) {
// was found, it returns true. If it was not found, it returns nil, false.
func (this *Cli) LongFlag (long string) (*Flag, bool) {
if long == "" { return nil, false }
for _, flag := range this.Flags {
for index := len(this.Flags) - 1; index >= 0; index -- {
flag := this.Flags[index]
if flag.Long == long { return flag, true }
}
return nil, false