Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lukechampine/flagg
Barebones subcommand handling
https://github.com/lukechampine/flagg
cli flags
Last synced: 3 months ago
JSON representation
Barebones subcommand handling
- Host: GitHub
- URL: https://github.com/lukechampine/flagg
- Owner: lukechampine
- License: mit
- Created: 2017-09-04T02:27:59.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-06-02T18:44:54.000Z (over 5 years ago)
- Last Synced: 2024-10-21T09:15:25.135Z (4 months ago)
- Topics: cli, flags
- Language: Go
- Size: 19.5 KB
- Stars: 57
- Watchers: 4
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
flagg
-----[](https://godoc.org/lukechampine.com/flagg)
[](https://goreportcard.com/report/lukechampine.com/flagg)```
go get lukechampine.com/flagg
````flagg` is a tiny package that makes it easier to build CLI apps that use
subcommands. I built it because the stdlib `flag` package is too low-level,
but popular alternatives like `spf13/cobra` are full-fledged frameworks with
too many bells and whistles for my liking. `flagg` is 67 lines of code and
imports only stdlib packages.`flagg` is designed around the stdlib `*flag.FlagSet` type. You represent each
subcommand in your app with a `*flag.FlagSet`, and arrange them into a hierarchy
with `flagg.Tree`. Then just call `flagg.Parse` to parse the subcommand selected
by `os.Args`.## Example
```go
// commands are just *flag.FlagSets
var rootCmd *flag.FlagSet = flagg.Root
rootCmd.Usage = flagg.SimpleUsage(rootCmd, "An example program")
verbose := rootCmd.Bool("v", false, "display verbose output")// flagg.New constructs a *flag.FlagSet with the given name and usage
// description
fooCmd := flagg.New("foo", "The foo subcommand")
bars := fooCmd.Int("bars", 0, "number of bars to foo")
quuxCmd := flagg.New("quux", "The quux subcommand")// construct the command hierarchy
tree := flagg.Tree{
Cmd: rootCmd,
Sub: []flagg.Tree{
{Cmd: fooCmd},
{Cmd: quuxCmd},
},
}// Parse the command hierarchy. cmd is the selected command, e.g. if
// os.Args = []string{"./example", "quux"}, then cmd == quuxCmd.
cmd := flagg.Parse(tree)// again, cmd is just a *flag.FlagSet, so Args() returns the arguments of the
// selected command.
args := cmd.Args()// use a switch to identify the cmd that was selected
switch cmd {
case fooCmd:
fmt.Printf("fooing %v bars\n", *bars)
case quuxCmd:
fmt.Println("quux!", args)
}
```