Giter Site home page Giter Site logo

Comments (2)

jpillora avatar jpillora commented on August 13, 2024

Interesting, though not a big fan of all the deep traversals; I think two ways of methods for adding commands adds a little too much complexity, and I can imagine needing to check the docs to confirm which one is which. Also, I think you could get lost in Parent().Parent(). calls quite quickly.

With the single AddCommand(Opts) Opts, which drops the name param, I was thinking you can achieve something similar with:

func main() {
	opts.New(&Config{}).
		Name("root").
		AddCommand(
			opts.New(&Foo{}).
				Name("foo").
				AddCommand(
					opts.New(&Bar{}).
						Name("bar"),
				),
		).
		Parse().
		RunFatal()
}

Adding custom documentation for each command would get messy here, and in deeply nested fluent chains. With Name(), Doc() calls, I think subcommands should have their own initialisation (register) function:

func main() {
	config := Config{}
	c := opts.New(&config).Name("root")
	registerFoo(c)
	//registerFizz(c)..
	//registerFuzz(c)....
	c.Parse().RunFatal()
}

//another file foo.go, or could be
//another package (foo.Register)
func registerFoo(parent opts.Opts) {
	foo := Foo{}
	f := opts.New(&foo).Name("foo")
	registerBar(f)
	parent.AddCommand(f)
}

//another file bar.go, or could be
//another package (bar.Register)
func registerBar(parent opts.Opts) {
	bar := Bar{}
	b := opts.New(&bar).Name("bar")
	parent.AddCommand(b)
}

In addition, the users's register functions could also return the parent if they wanted to slot the call into their fluent chain.

from opts.

millergarym avatar millergarym commented on August 13, 2024

With the single AddCommand(Opts) Opts, which drops the name param, I was thinking you can achieve something similar with:

Nice!

I can see this pitching up in of my code. Extendable to Docs.

func NewOpt(name string, config interface{}) opts.Builder {
	return opts.New(config).Name(name)
}

func main() {
	NewOpt("root", &Config{}).
		AddCommand(NewOpt("foo", &Foo{}).
			AddCommand(NewOpt("bar", &Bar{})),
		).
		Parse().
		Run()
}

Might be worth putting a NewNamed into the library as a hint / helper.

func NewNamed(name string, config interface{}) Builder {
	return New(config).Name(name)
}

my code would then be

	newopt := opts.NewNamed
	newopt("root", &Config{}).
		AddCommand(newopt("foo", &Foo{}).
			AddCommand(newopt("bar", &Bar{})),
		).
		Parse().
		Run()

from opts.

Related Issues (17)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.