https://github.com/meblum/cmd
Idiomatic Go Package for cli subcommands
https://github.com/meblum/cmd
arguments cli cmd flags go subcommands
Last synced: 6 months ago
JSON representation
Idiomatic Go Package for cli subcommands
- Host: GitHub
- URL: https://github.com/meblum/cmd
- Owner: meblum
- License: mit
- Created: 2023-02-06T19:21:59.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2023-03-29T03:31:33.000Z (almost 3 years ago)
- Last Synced: 2025-04-09T04:41:40.853Z (9 months ago)
- Topics: arguments, cli, cmd, flags, go, subcommands
- Language: Go
- Homepage: https://pkg.go.dev/github.com/meblum/cmd
- Size: 19.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Idiomatic Go Package for cli subcommands
[][godev]
This package is intended to be used in conjunction with the Go `flag` package for maping subcommands to their respective flagset.
The primary features of `cmd` are:
- Use the standerd `flag` packege. `cmd` does not implement its own flag parsing. Instead it integrates nicely with the standerd `flag` package.
- Common API, style and behavior closely follows that of the standers `flag` packege.
- Minimal feature set, does not attempt to be a "one size fits all" solution.
- Optionaly enable/disable extra command argument support (besides flags) for subcommands.
## Install
```
go get -u github.com/meblum/cmd
```
## Example usage
```go
import (
"flag"
"fmt"
"github.com/meblum/cmd"
)
func main() {
greeter := greetHandler{
greetType: "hello",
greetName: "Anonymous",
}
greetFlagSet := flag.NewFlagSet("greet", flag.ExitOnError)
greetFlagSet.StringVar(&greeter.greetType, "type", greeter.greetType, "greet type (hello|bye)")
greetFlagSet.StringVar(&greeter.greetName, "name", greeter.greetName, "name to print greeting for")
version := versionHandler{
version: "1.0.0",
verbose: false,
}
versionFlagSet := flag.NewFlagSet("version", flag.ExitOnError)
versionFlagSet.BoolVar(&version.verbose, "verbose", version.verbose, "output version with additional information")
c := &cmd.CmdSet{}
c.Add("print greeting", greetFlagSet, greeter, false)
c.Add("print version", versionFlagSet, version, false)
c.HandleCmd(nil, flag.ExitOnError)
}
type greetHandler struct {
greetType string
greetName string
}
func (g greetHandler) Handle(c *cmd.Cmd) error {
switch g.greetType {
case "hello":
fmt.Printf("Hello, %v! Nice to meet you :)", g.greetName)
case "bye":
fmt.Printf("Hello, %v! Nice to meet you :)", g.greetName)
default:
return fmt.Errorf("invalid greet type %v, valid options are 'hello' or 'bye'", g.greetType)
}
return nil
}
type versionHandler struct {
version string
verbose bool
}
func (g versionHandler) Handle(c *cmd.Cmd) error {
if g.verbose {
fmt.Printf("You are using Greeter version %v", g.version)
}
fmt.Print(g.version)
return nil
}
```
See the [documentation][godev] for more information.
[godev]: https://pkg.go.dev/github.com/meblum/cmd
## License
MIT - See [LICENSE][license] file
[license]: https://github.com/meblum/cmd/blob/master/LICENSE