Skip to content
1 Star

From flags to shutdown, type-safe.

The only Go CLI framework that unifies type-safe flags, dependency injection with lifecycle management, and zero-panic error contracts. Wraps Cobra, so you keep full compatibility and lose the footguns.

Get Started →
$ go get github.com/larsartmann/cmdguard/v5
❯ main.go
func main() {
    cli, err := v5.NewCLI[AppConfig]("myapp", "My CLI", AppConfig{}) // batteries ON by default
    if err != nil {
        log.Fatal(err)
    }

    // Register a service (lazy, lifecycle-managed)
    err = v5.Provide(cli.Scope(), func(i do.Injector) (*Database, error) {
        return &Database{DSN: "postgres://..."}, nil
    })
    if err != nil {
        log.Fatal(err)
    }

    query, err := v5.NewCommand("query", v5.NoFlags{},
        func(ctx context.Context, cfg *AppConfig, _ v5.NoFlags) error {
            db, invokeErr := v5.Invoke[*Database](cli.Scope())
            if invokeErr != nil {
                return invokeErr
            }

            return db.Query(ctx)
        },
        v5.WithShort("Query the database"),
    )

    b := v5.NewBuilder(cli)
    v5.Try(b, query, err) // collect constructor errors, no ladder
    if buildErr := b.Build(); buildErr != nil {
        log.Fatal(buildErr)
    }

    cli.ExecuteAndExit(context.Background()) // zero panics, correct exit code
}

The full production CLI stack.

Flags, DI, lifecycle, output, error handling, and the machine contract: all unified, all validated at construction.

Batteries On By Default

Strict validation, signal handling, graceful DI shutdown, strict config keys, error hints, and doctor/version are enabled at NewCLI. Each has a named opt-out.

Type-Safe Flags

Struct tags define the flag name, shorthand, default, env var, required state, enum values, counting, scoping, validation, and secret redaction. No string lookups.

Machine Contract

Every CLI answers describe with a versioned JSON contract: commands, flags, env vars, enums, required flags, and inheritance. Built for agents and tooling.

Dependency Injection

Lazy services, lifecycle hooks, health checks, and dependency-order shutdown on a signal. Powered by samber/do/v2.

Zero Panics, Real Errors

Every constructor returns an error. Classified errors print a Hint line, carry a stable error_code, and map to the right exit code.

Config You Can Explain

JSON, YAML, and TOML with flag > env > file > default precedence. Unknown keys fail with a suggestion, and config explain shows where each value came from.

16 Output Formats

Table, JSON, CSV, TSV, YAML, TOML, Markdown, XML, HTML, tree, D2, DOT, Mermaid, PlantUML, JSONL, and AsciiDoc via go-output.

Cobra Escape Hatch

Keep raw cobra.Command subcommands beside typed ones. Gradual migration, no rewrite, no lock-in.

Strict Positional Args

Since v5.1.0: WithStrictPositionalArgs rejects undeclared positional arguments, and WithArgsPassthrough re-accepts them explicitly. No more silent extras.

No other Go CLI framework covers this stack.

Type-safe flags exist elsewhere. DI lifecycle, a versioned machine contract, and zero-panic error contracts do not ship together anywhere else.

Capability CobraKongurfave/clicmdguard
Struct-tag flags
String lookups
Yes
String lookups
Validated at construction
Dependency injection
No
No
No
Lazy services, lifecycle, health checks
Graceful shutdown
Manual signal handling
Manual signal handling
Manual signal handling
Reverse-order on SIGINT/SIGTERM
Zero panics
Run panics, RunE returns
Some panic variants
Some panic variants
Only error returns, by construction
Error printing
Prints twice if not careful
Manual
Manual
Exactly once, correct exit code
Error hints and codes
No
No
No
Hint line plus stable error_code
Machine contract
No
No
No
describe JSON, versioned
Usage on error
Prints full usage (footgun)
Configurable
Configurable
Silenced by default
Styled output
Plain by default
Plain by default
Plain by default
fang + lipgloss by default
Cobra compatibility
Native
No
No
Escape hatch + gradual migration

Built for real-world CLIs.

From developer tools to production microservices.

Production CLIs

Ship robust command-line tools with proper error handling and exit codes out of the box.

DevTools

Build developer utilities with typed flags, config loading, and rich output formats.

Microservice CLIs

Wire up DI services, health checks, graceful shutdown, and telemetry in one framework.

Start building in seconds.

Three lines to a type-safe CLI with proper error handling and exit codes.