Documentation
¶
Overview ¶
Package ctrl provides a unified, zero-dependency CLI application framework and high-performance terminal UI toolkit for Go.
It consolidates POSIX argument and flag parsing (argkit), subcommand routing, terminal capability sniffing, and terminal UI styling primitives (tuikit) into a single, cohesive architecture.
Architectural Foundations ¶
- github.com/lemon4ksan/ctrl/argkit - Zero-dependency POSIX argument parser, flag clumper (-la), attached value parser (-I*.tmp), and fuzzy typo suggester.
- github.com/lemon4ksan/ctrl/tuikit - Zero-dependency terminal UI toolkit, providing CLI application management (tuikit.App), capability detection (tuikit.ProbeTerminal), tables (tuikit.Table), boxes (tuikit.Box), progress bars (tuikit.Progress), sparklines (tuikit.RenderSparkline), and status badges.
Basic Usage ¶
package main
import (
"context"
"fmt"
"io"
"os"
"github.com/lemon4ksan/ctrl"
"github.com/lemon4ksan/ctrl/tuikit"
)
func main() {
app := ctrl.NewApp("mycli", ctrl.Version, "High-performance CLI utility",
ctrl.NewCommand("hello", "Print a greeting", "mycli hello [name]",
func(ctx context.Context, args []string, stdout, stderr io.Writer) error {
name := "World"
if len(args) > 0 {
name = args[0]
}
fmt.Fprintf(stdout, "%s Hello, %s!\n", tuikit.BadgePass(), name)
return nil
},
),
)
if err := app.Run(context.Background(), os.Args[1:]); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}
Example ¶
Example demonstrates constructing a CLI application using ctrl.
package main
import (
"context"
"fmt"
"io"
"github.com/lemon4ksan/ctrl"
)
func main() {
app := ctrl.NewApp("example", "1.0.0", "Minimal example CLI",
ctrl.NewCommand("greet", "Print greeting message", "example greet [name]",
func(ctx context.Context, args []string, stdout, stderr io.Writer) error {
name := "World"
if len(args) > 0 {
name = args[0]
}
fmt.Fprintf(stdout, "Hello, %s!\n", name)
return nil
},
),
)
_ = app.Run(context.Background(), []string{"greet", "Gopher"})
}
Output: Hello, Gopher!
Index ¶
Examples ¶
Constants ¶
const Version = "0.1.0"
Version is the current semantic release version of the ctrl framework.
Variables ¶
var ( Bold = tuikit.Bold Dim = tuikit.Dim Italic = tuikit.Italic Underline = tuikit.Underline Red = tuikit.Red Green = tuikit.Green Yellow = tuikit.Yellow Blue = tuikit.Blue Magenta = tuikit.Magenta Cyan = tuikit.Cyan Gray = tuikit.Gray White = tuikit.White BadgePass = tuikit.BadgePass BadgePassed = tuikit.BadgePassed BadgeWarn = tuikit.BadgeWarn BadgeFail = tuikit.BadgeFail BadgeInfo = tuikit.BadgeInfo BadgeActive = tuikit.BadgeActive BadgeSync = tuikit.BadgeSync StripANSI = tuikit.StripANSI VisibleWidth = tuikit.VisibleWidth RenderStep = tuikit.RenderStep RenderBar = tuikit.RenderBar )
Styling and badge re-exports for rapid zero-dependency console formatting.
var ( // ErrUnknownCommand is returned when an unrecognized command name is invoked. ErrUnknownCommand = errors.New("ctrl: unknown command") // ErrMissingArgument is returned when required positional arguments are absent. ErrMissingArgument = errors.New("ctrl: missing required argument") // ErrFlagParsing is returned when flag decoding fails. ErrFlagParsing = errors.New("ctrl: flag parsing error") )
Standard error definitions for the ctrl framework.
Functions ¶
func ParseInterspersedFlags ¶
ParseInterspersedFlags parses a flag.FlagSet correctly with full POSIX semantics: clumping (-la), attached values (-I*.tmp), interspersed arguments, and typo suggestions.
Example ¶
ExampleParseInterspersedFlags demonstrates parsing POSIX flags and positional arguments.
package main
import (
"flag"
"fmt"
"github.com/lemon4ksan/ctrl"
)
func main() {
fs := flag.NewFlagSet("build", flag.ContinueOnError)
v := fs.Bool("v", false, "verbose")
out := fs.String("o", "", "output binary")
args := []string{"main.go", "-v", "-o=app.exe", "extra.go"}
posArgs, _ := ctrl.ParseInterspersedFlags(fs, args)
fmt.Printf("Verbose: %t\n", *v)
fmt.Printf("Output: %s\n", *out)
fmt.Printf("Positionals: %v\n", posArgs)
}
Output: Verbose: true Output: app.exe Positionals: [main.go extra.go]
Types ¶
type App ¶
App manages the lifecycle, subcommand routing, help formatting, and execution of CLI tools.
type Box ¶
Box formats enclosed text cards and panels with borders.
func NewBox ¶
NewBox creates a new rectangular box card with the given title and inner width constraint.
Example ¶
ExampleNewBox demonstrates creating an auto-sized bordered card panel.
package main
import (
"os"
"github.com/lemon4ksan/ctrl"
)
func main() {
card := ctrl.NewBox("Status Card", 28)
card.AddLine("Engine: OK")
_ = card.Render(os.Stdout)
}
Output:
type CommandGroup ¶
type CommandGroup = tuikit.CommandGroup
CommandGroup organizes subcommands under logical sections in help listings.
type FuncCommand ¶
type FuncCommand = tuikit.FuncCommand
FuncCommand is a lightweight adapter creating a Command from functional closures.
type Progress ¶
Progress manages live terminal feedback, throughput, ETA calculations, and progress rendering.
func NewProgress ¶
func NewProgress(opts ...tuikit.ProgressOption) *Progress
NewProgress creates and initializes a new Progress widget.
type Table ¶
Table manages columnar data formatting with guaranteed alignment.
func NewTable ¶
NewTable constructs a new Table with the specified header titles.
Example ¶
ExampleNewTable demonstrates creating an auto-aligned data table.
package main
import (
"os"
"github.com/lemon4ksan/ctrl"
)
func main() {
tbl := ctrl.NewTable("Method", "Route", "Latency")
tbl.AddRow("GET", "/health", "1.2 ms")
_ = tbl.Render(os.Stdout)
}
Output:
Directories
¶
| Path | Synopsis |
|---|---|
|
Package argkit provides a zero-dependency POSIX argument and flag parsing engine.
|
Package argkit provides a zero-dependency POSIX argument and flag parsing engine. |
|
Package tuikit provides a zero-dependency terminal UI toolkit for building CLI applications, REPLs, formatted tables, styled borders, badges, and progress indicators.
|
Package tuikit provides a zero-dependency terminal UI toolkit for building CLI applications, REPLs, formatted tables, styled borders, badges, and progress indicators. |