From ce84a374ccaa4059d9c74655e9ce5729b5d18853 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Mon, 30 Dec 2024 23:19:54 -0500 Subject: [PATCH] Reverse order of flag searching Flags specified later can override flags specified earlier. --- cli.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cli.go b/cli.go index 584205d..5fdc40a 100644 --- a/cli.go +++ b/cli.go @@ -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