commander

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2016 License: GPL-3.0 Imports: 4 Imported by: 0

README

This is a simple Go library to manage commands for your CLI tool. Easy to use and now you can focus on Business Logic instead of building the command routing.

What this library does for you?

Manage your separated commands. How? Generates a general help and command specific helps for your commands. If your command fails somewhere (panic for example), commander will display the error message and the command specific help to guide your user.

Install

$ go get https://github.com/Yitsushi/go-commander

Sample output (from totp-cli)

$ totp-cli help

change-password                   Change password
update                            Check and update totp-cli itself
version                           Print current version of this application
generate <namespace>.<account>    Generate a specific OTP
add-token [namespace] [account]   Add new token
list [namespace]                  List all available namespaces or accounts under a namespace
delete <namespace>[.account]      Delete an account or a whole namespace
help [command]                    Display this help or a command specific help

Usage

Every single command has to implement CommandInterface. Check this project for examples.

package main

// Import the package
import "github.com/Yitsushi/go-commander"

// Yout Command
type YourCommand struct {
}

func (c *YourCommand) Execute() {
  // Command Action
}

func (c *YourCommand) ArgumentDescription() string {
  return "[name]"
}

func (c *YourCommand) Description() string {
  return "This is my first command"
}

func (c *YourCommand) Help() string {
  return "This is a useless command, but at least I have one command"
}

func (c *YourCommand) Examples() []string {
  return []string{"", "test"}
}

// Main Section
func main() {
	registry := commander.NewCommandRegistry()

	registry.Register("your-command", &YourCommand{})

	registry.Execute()
}

Now you have a CLI tool with two commands: help and your-command.

❯ go build mytool.go

❯ ./mytool
your-command [name]   This is my first command
help [command]        Display this help or a command specific help

❯ ./mytool help your-command
Usage: mytool your-command [name]

This is a useless command, but at least I have one command

Examples:
  mytool your-command
  mytool your-command test

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CommandInterface

type CommandInterface interface {
	// Execute function will be executed when the command is called
	Execute()
	// Description is used to display the "one-liner" help message in genera help
	Description() string
	// ArgumentDescription is used in general help
	ArgumentDescription() string
	// Help output would be a human readable long command specific help message
	Help() string
	// Examples is an array of possible calls
	Examples() []string
}

CommandInterface defined a command. If a struct implements all the required function, it is acceptable as a Command for CommandRegistry

type CommandRegistry

type CommandRegistry struct {
	Commands map[string]CommandInterface
	// contains filtered or unexported fields
}

CommandRegistry will handle all CLI request and find the route to the proper Command

func NewCommandRegistry

func NewCommandRegistry() *CommandRegistry

NewCommandRegistry is a simple "constructor"-like function that initializes Commands map

func (*CommandRegistry) CommandHelp

func (c *CommandRegistry) CommandHelp(name string)

CommandHelp prints more detailed help for a specific Command

func (*CommandRegistry) Execute

func (c *CommandRegistry) Execute()

Execute finds the proper command, handle errors from the command and print Help if the given command it unknown or print the Command specific help if something went wrong or the user asked for it.

func (*CommandRegistry) Help

func (c *CommandRegistry) Help()

Help lists all available commands to the user

func (*CommandRegistry) Register

func (c *CommandRegistry) Register(name string, handler CommandInterface)

Register function is used to register a Command in CommandRegistry the first argument will be the command from the CLI the second argument will be the handler that implements CommandInterface

Jump to

Keyboard shortcuts

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