Skip to content

Commit

Permalink
优化一个方法名儿
Browse files Browse the repository at this point in the history
  • Loading branch information
reatang committed Nov 21, 2024
1 parent 1f43fee commit f5885da
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ import (

// go run main.go -a str1 -b -int=123 1111111 222222 333333
// go run main.go -a=str1 -int 123 -b -- 1111111 222222 333333
// go run main.go -a=str1 -int 123 -b 1111111 222222 333333

func main() {
looseflag.CommandLine.SetBoolArgs("b")
err := looseflag.Parse()
if err != nil {
panic(err)
Expand Down
1 change: 1 addition & 0 deletions example/base/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
// go run main.go -a str1 -int=123 -b -- 1111111 222222 333333

func main() {
looseflag.CommandLine.SetBoolArgs("b")
err := looseflag.Parse()
if err != nil {
panic(err)
Expand Down
12 changes: 6 additions & 6 deletions flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type FlagSet struct {
beforeArgs []string
afterArgs []string

isBoolValue []string
isBoolArgs []string

options map[string]any
}
Expand All @@ -40,13 +40,13 @@ func NewFlagSet(name string, errorHandling ErrorHandling) *FlagSet {
beforeArgs: make([]string, 0),
afterArgs: make([]string, 0),

isBoolValue: make([]string, 0),
options: make(map[string]any),
isBoolArgs: make([]string, 0),
options: make(map[string]any),
}
}

func (f *FlagSet) SetBoolVals(b ...string) {
f.isBoolValue = append(f.isBoolValue, b...)
func (f *FlagSet) SetBoolArgs(b ...string) {
f.isBoolArgs = append(f.isBoolArgs, b...)
}

func (f *FlagSet) Parse(args []string) error {
Expand Down Expand Up @@ -77,7 +77,7 @@ func (f *FlagSet) Parse(args []string) error {
} else {
f.options[name[:eqIndex]] = v
}
} else if sliceContains(f.isBoolValue, name) {
} else if sliceContains(f.isBoolArgs, name) {
f.options[name] = true
} else {
if len(args) == 0 {
Expand Down
2 changes: 1 addition & 1 deletion flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func TestFlagSet_Parse(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f := NewFlagSet(tt.name, 0)
f.SetBoolVals(tt.isBool...)
f.SetBoolArgs(tt.isBool...)
if err := f.Parse(tt.args.args); (err != nil) != tt.wantErr {
t.Errorf("Parse() error = %v, wantErr %v", err, tt.wantErr)
}
Expand Down

0 comments on commit f5885da

Please sign in to comment.