Register Cobra Command Examples
This example demonstrates the four different ways to register commands with Cobra in the Glazed framework:
1. Bare Command (bare)
A simple command that implements only the BareCommand interface:
- Outputs text directly to stdout
- Uses
cli.BuildCobraCommand
go run . bare --message "Hello World!"
2. Writer Command (writer)
A command that implements the WriterCommand interface:
- Outputs to a specified
io.Writer
- Uses
cli.BuildCobraCommand
go run . writer --count 3
3. Glaze Command (glaze)
A command that implements the GlazeCommand interface:
- Outputs structured data through the Glaze processor
- Uses
cli.BuildCobraCommand
- Supports all Glaze output formatting options
# Table format (default)
go run . glaze --rows 3
# JSON format
go run . glaze --rows 3 --output json
# CSV format
go run . glaze --rows 3 --output csv
4. Dual Command (dual) - NEW!
A command that implements both BareCommand and GlazeCommand interfaces:
- Can run in both classic and glaze modes
- Uses
cli.BuildCobraCommand with cli.WithDualMode(true)
- Mode is controlled by the
--with-glaze-output flag
# Classic mode (default)
go run . dual --name "Manuel" --times 2
# Glaze mode with table output
go run . dual --name "Manuel" --times 2 --with-glaze-output
# Glaze mode with JSON output
go run . dual --name "Manuel" --times 2 --with-glaze-output --output json
Key Features of the Dual Command
- Single Registration: One command, registered once, with dual functionality
- Flag Management: Glaze flags are automatically injected when needed
- Clean Help: By default, only shows the toggle flag, keeping help clean
- Backward Compatible: Existing command builders remain unchanged
- Flexible: Supports all existing Glaze output formats when in glaze mode
Customization Options
Dual-mode registration supports several options:
cobraDualCmd, err := cli.BuildCobraCommand(
dualCmd,
cli.WithDualMode(true),
cli.WithGlazeToggleFlag("custom-flag-name"), // Rename the toggle flag
cli.WithHiddenGlazeFlags("output", "format"), // Keep specific flags hidden
cli.WithDefaultToGlaze(), // Make glaze mode the default
)