A lightweight and flexible library for building command-line interfaces (CLIs) in Go. This library provides a simple and intuitive way to define commands, flags (including short names), aliases and more.
-n
for --name
).go get github.com/mirkobrombin/go-cli-builder
package main
import (
"fmt"
"os"
"github.com/mirkobrombin/go-cli-builder/v1/root"
"my-cli/commands"
)
func main() {
rootCmd := root.NewRootCommand("mycli", "mycli [command]", "A simple CLI example", "1.0.0")
rootCmd.AddBoolFlag("verbose", "v", "Enable verbose output", false, false)
rootCmd.AddCommand(commands.NewAddCommand())
rootCmd.AddCommand(commands.NewRemoveCommand())
rootCmd.AddCommand(commands.NewListCommand())
if err := rootCmd.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}
Note, the verbose
flag was registered at the root level, so it can be accessed
by all subcommands via the rootFlags
parameter in the Run
function, e.g.:
func runSubcommand(cmd *command.Command, rootFlags *command.RootFlags, args []string) error {
if rootFlags.GetBool("verbose") {
cmd.Logger.Info("Verbose output enabled")
}
// ...
return nil
}
For more detailed information, please refer to the documentation files in the docs/ directory.
This project is licensed under the MIT License. See the LICENSE file for details.