https://github.com/blixt/go-group
Add command group support with flags package.
https://github.com/blixt/go-group
Last synced: 5 months ago
JSON representation
Add command group support with flags package.
- Host: GitHub
- URL: https://github.com/blixt/go-group
- Owner: blixt
- Created: 2016-08-28T19:05:51.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2016-08-29T23:53:57.000Z (almost 10 years ago)
- Last Synced: 2025-10-11T15:58:39.816Z (8 months ago)
- Language: Go
- Homepage: https://godoc.org/github.com/blixt/go-group
- Size: 20.5 KB
- Stars: 4
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
group package
=============
[](https://godoc.org/github.com/blixt/go-group)
The group package makes it easier to handle multiple command line groups
with the `flags` package.
Example #1
----------
```go
package main
import "fmt"
import "github.com/blixt/go-group"
// Keep references to sub-commands to parse arguments and flags later.
var help = group.Sub("help")
var clone = group.Sub("clone")
// Each sub-command has its own FlagSet for specific flags.
var branch = clone.Flag.String("branch", "master", "The branch to clone")
func main() {
// Parse the sub-command used (and any flags along the way).
switch group.Parse() {
case help:
fmt.Println("This is some help for this tool.")
case clone:
repo := clone.Flag.Arg(0)
if repo == "" {
fmt.Println("Invalid repository!")
break
}
fmt.Printf("Cloning %s (branch %s)...\n", repo, *branch)
default:
fmt.Println("Unrecognized group. Choose one of:", group.Subs())
}
}
```
### Result
```
$ ./example clone -branch dev git.example.com:myrepo.git
Cloning git.example.com:myrepo.git (branch dev)...
$ ./example help
This is some help for this tool.
```
Example #2
----------
This example shows how to make a deeply nested command and global flags.
```go
package main
import "fmt"
import "github.com/blixt/go-group"
var preview = group.Sub("preview")
var app = preview.Sub("app")
var deploy = app.Sub("deploy")
// Global flag (before any of the command groups):
var verbose = group.Flag.Bool("v", false, "Output more")
func main() {
switch group.Parse() {
case deploy:
fmt.Println("Deploying...")
default:
fmt.Println("Unsupported command.")
}
if *verbose {
fmt.Println("And here's a bunch of extra output because you specified -v.")
}
}
```
(When the `app` command group is no longer in preview, you would just
need to change `preview.Sub("app")` to `group.Sub("app")`.)
### Result
```
$ ./example -v preview app deploy
Deploying...
And here's a bunch of extra output because you specified -v.
$ ./example somethingelse
Unsupported command.
```