Skip to content

Commit

Permalink
rename: Command -> CmdS, BaseOptI => Cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
hedzr committed Oct 17, 2024
1 parent 495b547 commit 257a2ad
Show file tree
Hide file tree
Showing 44 changed files with 843 additions and 711 deletions.
38 changes: 24 additions & 14 deletions builder/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (s *appS) Run(ctx context.Context, opts ...cli.Opt) (err error) {
return errors.New("a NewFlagBuilder()/Flg() call needs ending with Build()")
}

if s.root == nil || s.root.Command == nil {
if s.root == nil || s.root.Cmd == nil {
return cli.ErrEmptyRootCommand
}

Expand Down Expand Up @@ -65,7 +65,9 @@ func (s *appS) Build() {
if sr, ok := s.Runner.(setRoot); ok {
ctx := context.Background()
logz.DebugContext(ctx, "[cmdr] builder.appS.Build() - setRoot")
s.root.EnsureTree(ctx, s, s.root)
if cx, ok := s.root.Cmd.(*cli.CmdS); ok {
cx.EnsureTree(ctx, s, s.root)
}
sr.SetRoot(s.root, s.args)
}
}
Expand All @@ -79,12 +81,12 @@ func (s *appS) ensureNewApp() cli.App { //nolint:unparam
// Author: "",
// HeaderLine: "",
// FooterLine: "",
// Command: nil,
// CmdS: nil,
}
}
if s.root.Command == nil {
s.root.Command = new(cli.Command)
s.root.Command.SetName(s.root.AppName)
if s.root.Cmd == nil {
s.root.Cmd = new(cli.CmdS)
s.root.Cmd.SetName(s.root.AppName)
}
return s
}
Expand All @@ -107,13 +109,17 @@ func (s *appS) Info(name, version string, desc ...string) cli.App {
conf.Version = version
}
}
s.root.SetDescription("", desc...)
if cx, ok := s.root.Cmd.(*cli.CmdS); ok {
cx.SetDescription("", desc...)
}
return s
}

func (s *appS) Examples(examples ...string) cli.App {
s.ensureNewApp()
s.root.SetExamples(examples...)
if cx, ok := s.root.Cmd.(*cli.CmdS); ok {
cx.SetExamples(examples...)
}
return s
}

Expand Down Expand Up @@ -176,14 +182,14 @@ func (s *appS) Flg(longTitle string, titles ...string) cli.FlagBuilder {
return newFlagBuilderShort(s, longTitle, titles...)
}

func (s *appS) NewCmdFrom(from *cli.Command, cb func(b cli.CommandBuilder)) cli.App {
func (s *appS) NewCmdFrom(from *cli.CmdS, cb func(b cli.CommandBuilder)) cli.App {
b := newCommandBuilderFrom(from, s, "")
defer b.Build()
cb(b)
return s
}

func (s *appS) NewFlgFrom(from *cli.Command, defaultValue any, cb func(b cli.FlagBuilder)) cli.App {
func (s *appS) NewFlgFrom(from *cli.CmdS, defaultValue any, cb func(b cli.FlagBuilder)) cli.App {
b := newFlagBuilderFrom(from, s, defaultValue, "")
defer b.Build()
cb(b)
Expand All @@ -204,16 +210,20 @@ func (s *appS) AddFlg(cb func(b cli.FlagBuilder)) cli.App {
return s
}

func (s *appS) addCommand(child *cli.Command) {
func (s *appS) addCommand(child *cli.CmdS) {
atomic.AddInt32(&s.inCmd, -1)
if s.root == nil {
s.root = &cli.RootCommand{Command: child}
s.root = &cli.RootCommand{Cmd: child}
} else {
s.root.AddSubCommand(child)
if cx, ok := s.root.Cmd.(*cli.CmdS); ok {
cx.AddSubCommand(child)
}
}
}

func (s *appS) addFlag(child *cli.Flag) {
atomic.AddInt32(&s.inFlg, -1)
s.root.AddFlag(child)
if cx, ok := s.root.Cmd.(*cli.CmdS); ok {
cx.AddFlag(child)
}
}
4 changes: 2 additions & 2 deletions builder/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ func TestAppS_AddCmd(t *testing.T) {
a.AddCmd(func(b cli.CommandBuilder) {
b.Titles("ask", "a")
})
assertEqual(t, a.root.Long, "ask")
assertEqual(t, a.root.Short, "a")
assertEqual(t, a.root.Cmd.(*cli.CmdS).Long, "ask")
assertEqual(t, a.root.Cmd.(*cli.CmdS).Short, "a")

a.AddCmd(func(b cli.CommandBuilder) {
b.Titles("bunny", "b")
Expand Down
4 changes: 2 additions & 2 deletions builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func newDefaultRoot() *cli.RootCommand {
}

root := new(cli.RootCommand)
root.Command = new(cli.Command)
root.Cmd = new(cli.CmdS)
root.AppName = conf.AppName
root.Version = conf.Version
return root
Expand All @@ -44,6 +44,6 @@ type setRoot interface {
}

type adder interface {
addCommand(child *cli.Command)
addCommand(child *cli.CmdS)
addFlag(child *cli.Flag)
}
32 changes: 16 additions & 16 deletions builder/commandbuilders.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ type buildable interface {
Build()
}

// func NewCommandBuilder(parent *cli.Command, longTitle string, titles ...string) *ccb {
// func NewCommandBuilder(parent *cli.CmdS, longTitle string, titles ...string) *ccb {
// // s := &ccb{
// // nil, parent,
// // new(cli.Command),
// // new(cli.CmdS),
// // false, false,
// // }
// // s.Long, s.Short, s.Aliases = theTitles(longTitle, titles...)
Expand All @@ -24,13 +24,13 @@ type buildable interface {
// }

func newCommandBuilderShort(b buildable, longTitle string, titles ...string) *ccb {
return newCommandBuilderFrom(new(cli.Command), b, longTitle, titles...)
return newCommandBuilderFrom(new(cli.CmdS), b, longTitle, titles...)
}

func newCommandBuilderFrom(from *cli.Command, b buildable, longTitle string, titles ...string) *ccb {
func newCommandBuilderFrom(from *cli.CmdS, b buildable, longTitle string, titles ...string) *ccb {
s := &ccb{
b, from,
new(cli.Command),
new(cli.CmdS),
0, 0,
}
s.Long, s.Short, s.Aliases = theTitles(longTitle, titles...)
Expand All @@ -39,20 +39,20 @@ func newCommandBuilderFrom(from *cli.Command, b buildable, longTitle string, tit

type ccb struct {
buildable
parent *cli.Command
*cli.Command
parent *cli.CmdS
*cli.CmdS
inCmd int32
inFlg int32
}

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

Expand All @@ -71,20 +71,20 @@ func (s *ccb) WithSubCmd(cb func(b cli.CommandBuilder)) {
// }

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

// addCommand adds a in-building Cmd into current Command as a child-/sub-command.
// addCommand adds a in-building Cmd into current CmdS as a child-/sub-command.
// used by adder when ccb.Build.
func (s *ccb) addCommand(child *cli.Command) {
func (s *ccb) addCommand(child *cli.CmdS) {
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.
// addFlag adds a in-building Flg into current CmdS as its flag.
// used by adder when ccb.Build.
func (s *ccb) addFlag(child *cli.Flag) {
atomic.AddInt32(&s.inFlg, -1)
Expand Down Expand Up @@ -121,7 +121,7 @@ func (s *ccb) AddCmd(cb func(b cli.CommandBuilder)) cli.CommandBuilder {
// }

bc := newCommandBuilderShort(s, "new-command")
defer bc.Build() // `Build' will add `bc'(Command) to s.Command as its SubCommand
defer bc.Build() // `Build' will add `bc'(CmdS) to s.CmdS as its SubCommand
cb(bc)
atomic.AddInt32(&s.inCmd, 1)
return s
Expand All @@ -133,7 +133,7 @@ func (s *ccb) AddFlg(cb func(b cli.FlagBuilder)) cli.CommandBuilder {
// }

bc := newFlagBuilderShort(s, "new-flag")
defer bc.Build() // `Build' will add `bc'(Flag) to s.Command as its Flag
defer bc.Build() // `Build' will add `bc'(Flag) to s.CmdS as its Flag
// atomic.AddInt32(&s.inFlg, 1)
// defer func() { atomic.AddInt32(&s.inFlg, -1) }()
cb(bc)
Expand Down
6 changes: 3 additions & 3 deletions builder/flagbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/hedzr/cmdr/v2/cli"
)

// func NewFlagBuilder(parent *cli.Command, defaultValue any, longTitle string, titles ...string) *ffb {
// func NewFlagBuilder(parent *cli.CmdS, defaultValue any, longTitle string, titles ...string) *ffb {
// // s := &ffb{
// // nil, parent,
// // new(cli.Flag),
Expand All @@ -20,7 +20,7 @@ func newFlagBuilderShort(b buildable, longTitle string, titles ...string) *ffb {
return newFlagBuilderFrom(nil, b, nil, longTitle, titles...)
}

func newFlagBuilderFrom(parent *cli.Command, b buildable, defaultValue any, longTitle string, titles ...string) *ffb {
func newFlagBuilderFrom(parent *cli.CmdS, b buildable, defaultValue any, longTitle string, titles ...string) *ffb {
s := &ffb{
b, parent,
new(cli.Flag),
Expand All @@ -32,7 +32,7 @@ func newFlagBuilderFrom(parent *cli.Command, b buildable, defaultValue any, long

type ffb struct {
buildable
parent *cli.Command
parent *cli.CmdS
*cli.Flag
}

Expand Down
8 changes: 4 additions & 4 deletions cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ type App interface {
// // After the closure invoked, Build() will be called implicitly.
// AddFlg(cb func(b FlagBuilder)) App

// NewCmdFrom creates a CommandBuilder from 'from' Command.
NewCmdFrom(from *Command, cb func(b CommandBuilder)) App
// NewFlgFrom creates a CommandBuilder from 'from' Command.
NewFlgFrom(from *Command, defaultValue any, cb func(b FlagBuilder)) App
// NewCmdFrom creates a CommandBuilder from 'from' CmdS.
NewCmdFrom(from *CmdS, cb func(b CommandBuilder)) App
// NewFlgFrom creates a CommandBuilder from 'from' CmdS.
NewFlgFrom(from *CmdS, defaultValue any, cb func(b FlagBuilder)) App

Runner

Expand Down
13 changes: 8 additions & 5 deletions cli/baseopt.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,17 @@ func (c *BaseOpt) SetHidden(hidden bool, vendorHidden ...bool) {
}
}

// func (c *BaseOpt) Owner() *CmdS { return c.owner } // the owner of this CmdS

func (c *BaseOpt) OwnerOrParent() BacktraceableMin { return c.owner }
func (c *BaseOpt) OwnerIsNil() bool { return c.owner == nil }
func (c *BaseOpt) OwnerCmd() BaseOptI { return c.owner }
func (c *BaseOpt) Owner() *Command { return c.owner } // the owner of this Command
func (c *BaseOpt) Root() *RootCommand { return c.root } // returns Root Command (*RootCommand),
func (c *BaseOpt) OwnerIsNotNil() bool { return c.owner != nil }
func (c *BaseOpt) OwnerCmd() Cmd { return c.owner }
func (c *BaseOpt) Root() *RootCommand { return c.root } // returns Root CmdS (*RootCommand),
func (c *BaseOpt) App() App { return c.root.app } // App returns the current App
func (c *BaseOpt) Set() store.Store { return c.root.app.Store() } // Set returns store.Store associated with the current App
func (c *BaseOpt) SetOwner(o *Command) { c.owner = o }
func (c *BaseOpt) SetOwner(o *CmdS) { c.owner = o }
func (c *BaseOpt) SetOwnerCmd(o Cmd) { c.owner = o }
func (c *BaseOpt) SetRoot(root *RootCommand) { c.root = root }

// func (c *BaseOpt) AppName() string {
Expand Down Expand Up @@ -113,7 +116,7 @@ func (c *BaseOpt) Shorts() (shorts []string) {
return
}

// GetName returns the name of a `Command`.
// GetName returns the name of a `CmdS`.
func (c *BaseOpt) GetName() string {
if len(c.name) > 0 {
return c.name
Expand Down
Loading

0 comments on commit 257a2ad

Please sign in to comment.