Comments (2)
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.
With the single
AddCommand(Opts) Opts
, which drops thename
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)
- Runner Runner HOT 2
- Completion not implemented. HOT 2
- flag: *customtype - unsupported type HOT 1
- Tag commits - go mod friendly HOT 3
- goopts: Create struct from existing help HOT 1
- API change - added Call - Tag required - v1.1.0 HOT 2
- Sort commands in help output
- SubOpts
- env vars should have the program name as prefix HOT 2
- UseEnv() should not apply to auto-generated flags such as --help HOT 2
- Parse() calls os.Exit(0) in case of parse error HOT 3
- Override config file option value with option parameter HOT 3
- Inheriting flags with nested opts HOT 1
- Having option flags after arguments HOT 1
- Low-level parser API HOT 2
- Set default flag when default tag is set and no value present HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from opts.