cli

package
v0.0.0-...-97f1b17 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2017 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package cli is a very minimal framework for creating command line applications.

cli only supports boolean flags and top level commands, which is all that gomake needs. We can write a simple greeter like so:

package main

import (
	"fmt"
	"os"

	"github.com/hinshun/gomake/pkg/cli"
)

func main() {
	app := cli.App{
		Name: "greeter",
		Action: func(ctx *cli.Context) error {
			fmt.Println("hello")
			return nil
		},
	}

	app.Run(os.Args)
}

Index

Constants

This section is empty.

Variables

View Source
var (
	// HelpFlag is the flag to display the App's help text
	HelpFlag = &Flag{
		Name:        "help",
		Aliases:     []string{"h"},
		Description: "show help",
	}

	// VersionFlag is the flag to display the App's version text
	VersionFlag = &Flag{
		Name:        "version",
		Aliases:     []string{"v"},
		Description: "print the version",
	}
)
View Source
var (
	// ErrIncorrectUsage is returned when the App is ran with bad arguments.
	ErrIncorrectUsage = errors.New("incorrect usage")
)

Functions

func ParseFlags

func ParseFlags(flags Flags, args []string) map[string]struct{}

ParseFlags parses the args and returns a map of flags set.

Types

type Action

type Action func(ctx *Context) error

Action is a function to call with the context of arguments to the App.

func ParseCommands

func ParseCommands(defaultAction Action, commands Commands, args []string) Action

ParseCommands parses the args and returns the Action to invoke.

type App

type App struct {
	// Name is the name of the program.
	Name string
	// Version is the version of the program.
	Version string
	// Action is the default action to execute when no subcommands are specified.
	Action Action
	// Commands is the list of subcommands the program can run.
	Commands Commands
	// Flags is the list of boolean flags that can be enabled.
	Flags Flags
}

App is a simple cli application.

func (*App) Run

func (a *App) Run(args []string) error

Run runs the App with the given args and shows help on errors.

func (*App) ShowHelp

func (a *App) ShowHelp() error

ShowHelp displays the help text for the App.

func (*App) ShowVersion

func (a *App) ShowVersion()

ShowHelp displays the version text for the App.

type Command

type Command struct {
	// Name is the name of the subcommand.
	Name string
	// Description is a brief text about the subcommand.
	Description string
	// Action is the function to call when the command is invoked.
	Action Action
}

Command is a subcommand for an App.

type Commands

type Commands []*Command

Commands is a sortable list of commands.

func (Commands) ActionForName

func (c Commands) ActionForName(name string) Action

func (Commands) Len

func (c Commands) Len() int

Len returns the length of commands.

func (Commands) Less

func (c Commands) Less(i, j int) bool

Len returns whether the command at index i is less than at index j.

func (Commands) Swap

func (c Commands) Swap(i, j int)

Swap swaps the commands at index i and j.

type Context

type Context struct {
	// Action is the context wrapped function to be evaluated.
	Action func() error
	// contains filtered or unexported fields
}

Context is the context is which an Action is ran.

func NewContext

func NewContext(app *App, args []string) (*Context, error)

NewContext initializes a new context for the Action to run in.

func (*Context) IsSet

func (c *Context) IsSet(name string) bool

IsSet returns whether flag with name is enabled.

type Flag

type Flag struct {
	// Name is the name of this flag.
	Name string
	// Aliases is the list of alternate names to enable the flag.
	Aliases []string
	// Description is a brief text of what the flag enables.
	Description string
}

Flag is a boolean flag that gets passed down to the action called.

func (*Flag) HasName

func (f *Flag) HasName(name string) bool

HasName returns true if name matches the flag's name or its aliases.

type Flags

type Flags []*Flag

Flags is a list of flags.

func (Flags) NameForAlias

func (f Flags) NameForAlias(alias string) string

HasName returns true if any flag in Flags matches name.

Jump to

Keyboard shortcuts

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