Home / Market / golang-spf13-cobra

golang-spf13-cobra

Golang CLI command tree library using spf13/cobra — cobra.Command, RunE vs Run, PersistentPreRunE hook chain, Args validators (NoArgs, ExactArgs, MatchAll, custom), persistent vs local flags, command groups, ValidArgsFunction, RegisterFlagCompletionFunc, ShellCompDirective, usage/help template customization, man-page and markdown doc generation, and testing with SetArgs/SetOut/SetErr. Apply when using or adopting spf13/cobra, or when the codebase imports `github.com/spf13/cobra`. For configurati

skill★ 00 dlclawhubMIT-0Install in CosmosUpstream Source

Documentation & Preview

[ { "id": 1, "name": "rune-vs-run-error-propagation", "description": "Tests use of RunE instead of Run for error propagation", "prompt": "I'm writing a cobra subcommand in Go that calls an external API. If the API returns an error, the command should exit non-zero. Should I use Run or RunE?", "trap": "Without the skill, the model may say both work, suggest using Run with os.Exit(1), or not explain why Run is problematic. The correct answer is always RunE — it propagates the error through cobra's error handling chain.", "assertions": [ { "id": "1.1", "text": "Recommends RunE, not Run" }, { "id": "1.2", "text": "Explains that Run cannot return an error — you'd need os.Exit or panic" }, { "id": "1.3", "text": "Shows RunE returning the error from the handler" }, { "id": "1.4", "text": "Does NOT suggest using os.Exit inside RunE" }, { "id": "1.5", "text": "Mentions that returning error from RunE causes cobra to exit non-zero" } ] }, { "id": 2, "name": "args-validator-not-manual-check", "description": "Tests use of cobra Args validators instead of manual len(args) checks in RunE", "prompt": "I'm writing a Go CLI with cobra. My 'delete' command requires exactly one positional argument (the resource name). How should I validate this?", "trap": "Without the skill, the model writes len(args) != 1 check inside RunE. The correct approach is Args: cobra.ExactArgs(1) on the command definition, which validates before RunE runs and gives a standard error message.", "assertions": [ { "id": "2.1", "text": "Sets Args: cobra.ExactArgs(1) on the command struct" }, { "id": "2.2", "text": "Does NOT write len(args) check inside RunE" }, { "id": "2.3", "text": "Mentions that cobra prints a standard error message when validation fails" }, { "id": "2.4", "text": "RunE body accesses args[0] directly without re-validating length" } ] }, { "id": 3, "name": "outOrStdout-not-os-stdout", "description": "Tests use of cmd.OutOrStdout() instead of os.Stdout for testable output", "prompt": "I'm writing a cobra command in Go that prints a table of results to the terminal. How should I write to stdout from inside RunE?", "trap": "Without the skill, the model uses fmt.Println or os.Stdout directly. The correct approach is fmt.Fprintln(cmd.OutOrStdout(), ...) which can be redirected to a buffer in tests.", "assertions": [ { "id": "3.1", "text": "Uses cmd.OutOrStdout() as the io.Writer target" }, { "id": "3.2", "text": "Does NOT use os.Stdout directly" }, { "id": "3.3", "text": "Does NOT use fmt.Println (which hardcodes os.Stdout)" }, { "id": "3.4", "text": "Mentions testability as the reason — SetOut can redirect the writer in tests" } ] }, { "id": 4, "name": "persistent-prerunE-hook-chain", "description": "Tests PersistentPreRunE on root for global config init and the child override trap", "prompt": "In my Go CLI with cobra, I want to initialize viper config before any subcommand runs. I also have one subcommand that needs its own PersistentPreRunE for extra setup. How do I make sure both run?", "trap": "Without the skill, the model defines PersistentPreRunE on both root and child without noting that the child's hook replaces the parent's — so root's config init never runs for that subcommand.", "assertions": [ { "id": "4.1", "text": "Explains that a child's PersistentPreRunE replaces (not chains) the parent's" }, { "id": "4.2", "text": "Shows explicitly calling the parent's PersistentPreRunE from inside the child's hook" }, { "id": "4.3", "text": "Does NOT claim both hooks run automatically" }, { "id": "4.4", "text": "Uses PersistentPreRunE (the *E variant) not PersistentPreRun" } ] }, { "id": 5, "name": "silence-usage-and-errors", "description": "Tests SilenceUsage and SilenceErrors on root command", "prompt": "When my Go cobra CLI returns an error from RunE, the terminal shows the full usage/help text followed by the error. I only want to see the error message, not the usage. How do I fix this?", "trap": "Without the skill, the model may suggest overriding SetUsageTemplate or wrapping the error. The correct fix is SilenceUsage: true on the root command.", "assertions": [ { "id": "5.1", "text": "Sets SilenceUsage: true on the root cobra.Command" }, { "id": "5.2", "text": "Optionally mentions SilenceErrors: true (for custom error formatting)" }, { "id": "5.3", "text": "Does NOT suggest removing or wrapping the error in RunE" }, { "id": "5.4", "text": "Explains that SilenceUsage only suppresses usage on error, not on --help" } ] }, { "id": 6, "name": "command-group-registration-order", "description": "Tests that AddGroup must be called before AddCommand that references it", "prompt": "I want to group my cobra subcommands in the help output under labels like 'Core Commands:' and 'Management Commands:'. How do I set this up?", "trap": "Without the skill, the model calls AddCommand first and AddGroup after, which doesn't work — groups must be registered before the commands that reference them.", "assertions": [ { "id": "6.1", "text": "Calls AddGroup before AddCommand for commands that use that group" }, { "id": "6.2", "text": "Sets GroupID on the subcommand matching the Group's ID field" }, { "id": "6.3", "text": "Shows cobra.Group{ID: ..., Title: ...} struct" }, { "id": "6.4", "text": "Does NOT call AddCommand before AddGroup for the same group" } ] }, { "id": 7, "name": "valid-args-function-dynamic-completion", "description": "Tests ValidArgsFunction for dynamic shell completion instead of static ValidArgs", "prompt": "My Go cobra 'get pod' command should complete pod names dynamically by querying the API server. ValidArgs only accepts a static list. How do I provide dynamic completions?", "trap": "Without the skill, the model tries to populate ValidArgs at startup (querying the API at init time) or doesn't know about ValidArgsFunction.", "assertions": [ { "id": "7.1", "text": "Uses ValidArgsFunction (not ValidArgs) for dynamic completions" }, { "id": "7.2", "text": "Function signature returns ([]string, cobra.ShellCompDirective)" }, { "id": "7.3", "text": "Returns cobra.ShellCompDirectiveNoFileComp to prevent file fallback" }, { "id": "7.4", "text": "Does NOT query the API at init() or in ValidArgs (static list)" }, { "id": "7.5", "text": "Handles errors by returning cobra.ShellCompDirectiveError" } ] }, { "id": 8, "name": "register-flag-completion-func", "description": "Tests RegisterFlagCompletionFunc for flag value completion", "prompt": "My Go cobra command has an --output flag that accepts 'json', 'yaml', or 'table'. How do I make the shell complete valid values when the user types --output <TAB>?", "trap": "Without the skill, the model does not know about RegisterFlagCompletionFunc and instead documents the valid values only in the flag description string.", "assertions": [ { "id": "8.1", "text": "Calls cmd.RegisterFlagCompletionFunc(\"output\", func(...) ...)" }, { "id": "8.2", "text": "The completion function returns []string{\"json\", \"yaml\", \"table\"} (or similar)" }, { "id": "8.3", "text": "Returns cobra.ShellCompDirectiveNoFileComp" }, { "id": "8.4", "text": "Does NOT rely only on the flag usage string for user guidance" } ] }, { "id": 9, "name": "test-isolation-fresh-root", "description": "Tests that a fresh command tree must be created per test to avoid flag state leakage", "prompt": "I'm writing tests for my Go cobra CLI. My first test runs 'myapp serve --port 9090' and passes. My second test runs 'myapp serve' without --port and expects the default 8080, but gets 9090. What's wrong and how do I fix it?", "trap": "Without the skill, the model may suggest resetting the flag value manually or calling ResetFlags(). The correct fix is to create a fresh command tree per test.", "assertions": [ { "id": "9.1", "text": "Identifies the root cause as reusing the same cobra.Command instance across tests" }, { "id": "9.2", "text": "Recommends building a new command tree per test (constructor function)" }, { "id": "9.3", "text": "Shows a newRootCmd() or similar factory function pattern" }, { "id": "9.4", "text": "Does NOT suggest ResetFlags() as the primary solution" }, { "id": "9.5", "text": "Each test calls the factory to get a fresh *cobra.Command" } ] }, { "id": 10, "name": "match-all-validator-composition", "description": "Tests MatchAll for composing multiple arg validators", "prompt": "My Go cobra 'apply' command needs positional args that are all valid resource names (from a known list) AND there must be at least one. How do I express both constraints?", "trap": "Without the skill, the model writes a custom validator function that manually checks both conditions with if statements. MatchAll composes built-in validators without custom code.", "assertions": [ { "id": "10.1", "text": "Uses cobra.MatchAll to compose validators" }, { "id": "10.2", "text": "Combines cobra.MinimumNArgs(1) (or ExactArgs) with cobra.OnlyValidArgs" }, { "id": "10.3", "text": "Sets ValidArgs with the known resource names" }, { "id": "10.4", "text": "Does NOT write a fully manual validator function for the combined check" } ] }, { "id": 11, "name": "cobra-vs-viper-distinction", "description": "Tests understanding of what cobra does vs what viper does", "prompt": "I'm starting a Go CLI project. I need subcommands, flags, shell completions, AND the ability to read configuration from a YAML file and environment variables. I've heard of cobra and viper. Which library handles which concern?", "trap": "Without the skill, the model may conflate the two or understate how they integrate. The correct answer clearly assigns cobra=command tree/flags/completions and viper=layered config resolution, with BindPFlag as the integration seam.", "assertions": [ { "id": "11.1", "text": "Assigns cobra to command tree, flags, arg validation, shell completions" }, { "id": "11.2", "text": "Assigns viper to config file, env var, and layered value resolution" }, { "id": "11.3", "text": "Identifies BindPFlag (or similar) as the integration seam between them" }, { "id": "11.4", "text": "Explains they can be used independently (cobra without viper, or viper without cobra)" }, { "id": "11.5", "text": "Does NOT say cobra reads config files or viper defines subcommands" } ] }, { "id": 12, "name": "cobra-cli-scaffolder", "description": "Tests knowledge of the cobra-cli scaffolding tool", "prompt": "I want to quickly scaffold a new Go CLI project with cobra. Is there a tool that generates the initial files and lets me add subcommands from the command line?", "trap": "Without the skill, the