go-commando

command module
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 5, 2026 License: MIT Imports: 6 Imported by: 0

README

go-commando GoDoc

The go-commando module provides tools and testing utilities for creating and testing modular types that encapsulate distinct program flow. Inspired by the separation of concerns design principal.

Basic Usage

The Command Interface
type Command interface {
	// Get the command's identifier.
	GetID() string

	// Get the command's usage string.
	GetUsage() string

	// Initializes the command before it's run.
	Initialize()

	// Run the command using the provided arguments and return any errors.
	Run(args []string) error
}
A Sample Flag-Based Command
type HelloCommand struct {
	command.FlagCommandBase
}

func NewHelloCommand() *HelloCommand {
	return &HelloCommand{
		FlagCommandBase: command.NewFlagCommandBase("hello", "prints \"Hello {name}\" to the console"),
	}
}

func (cmd HelloCommand) Run(args []string) error {
	name := cmd.Flags.String("name", "World", "name to use when saying hello")
	cmd.Flags.Parse(args)

	if *name == "" {
		return fmt.Errorf("name cannot be empty")
	}

	fmt.Printf("Hello %s!\n", *name)
	return nil
}
Sample main Function
func main() {
	ls := flag.Bool("ls", false, "list all commands")
	flag.Parse()

	runner := command.NewRunner(
		sample.NewHelloCommand(),
	)

	if *ls || len(os.Args) < 2 {
		runner.ListCommands()
		return
	}

	if err := runner.RunCommand(os.Args[1], os.Args[2:]); err != nil {
		fmt.Printf("%s %s\n", style.BoldError.Sprint("ERROR:"), err.Error())
	}
}

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
Package command provides tools for creating modular types that encapsulate distinct program flow.
Package command provides tools for creating modular types that encapsulate distinct program flow.
sample
Package sample provides sample commands that demonstrate the command package.
Package sample provides sample commands that demonstrate the command package.
Package test provides test utilities for command types.
Package test provides test utilities for command types.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL