cli

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Register

func Register(c *Command)

Register adds a command to the dispatch table. Commands call this from init().

func Run

func Run(argv []string) int

Run parses argv, builds a Context, and dispatches to the matching command. It returns a process exit code.

Types

type Args

type Args struct {
	Command    string
	Positional []string
	Named      map[string]string
	Flags      map[string]bool
}

Args is a parsed goforge invocation using poly-style argument syntax:

goforge <command> [positional...] key:value :flag

Tokens are classified as:

  • "key:value" -> a named argument (Named["key"] = "value")
  • ":flag" -> a boolean flag (Flags["flag"] = true)
  • bare word -> a positional argument (e.g. the create sub-type)

A value may itself contain colons (e.g. since:stable-1, out:a/b.txt); only the first colon separates the key from the value.

func Parse

func Parse(argv []string) Args

Parse splits a raw argument vector (already stripped of the program name) into the command and its arguments.

func (Args) Flag

func (a Args) Flag(name string) bool

Flag reports whether a boolean flag was set.

func (Args) Get

func (a Args) Get(name string) (string, bool)

Get returns a named argument and whether it was present.

func (Args) GetOr

func (a Args) GetOr(name, def string) string

GetOr returns a named argument or a default when absent.

func (Args) Pos

func (a Args) Pos(i int) string

Pos returns the positional argument at index i, or "" if out of range.

type Command

type Command struct {
	Name    string
	Summary string
	Usage   string
	Run     func(*Context) error
}

Command is a registered goforge subcommand.

func Commands

func Commands() []*Command

Commands returns all registered commands sorted by name.

func Lookup

func Lookup(name string) (*Command, bool)

Lookup returns a registered command by name.

type Context

type Context struct {
	Args   Args
	Stdin  io.Reader
	Stdout io.Writer
	Stderr io.Writer
	Dir    string
	IsTTY  bool
}

Context carries everything a command handler needs: the parsed arguments, output streams, and the directory from which goforge was invoked.

func (*Context) Colors

func (c *Context) Colors() render.Colors

Colors returns a colorizer honoring the color-mode argument and TTY status.

type ExitError

type ExitError struct{ Code int }

ExitError lets a command set the process exit code without the dispatcher printing an error message (the command has already reported its own output).

func (ExitError) Error

func (e ExitError) Error() string

Jump to

Keyboard shortcuts

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