chore(cli): align exported API docs with usage examples
Some checks are pending
Security Scan / security (push) Waiting to run

This commit is contained in:
Virgil 2026-04-02 17:10:02 +00:00
parent 8b345eba5b
commit 050ee5bd8f
3 changed files with 87 additions and 3 deletions

View file

@ -34,9 +34,16 @@ var (
) )
// SemVer returns the full SemVer 2.0.0 version string. // SemVer returns the full SemVer 2.0.0 version string.
// - Release: 1.2.0 //
// - Pre-release: 1.2.0-dev.8 // Examples:
// - Full: 1.2.0-dev.8+df94c24.20260206 // // Release only:
// // AppVersion=1.2.0 -> 1.2.0
// cli.AppVersion = "1.2.0"
// fmt.Println(cli.SemVer())
//
// // Pre-release + commit + date:
// // AppVersion=1.2.0, BuildPreRelease=dev.8, BuildCommit=df94c24, BuildDate=20260206
// // -> 1.2.0-dev.8+df94c24.20260206
func SemVer() string { func SemVer() string {
v := AppVersion v := AppVersion
if BuildPreRelease != "" { if BuildPreRelease != "" {
@ -64,19 +71,37 @@ func WithAppName(name string) {
type LocaleSource = i18n.FSSource type LocaleSource = i18n.FSSource
// WithLocales returns a locale source for use with MainWithLocales. // WithLocales returns a locale source for use with MainWithLocales.
//
// Example:
// fs := embed.FS{}
// locales := cli.WithLocales(fs, "locales")
// cli.MainWithLocales([]cli.LocaleSource{locales})
func WithLocales(fsys fs.FS, dir string) LocaleSource { func WithLocales(fsys fs.FS, dir string) LocaleSource {
return LocaleSource{FS: fsys, Dir: dir} return LocaleSource{FS: fsys, Dir: dir}
} }
// CommandSetup is a function that registers commands on the CLI after init. // CommandSetup is a function that registers commands on the CLI after init.
//
// Example:
// cli.Main(
// cli.WithCommands("doctor", doctor.AddDoctorCommands),
// )
type CommandSetup func(c *core.Core) type CommandSetup func(c *core.Core)
// Main initialises and runs the CLI with the framework's built-in translations. // Main initialises and runs the CLI with the framework's built-in translations.
//
// Example:
// cli.WithAppName("core")
// cli.Main(config.AddConfigCommands)
func Main(commands ...CommandSetup) { func Main(commands ...CommandSetup) {
MainWithLocales(nil, commands...) MainWithLocales(nil, commands...)
} }
// MainWithLocales initialises and runs the CLI with additional translation sources. // MainWithLocales initialises and runs the CLI with additional translation sources.
//
// Example:
// locales := []cli.LocaleSource{cli.WithLocales(embeddedLocales, "locales")}
// cli.MainWithLocales(locales, doctor.AddDoctorCommands)
func MainWithLocales(locales []LocaleSource, commands ...CommandSetup) { func MainWithLocales(locales []LocaleSource, commands ...CommandSetup) {
// Recovery from panics // Recovery from panics
defer func() { defer func() {

View file

@ -29,6 +29,13 @@ func WithCommands(name string, register func(root *Command), localeFS ...fs.FS)
} }
// CommandRegistration is a function that adds commands to the CLI root. // CommandRegistration is a function that adds commands to the CLI root.
//
// Example:
// func addCommands(root *cobra.Command) {
// root.AddCommand(cli.NewRun("ping", "Ping API", "", func(cmd *cli.Command, args []string) {
// cli.Println("pong")
// }))
// }
type CommandRegistration func(root *cobra.Command) type CommandRegistration func(root *cobra.Command)
var ( var (
@ -44,6 +51,13 @@ var (
// func init() { // func init() {
// cli.RegisterCommands(AddCommands, locales.FS) // cli.RegisterCommands(AddCommands, locales.FS)
// } // }
//
// Example:
// cli.RegisterCommands(func(root *cobra.Command) {
// root.AddCommand(cli.NewRun("version", "Show version", "", func(cmd *cli.Command, args []string) {
// cli.Println(cli.SemVer())
// }))
// })
func RegisterCommands(fn CommandRegistration, localeFS ...fs.FS) { func RegisterCommands(fn CommandRegistration, localeFS ...fs.FS) {
registeredCommandsMu.Lock() registeredCommandsMu.Lock()
registeredCommands = append(registeredCommands, fn) registeredCommands = append(registeredCommands, fn)
@ -102,6 +116,11 @@ func loadLocaleSources(sources ...LocaleSource) {
} }
// RegisteredLocales returns all locale filesystems registered by command packages. // RegisteredLocales returns all locale filesystems registered by command packages.
//
// Example:
// for _, fs := range cli.RegisteredLocales() {
// _ = fs
// }
func RegisteredLocales() []fs.FS { func RegisteredLocales() []fs.FS {
registeredCommandsMu.Lock() registeredCommandsMu.Lock()
defer registeredCommandsMu.Unlock() defer registeredCommandsMu.Unlock()
@ -114,6 +133,11 @@ func RegisteredLocales() []fs.FS {
} }
// RegisteredCommands returns an iterator over the registered command functions. // RegisteredCommands returns an iterator over the registered command functions.
//
// Example:
// for attach := range cli.RegisteredCommands() {
// _ = attach
// }
func RegisteredCommands() iter.Seq[CommandRegistration] { func RegisteredCommands() iter.Seq[CommandRegistration] {
return func(yield func(CommandRegistration) bool) { return func(yield func(CommandRegistration) bool) {
registeredCommandsMu.Lock() registeredCommandsMu.Lock()

View file

@ -39,6 +39,12 @@ type runtime struct {
} }
// Options configures the CLI runtime. // Options configures the CLI runtime.
//
// Example:
// opts := cli.Options{
// AppName: "core",
// Version: "1.0.0",
// }
type Options struct { type Options struct {
AppName string AppName string
Version string Version string
@ -52,6 +58,11 @@ type Options struct {
// Init initialises the global CLI runtime. // Init initialises the global CLI runtime.
// Call this once at startup (typically in main.go or cmd.Execute). // Call this once at startup (typically in main.go or cmd.Execute).
//
// Example:
// err := cli.Init(cli.Options{AppName: "core"})
// if err != nil { panic(err) }
// defer cli.Shutdown()
func Init(opts Options) error { func Init(opts Options) error {
var initErr error var initErr error
once.Do(func() { once.Do(func() {
@ -141,6 +152,11 @@ func RootCmd() *cobra.Command {
// Execute runs the CLI root command. // Execute runs the CLI root command.
// Returns an error if the command fails. // Returns an error if the command fails.
//
// Example:
// if err := cli.Execute(); err != nil {
// cli.Warn("command failed:", "err", err)
// }
func Execute() error { func Execute() error {
mustInit() mustInit()
return instance.root.Execute() return instance.root.Execute()
@ -149,6 +165,13 @@ func Execute() error {
// Run executes the CLI and watches an external context for cancellation. // Run executes the CLI and watches an external context for cancellation.
// If the context is cancelled first, the runtime is shut down and the // If the context is cancelled first, the runtime is shut down and the
// command error is returned if execution failed during shutdown. // command error is returned if execution failed during shutdown.
//
// Example:
// ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
// defer cancel()
// if err := cli.Run(ctx); err != nil {
// cli.Error(err.Error())
// }
func Run(ctx context.Context) error { func Run(ctx context.Context) error {
mustInit() mustInit()
if ctx == nil { if ctx == nil {
@ -174,6 +197,10 @@ func Run(ctx context.Context) error {
// RunWithTimeout returns a shutdown helper that waits for the runtime to stop // RunWithTimeout returns a shutdown helper that waits for the runtime to stop
// for up to timeout before giving up. It is intended for deferred cleanup. // for up to timeout before giving up. It is intended for deferred cleanup.
//
// Example:
// stop := cli.RunWithTimeout(5 * time.Second)
// defer stop()
func RunWithTimeout(timeout time.Duration) func() { func RunWithTimeout(timeout time.Duration) func() {
return func() { return func() {
if timeout <= 0 { if timeout <= 0 {
@ -197,12 +224,20 @@ func RunWithTimeout(timeout time.Duration) func() {
// Context returns the CLI's root context. // Context returns the CLI's root context.
// Cancelled on SIGINT/SIGTERM. // Cancelled on SIGINT/SIGTERM.
//
// Example:
// if ctx := cli.Context(); ctx != nil {
// _ = ctx
// }
func Context() context.Context { func Context() context.Context {
mustInit() mustInit()
return instance.ctx return instance.ctx
} }
// Shutdown gracefully shuts down the CLI. // Shutdown gracefully shuts down the CLI.
//
// Example:
// cli.Shutdown()
func Shutdown() { func Shutdown() {
if instance == nil { if instance == nil {
return return