3 Commits

Author SHA1 Message Date
ce84a374cc Reverse order of flag searching
Flags specified later can override flags specified earlier.
2024-12-30 23:19:54 -05:00
6842224b37 Fix godoc link in README 2024-12-30 10:09:13 -05:00
541886e9ad Fix slice out of bounds
Closes #1
2024-12-12 01:17:52 -05:00
2 changed files with 6 additions and 3 deletions

View File

@@ -1,5 +1,5 @@
# go-cli
[![Go Reference](https://pkg.go.dev/badge/git.tebibyte.media/sashakoshka/cli.svg)](https://pkg.go.dev/git.tebibyte.media/sashakoshka/cli)
[![Go Reference](https://pkg.go.dev/badge/git.tebibyte.media/sashakoshka/go-cli.svg)](https://pkg.go.dev/git.tebibyte.media/sashakoshka/go-cli)
CLI module for Go programs.

7
cli.go
View File

@@ -81,6 +81,7 @@ func (this *Cli) Parse (args []string) (*Cli, error) {
if len(args) > 0 { args = args[1:] }
next := func () string {
if len(args) < 1 { return "" }
args = args[1:]
if len(args) > 0 {
return args[0]
@@ -168,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
@@ -178,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