Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: parse bool type #60

Merged
merged 2 commits into from
Jul 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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', '-test'
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)
}