Documentation
¶
Overview ¶
Package cli provides utilities for building command-line applications.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrExitVersion = &unprintableError{errors.New("version flag exit")}
ErrExitVersion is an error indicating the application should exit after showing version.
var ErrInvalidArgs = errors.New("invalid arguments")
This error should be wrapped with fmt.Errorf to provide a specific, user-friendly message explaining the nature of the invalid arguments.
For example:
return fmt.Errorf("%w: missing required argument 'filename'", cli.ErrInvalidArgs)
Functions ¶
func Main ¶ added in v0.6.0
func Main(app App)
Main is a helper function that handles common startup tasks for command-line applications. It sets up signal handling for interrupts, runs the application, and prints errors to stderr.
func SetDocComment ¶ added in v0.6.0
func SetDocComment(src []byte)
SetDocComment stores the provided byte slice as the source for the application's documentation comment.
The parsing process assumes that the documentation comment is enclosed within a single /* ... */ block and extracts the content line by line. Any other multi-line comments within the embedded file will be ignored.
The parsed documentation will be included in the help message.
Example usage ¶
In application's doc.go:
/* Amazinator does amazing things... # Usage $ amazinator [flags...] Amazinator amazes amazinations by amazing your amazinators. */ package main import ( _ "embed" "go.astrophena.name/tools/internal/cli" ) //go:embed doc.go var doc []byte func init() { cli.SetDocComment(doc) }
Types ¶
type AppFunc ¶ added in v0.6.0
AppFunc is a function type that implements the App interface. It has no defined flags.
type Env ¶ added in v0.6.0
type Env struct { Args []string Getenv func(string) string Stdin io.Reader Stdout io.Writer Stderr io.Writer // contains filtered or unexported fields }
Env represents the application environment.