Skip to content

Commit

Permalink
fix: parse bool type
Browse files Browse the repository at this point in the history
  • Loading branch information
hb0730 committed Jul 30, 2022
1 parent c1d2ceb commit 6b76859
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 20 deletions.
45 changes: 25 additions & 20 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
22 changes: 22 additions & 0 deletions util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 6b76859

Please sign in to comment.