Documentation
¶
Overview ¶
Package click provides tiny helpers for building nested CLIs with root flags and optional passthrough arguments.
The package intentionally exposes a small API centered on App, Command, and Env, while keeping traversal and help internals private.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type App ¶
type App[T any] struct { // Name is used when constructing the root flag set. Name string // ConfigureRoot initializes shared root state before flags are parsed. ConfigureRoot func(*T) // ConfigureRootFlags registers flags shared across the whole command tree. ConfigureRootFlags func(*flag.FlagSet, *T) // Commands are the top-level commands available in the app. Commands []Command[T] // Stdout overrides the default command output writer. Stdout io.Writer // Stderr overrides the default error output writer. Stderr io.Writer }
Example ¶
package main
import (
"context"
"flag"
"fmt"
click "github.com/0xdsqr/go-click"
)
func main() {
type rootFlags struct {
Host string
Verbose bool
}
app := click.App[rootFlags]{
Name: "demo",
ConfigureRoot: func(root *rootFlags) {
root.Host = "local"
},
ConfigureRootFlags: func(fs *flag.FlagSet, root *rootFlags) {
fs.BoolVar(&root.Verbose, "v", false, "enable verbose output")
},
Commands: []click.Command[rootFlags]{
{
Name: "hello",
ConfigureFlags: func(fs *flag.FlagSet) {
fs.Bool("loud", false, "print loudly")
},
Run: func(_ context.Context, env click.Env[rootFlags], _ []string, _ []string) error {
fmt.Fprintln(env.Stdout, "hello from go-click")
return nil
},
},
},
}
if err := app.Run(context.Background(), []string{"hello"}); err != nil {
panic(err)
}
}
Output: hello from go-click
type Command ¶
type Command[T any] struct { // Name is the token users type to select this command. Name string // Description is a short help string shown in command listings. Description string // Usage overrides the default command path shown in help output. Usage string // Commands are this command's nested subcommands. Commands []Command[T] // ConfigureFlags registers flags for this specific command. ConfigureFlags func(*flag.FlagSet) // Passthrough allows args after `--` to be passed separately to Run. Passthrough bool // Run executes the command with normal and passthrough arguments. Run func(context.Context, Env[T], []string, []string) error }
Command describes one CLI command in the tree.
Click to show internal directories.
Click to hide internal directories.