From 6b7685965c2b914e31ab71fb781aaa8da8477a0d Mon Sep 17 00:00:00 2001 From: hb0730 <1278032416@qq.com> Date: Sat, 30 Jul 2022 17:15:13 +0800 Subject: [PATCH 1/2] fix: parse bool type --- util.go | 45 +++++++++++++++++++++++++-------------------- util_test.go | 22 ++++++++++++++++++++++ 2 files changed, 47 insertions(+), 20 deletions(-) diff --git a/util.go b/util.go index 129291f..06df402 100644 --- a/util.go +++ b/util.go @@ -238,27 +238,32 @@ func strictFormatArgs(args []string) (fmtArgs []string) { } for _, arg := range args { - // eg: --a ---name - if strings.Index(arg, "--") == 0 { - farg := strings.TrimLeft(arg, "-") - if rl := len(farg); rl == 1 { // fix: "--a" -> "-a" - arg = "-" + farg - } else if rl > 1 { // fix: "---name" -> "--name" - arg = "--" + farg - } - // TODO No change remain OR remove like "--" "---" - // maybe ... - - } else if strings.IndexByte(arg, '-') == 0 { - ln := len(arg) - // fix: "-abc" -> "-a -b -c" - if ln > 2 { - chars := strings.Split(strings.Trim(arg, "-"), "") - - for _, s := range chars { - fmtArgs = append(fmtArgs, "-"+s) + // if contains `=` append self + // TODO mode: + // `--test=x`, `-t=x` , `-test=x` + if !strings.Contains(arg, "=") { + // eg: --a ---name + if strings.Index(arg, "--") == 0 { + farg := strings.TrimLeft(arg, "-") + if rl := len(farg); rl == 1 { // fix: "--a" -> "-a" + arg = "-" + farg + } else if rl > 1 { // fix: "---name" -> "--name" + arg = "--" + farg + } + // TODO No change remain OR remove like "--" "---" + // maybe ... + + } else if strings.IndexByte(arg, '-') == 0 { + ln := len(arg) + // fix: "-abc" -> "-a -b -c" + if ln > 2 { + chars := strings.Split(strings.Trim(arg, "-"), "") + + for _, s := range chars { + fmtArgs = append(fmtArgs, "-"+s) + } + continue } - continue } } diff --git a/util_test.go b/util_test.go index 93773cc..6545c07 100644 --- a/util_test.go +++ b/util_test.go @@ -30,3 +30,25 @@ func TestHelpVars(t *testing.T) { // invalid input is.Eq("hello {key0}", vs.ReplaceVars("hello {key0}")) } + +func Test_strictFormatArgs(t *testing.T) { + str1 := "" + t1 := false + t2 := false + t3 := false + //t4 := false + is := assert.New(t) + cmd := gcli.NewCommand("init", "test bool pare", func(c *gcli.Command) { + c.StrOpt(&str1, "name", "n", "", "test string parse") + c.BoolOpt(&t1, "test1", "t", false, "test bool arse") + c.BoolOpt(&t2, "test2", "s", false, "test bool arse") + c.BoolOpt(&t3, "test3", "c", true, "test bool arse") + //c.BoolOpt(&t4, "test4", "d", false, "test bool arse") + }) + err := cmd.Run([]string{"-n", "ccc", "-test1=true", "-s", "--test3=false"}) + is.NoErr(err) + is.Eq("ccc", str1) + is.Eq(true, t1) + is.Eq(true, t2) + is.Eq(false, t3) +} From f2d4e59d41ead2eb4ea494b2c45ca643e21436f6 Mon Sep 17 00:00:00 2001 From: hb0730 <1278032416@qq.com> Date: Sat, 30 Jul 2022 17:20:10 +0800 Subject: [PATCH 2/2] fix: parse bool type --- util.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/util.go b/util.go index 06df402..33d7f6d 100644 --- a/util.go +++ b/util.go @@ -238,9 +238,9 @@ func strictFormatArgs(args []string) (fmtArgs []string) { } for _, arg := range args { - // if contains `=` append self + // if contains '=' append self // TODO mode: - // `--test=x`, `-t=x` , `-test=x` + // '--test=x', '-t=x' , '-test=x', '-test' if !strings.Contains(arg, "=") { // eg: --a ---name if strings.Index(arg, "--") == 0 {