Documentation
¶
Overview ¶
Package cobra applies a restrained ANSI palette — aligned with github.com/firetiger-oss/stripes — to help, usage, and error output of commands built with github.com/spf13/cobra.
The integration mirrors github.com/charmbracelet/fang: help and usage are rendered from scratch (not by post-processing cobra's defaults), and ANSI is downgraded or stripped automatically when stdout/stderr is not a terminal.
Typical usage:
root := &cobra.Command{Use: "mytool", Short: "..."}
// ... add subcommands and flags ...
if err := stripescobra.Execute(ctx, root); err != nil {
os.Exit(1)
}
Index ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultStyles = &Styles{ Title: stripes.DefaultStyles.Name, Program: stripes.DefaultStyles.Name, Command: stripes.DefaultStyles.Anchor, Flag: stripes.DefaultStyles.String, Argument: lipgloss.NewStyle().Foreground(lipgloss.Color("250")), Description: lipgloss.NewStyle(), Default: stripes.DefaultStyles.Comment, Example: stripes.DefaultStyles.Code, Error: lipgloss.NewStyle().Foreground(lipgloss.Color("1")).Bold(true), Hint: stripes.DefaultStyles.Comment, Indent: stripes.DefaultStyles.Indent, Width: stripes.DefaultStyles.Width, }
DefaultStyles reuses fields of stripes.DefaultStyles so the two palettes cannot drift. lipgloss.Style is a value type, so the assignments below are independent copies — later mutation of stripes.DefaultStyles does not affect this instance.
Functions ¶
func Apply ¶
Apply installs styled help and usage rendering on root and every subcommand reachable from it. It does not run the command.
Use Apply when the caller wants to own the Execute call; otherwise prefer Execute, which also routes errors through the configured handler.
func DefaultErrorHandler ¶
DefaultErrorHandler writes a styled "Error: <msg>" line to w. For errors that look like flag- or usage-parsing failures, it also emits a hint pointing users at --help.
Types ¶
type ErrorHandler ¶
ErrorHandler renders an error to w using styles s. Implementations should not return — any reporting must happen before returning.
type Option ¶
type Option func(*config)
Option configures Execute or Apply.
func WithErrorHandler ¶
func WithErrorHandler(fn ErrorHandler) Option
WithErrorHandler overrides the function used to render errors returned from cobra.Command.ExecuteContext.
func WithErrorOutput ¶
WithErrorOutput overrides the writer used for error output. Defaults to os.Stderr.
func WithOutput ¶
WithOutput overrides the writer used for help and usage output. Defaults to os.Stdout.
type Styles ¶
type Styles struct {
Title lipgloss.Style // section headings: "Usage:", "Flags:", etc.
Program lipgloss.Style // program name in the usage line
Command lipgloss.Style // subcommand names in the commands list
Flag lipgloss.Style // flag tokens like --verbose / -v
Argument lipgloss.Style // <file>, [flags], placeholders
Description lipgloss.Style // descriptions / Long text
Default lipgloss.Style // "(default 5)" trailing fragments
Example lipgloss.Style // example command lines
Error lipgloss.Style // "Error:" prefix
Hint lipgloss.Style // "Try --help for usage." footer
Indent string
Width int
}
Styles defines the styling configuration for cobra help, usage, and error output. Each field is applied to a specific token type when rendering.