https://github.com/matzefriedrich/cobra-extensions
An extension library for Cobra.
https://github.com/matzefriedrich/cobra-extensions
cli cobra go golang reflection separation-of-concerns struct-tags
Last synced: 4 months ago
JSON representation
An extension library for Cobra.
- Host: GitHub
- URL: https://github.com/matzefriedrich/cobra-extensions
- Owner: matzefriedrich
- License: mit
- Created: 2023-10-26T20:20:51.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-13T22:34:54.000Z (5 months ago)
- Last Synced: 2025-01-13T23:28:46.231Z (5 months ago)
- Topics: cli, cobra, go, golang, reflection, separation-of-concerns, struct-tags
- Language: Go
- Homepage:
- Size: 79.1 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/matzefriedrich/cobra-extensions/actions/workflows/go.yml)
[](https://coveralls.io/github/matzefriedrich/cobra-extensions)
[](https://pkg.go.dev/github.com/matzefriedrich/cobra-extensions)
[](https://goreportcard.com/report/github.com/matzefriedrich/cobra-extensions)


# cobra-extensions
**cobra-extensions** is an extension package for the well-known [Cobra](https://github.com/spf13/cobra) library, designed to enhance command management by introducing a declarative approach for binding flags to command structs. With this approach, you build complex CLI applications consisting of many commands in a clean and organized manner.
By leveraging command structs, adopting patterns like dependency injection becomes far easier, further simplifying the design of scalable and maintainable CLI tools. The library automates flag setup using struct tags, reducing boilerplate and improving developer productivity.
## Usage
Using the Cobra extensions is a no-brainer. Use, `go get` to install the latest version of the library.
````bash
$ go get -u github.com/matzefriedrich/cobra-extensions@latest
````Next, include Cobra extensions in your application.
````go
import (
"github.com/matzefriedrich/cobra-extensions/pkg/commands"
"github.com/matzefriedrich/cobra-extensions/pkg/types"
"github.com/spf13/cobra"
)
````## Example
All it needs is the definition of a struct that implements the `TypedCommand` interface. Public fields are annotated with `flag` tags to define flags and bind them to the struct once parsed. A private field of type `CommandName` specifies the command's name. For instance:
````golang
package commandsimport (
"fmt"
"github.com/matzefriedrich/cobra-extensions/pkg/commands"
"github.com/matzefriedrich/cobra-extensions/pkg/types"
"github.com/spf13/cobra"
)type helloCommand struct {
use types.CommandName `flag:"hello"`
Arguments helloArgs
}var _ types.TypedCommand = (*helloCommand)(nil)
type helloArgs struct {
types.CommandArgs
Name string
}func CreateHelloCommand() *cobra.Command {
instance := &helloCommand{
Arguments: helloArgs{
CommandArgs: types.NewCommandArgs(types.MinimumArgumentsRequired(1)),
}}
return commands.CreateTypedCommand(instance)
}func (c *helloCommand) Execute() {
fmt.Printf("Hello %s.\n", c.Arguments.Name)
}````
A `CreateHelloCommand` factory method creates a new `helloCommand` instance and utilizes the `CreateTypedCommand` method to create and initialize a Cobra command.
````golang
package mainimport (
"github.com/matzefriedrich/cobra-extensions/example/commands"
"github.com/matzefriedrich/cobra-extensions/pkg/charmer"
"github.com/spf13/cobra"
)func main() {
app := charmer.NewRootCommand("simple-example", "")
app.AddCommand(commands.CreateHelloCommand())
err := app.Execute()
if err != nil {
panic(err)
}
}
````