Skip to content

Commit

Permalink
removing AddCmd/AddFlg since them took obsess behavior to Cmd/Flg
Browse files Browse the repository at this point in the history
  • Loading branch information
hedzr committed Sep 13, 2024
1 parent 0081fbf commit 6f3c68f
Show file tree
Hide file tree
Showing 9 changed files with 1,167 additions and 1,104 deletions.
95 changes: 83 additions & 12 deletions builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,81 @@ func TestFfb_NewFlagBuilder(t *testing.T) {
testNewFlagBuilder(t)
}

func TestCcb_NewCommandBuilderPanics(t *testing.T) {
deferTest := func(t *testing.T, cb func(bb *ccb)) {
defer func() {
if r := recover(); r == nil {
t.Logf("test for %q is ok.", t.Name())
return
}
t.Fatalf("test for %q is bad: expecting a panic but it isn't threw", t.Name())
}()

b := buildable(nil)
bb := newCommandBuilderShort(b, "help", "h", "info")
cb(bb)
}

t.Run("inCmd == true and Cmd panics", func(t *testing.T) {
deferTest(t, func(bb *ccb) {
bb.AddCmd(func(b cli.CommandBuilder) {
b.OnMatched(nil)

cb := b.Cmd("dash", "d")

cb.UseShell("/bin/dash")
})
})
})

t.Run("inCmd == true and AddCmd panics", func(t *testing.T) {
deferTest(t, func(bb *ccb) {
bb.AddCmd(func(b cli.CommandBuilder) {
b.OnMatched(nil)

b.AddCmd(func(b cli.CommandBuilder) {
b.OnMatched(nil)
b.UseShell("/bin/dash")
})
})
})
})

t.Run("Cmd panics", func(t *testing.T) {
deferTest(t, func(bb *ccb) {
cb := bb.Cmd("dash", "d")
cb.UseShell("/bin/dash")
})
})
t.Run("Flg panics", func(t *testing.T) {
deferTest(t, func(bb *ccb) {
fb := bb.Flg("cool", "c")
fb.OnMatched(nil)
})
})
t.Run("AddCmd panics", func(t *testing.T) {
deferTest(t, func(bb *ccb) {
bb.AddCmd(func(b cli.CommandBuilder) {
b.OnMatched(nil)
})
})
})
t.Run("AddFlg panics", func(t *testing.T) {
deferTest(t, func(bb *ccb) {
bb.AddFlg(func(b cli.FlagBuilder) {
b.OnMatched(nil)
})
})
})
}

func TestCcb_NewCommandBuilder(t *testing.T) {
testNewCommandBuilder(t)
}

func testNewCommandBuilder(t *testing.T) {
b := buildable(nil)
bb := newCommandBuilderShort(b, "help", "h", "info")
bb := newCommandBuilderShort(b, "help", "h", "info", "tip", "whatsthis")

bb.Titles("verbose", "v", "verbose-mode", "non-quiet-mode")
bb.ExtraShorts("V")
Expand All @@ -49,31 +117,34 @@ func testNewCommandBuilder(t *testing.T) {
bb.InvokeShell("")
bb.UseShell("/bin/bash")

cb := bb.NewCommandBuilder("command", "c", "cc", "cmd")
cb.UseShell("/bin/zsh")

fb := bb.NewFlagBuilder("flag", "f", "ff", "flg")
fb.OnMatched(nil)

bb.Build()

bb.AddCmd(func(b cli.CommandBuilder) {
b.OnMatched(nil)
})
bb.AddFlg(func(b cli.FlagBuilder) {
b.OnMatched(nil)
})

cb = bb.Cmd("dash", "d")
bb.Build()

cb := bb.Cmd("dash", "d")
cb.UseShell("/bin/dash")
cb.Build()

fb = bb.Flg("cool", "c")
fb := bb.Flg("cool", "c")
fb.OnMatched(nil)
fb.Build()

// cb := bb.NewCommandBuilder("command", "c", "cc", "cmd")
// cb.UseShell("/bin/zsh")
//
// fb := bb.NewFlagBuilder("flag", "f", "ff", "flg")
// fb.OnMatched(nil)

}

func testNewFlagBuilder(t *testing.T) {
b := buildable(nil)
bb := newFlagBuilderShort(b, "verbose", "v", "verbose-mode")
bb := newFlagBuilderShort(b, "verbose", "v", "verbose-mode", "test-mode", "debug-mode")

app := buildable(nil)
bb.SetApp(app)
Expand Down
153 changes: 81 additions & 72 deletions builder/commandbuilders.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package builder

import (
"sync/atomic"

"github.com/hedzr/cmdr/v2/cli"
)

Expand All @@ -29,7 +31,7 @@ func newCommandBuilderFrom(from *cli.Command, b buildable, longTitle string, tit
s := &ccb{
b, from,
new(cli.Command),
false, false,
0, 0,
}
s.Long, s.Short, s.Aliases = theTitles(longTitle, titles...)
return s
Expand All @@ -39,21 +41,94 @@ type ccb struct {
buildable
parent *cli.Command
*cli.Command
inCmd bool
inFlg bool
inCmd int32
inFlg int32
}

func (s *ccb) Build() {
if a, ok := s.buildable.(adder); ok {
a.addCommand(s.Command)
}
if s.parent != nil {
s.parent.AddSubCommand(s.Command)
// if s.parent != nil {
// s.parent.AddSubCommand(s.Command)
// // if a, ok := s.b.(adder); ok {
// // a.addCommand(s.Command)
// // }
// }

atomic.StoreInt32(&s.inCmd, 0)
atomic.StoreInt32(&s.inFlg, 0)
}

// addCommand adds a in-building Cmd into current Command as a child-/sub-command.
// used by adder when ccb.Build.
func (s *ccb) addCommand(child *cli.Command) {
atomic.AddInt32(&s.inCmd, -1) // reset increased inCmd at AddCmd or Cmd
s.AddSubCommand(child)
}

// addFlag adds a in-building Flg into current Command as its flag.
// used by adder when ccb.Build.
func (s *ccb) addFlag(child *cli.Flag) {
atomic.AddInt32(&s.inFlg, -1)
s.AddFlag(child)
}

func (s *ccb) NewCommandBuilder(longTitle string, titles ...string) cli.CommandBuilder {
return s.Cmd(longTitle, titles...)
}

func (s *ccb) NewFlagBuilder(longTitle string, titles ...string) cli.FlagBuilder {
return s.Flg(longTitle, titles...)
}

func (s *ccb) Cmd(longTitle string, titles ...string) cli.CommandBuilder {
if atomic.LoadInt32(&s.inCmd) != 0 {
panic("cannot call Cmd() without Build() last Cmd()/AddCmd()")
}
atomic.AddInt32(&s.inCmd, 1)
return newCommandBuilderShort(s, longTitle, titles...)
}

func (s *ccb) Flg(longTitle string, titles ...string) cli.FlagBuilder {
if atomic.LoadInt32(&s.inFlg) != 0 {
panic("cannot call Flg() without Build() last Flg()")
}
atomic.AddInt32(&s.inFlg, 1)
return newFlagBuilderShort(s, longTitle, titles...)
}

func (s *ccb) AddCmd(cb func(b cli.CommandBuilder)) cli.CommandBuilder {
// if atomic.LoadInt32(&s.inCmd) != 0 {
// panic("cannot call AddCmd() without Build() last Cmd()/AddCmd()")
// }

bc := newCommandBuilderShort(s, "new-command")
defer bc.Build() // `Build' will add `bc'(Command) to s.Command as its SubCommand
cb(bc)
atomic.AddInt32(&s.inCmd, 1)
return s
}

func (s *ccb) AddFlg(cb func(b cli.FlagBuilder)) cli.CommandBuilder {
// if atomic.LoadInt32(&s.inFlg) != 0 {
// panic("cannot call AddFlg() without Build() last Flg()/AddFlg()")
// }

s.inCmd, s.inFlg = false, false
bc := newFlagBuilderShort(s, "new-flag")
defer bc.Build() // `Build' will add `bc'(Flag) to s.Command as its Flag
// atomic.AddInt32(&s.inFlg, 1)
// defer func() { atomic.AddInt32(&s.inFlg, -1) }()
cb(bc)
return s
}

//

//

//

func (s *ccb) Titles(longTitle string, titles ...string) cli.CommandBuilder {
s.Long, s.Short, s.Aliases = theTitles(longTitle, titles...)
return s
Expand Down Expand Up @@ -139,72 +214,6 @@ func (s *ccb) UseShell(shellPath string) cli.CommandBuilder {
return s
}

//
//
//

func (s *ccb) NewCommandBuilder(longTitle string, titles ...string) cli.CommandBuilder {
return s.Cmd(longTitle, titles...)
}

func (s *ccb) NewFlagBuilder(longTitle string, titles ...string) cli.FlagBuilder {
return s.Flg(longTitle, titles...)
}

func (s *ccb) Cmd(longTitle string, titles ...string) cli.CommandBuilder {
if s.inCmd {
panic("cannot call Cmd() without Build() last Cmd()/AddCmd()")
}
s.inCmd = true
return newCommandBuilderShort(s, longTitle, titles...)
}

func (s *ccb) Flg(longTitle string, titles ...string) cli.FlagBuilder {
if s.inFlg {
panic("cannot call Flg() without Build() last Flg()")
}
s.inFlg = true
return newFlagBuilderShort(s, longTitle, titles...)
}

func (s *ccb) AddCmd(cb func(b cli.CommandBuilder)) cli.CommandBuilder {
if s.inCmd {
panic("cannot call AddCmd() without Build() last Cmd()/AddCmd()")
}
s.inCmd = true
defer func() { s.inCmd = false }()

b := newCommandBuilderShort(s, "")
defer b.Build()
cb(b)
return s
}

func (s *ccb) AddFlg(cb func(b cli.FlagBuilder)) cli.CommandBuilder {
if s.inFlg {
panic("cannot call Flg() without Build() last Flg()/AddFlg()")
}
s.inFlg = true
defer func() { s.inFlg = false }()

b := newFlagBuilderShort(s, "")
defer b.Build()
cb(b)
return s
}

// addCommand adds a in-building Cmd into current Command as a child-/sub-command.
func (s *ccb) addCommand(child *cli.Command) {
s.inCmd = false
s.AddSubCommand(child)
}

// addFlag adds a in-building Flg into current Command as a child flag.
func (s *ccb) addFlag(child *cli.Flag) {
s.inFlg = false
s.AddFlag(child)
}

func theTitles(longTitle string, titles ...string) (lt, st string, aliases []string) {
lt = longTitle
switch len(titles) {
Expand Down
9 changes: 6 additions & 3 deletions builder/flagbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ func (s *ffb) Build() {
if a, ok := s.buildable.(adder); ok {
a.addFlag(s.Flag)
}
if s.parent != nil {
s.parent.AddFlag(s.Flag)
}
// if s.parent != nil {
// s.parent.AddFlag(s.Flag)
// // if a, ok := s.b.(adder); ok {
// // a.addFlag(s.Flag)
// // }
// }
}

func (s *ffb) SetApp(app buildable) { s.buildable = app }
Expand Down
Loading

0 comments on commit 6f3c68f

Please sign in to comment.