https://github.com/phrozen/cmd
Commander is a command/task CLI manager written in Go.
https://github.com/phrozen/cmd
Last synced: 8 months ago
JSON representation
Commander is a command/task CLI manager written in Go.
- Host: GitHub
- URL: https://github.com/phrozen/cmd
- Owner: phrozen
- License: other
- Created: 2015-09-30T19:49:46.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2021-02-20T19:42:18.000Z (over 5 years ago)
- Last Synced: 2023-03-23T10:58:30.654Z (about 3 years ago)
- Language: Go
- Homepage:
- Size: 5.86 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Commander
Commander (**cmd**) is a command/task manager that builds CLI tasks and flags out of your Go code using reflection on your data structs and functions.
### Usage
To create tasks and flags to run those tasks simply define your usual struct types amd functions on them.
```
type Test struct {
Name string
}
func (t *Test) Hello() {
fmt.Printf("Hello %s!", t.Name)
}
```
Then simply call Commanderize on your struct. You can set default values by initializing your struct.
```
err:= Commanderize(cmd.Default, &Test{"World"})
if err!= nil {
// Do something with err
}
```
What ```Commanderize()``` does is, it reads all the exported fields on your struct type (only those supported by the ```flag``` pkg) and binds them to flags with their name in lower case which defaults to their initial value and uses the ```cmd``` tag as the usage string, calls internally ```flag.Parse()``` so that your fields get the parsed values if any, then parses the first argument ```Arg(0)``` which should be in the format ```:``` looks for all the exported methods and executes the right one. It returns a detailed error (which you can print) if there was a problem in any of the steps. So after that you can compile your binary and run:
```
# binary test:hello
Hello World!
# binary -name=John test:hello
Hello John!
```
You can code multiple structs and pass them to ```Commanderize()``` in one call, the first parameter is an Options struct and a Default is provided (use it for now), the second one is a variadic argument which takes any number of structs so your CLI can better separate one task from another. Default usage is done via the ```cmd``` tag and you can untag fields so they aren´t checked. Any unexported member (field or function) is not checked by Commander.
```
type MyStruct struct {
Check bool `cmd:"Usage string for check."`
Price float64 `cmd:"Usage string for price."`
unexported string // Unexported fields do not generate flags
Untagged *SomeType `cmd:"-"` // Untagged fields do not generate flags
}
```
### Todo
* Better documentation
* Support variadic argument functions with extra Args[1:]
* Colorize CLI