add-command
Scaffold a new Cobra subcommand in a starterpack-go-cli project (flags, help text, unit + integration tests, registration). Use when asked to add a CLI command, subcommand, or "a new `<tool> <verb>`" entry point.
- License 许可
- MIT
- 最近更新
- 2026-10-07
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- add-command
- description
- Scaffold a new Cobra subcommand in a starterpack-go-cli project (flags, help text, unit + integration tests, registration). Use when asked to add a CLI command, subcommand, or "a new `<tool> <verb>`" entry point.
03 SKILL.md 正文
Add a Cobra command
Outcome
./<binary> <name> --help shows the new command; it's wired into rootCmd and
covered by an integration test.
Contract
- One file per command:
cmd/<name>.go, packagecmd. var <name>Cmd = &cobra.Command{...}withUse,Short, andLongall set —Shortis a one-line summary (shown in--helplistings),Longcan be multi-line.- Prefer
RunE func(cmd *cobra.Command, args []string) erroroverRun— return wrapped errors (fmt.Errorf("...: %w", err)) instead of callingos.Exit; Cobra's top-levelExecute()incmd/root.goalready formats the error viacolor.Errorand exits 1. - Validate positional args with a Cobra
Argsvalidator (cobra.ExactArgs(1),cobra.MaximumNArgs(1),cobra.RangeArgs(min, max), ...) rather than hand-rolledlen(args)checks. - Command-specific flags:
<name>Cmd.Flags().<Type>VarP(&var, "flag", "f", default, "help text")insideinit(). Flags shared across every command belong onrootCmd.PersistentFlags()incmd/root.go, not duplicated per command. - User-facing output via
internal/color(color.Success/Error/Warning/Info), not rawfmt.Println. Debug/trace detail viainternal/logger(logger.Debug/Info/Warn/Error), which already respects the global--verbose/--log-levelflags. - Business logic that doesn't need Cobra/flags/terminal output goes in
pkg/(orinternal/if it's CLI-specific plumbing, not a reusable library) and gets called fromRunE— keep the command file itself thin. - Register with
rootCmd.AddCommand(<name>Cmd)inside the same file'sinit().
Steps
- Create
cmd/<name>.gofollowing the pattern above.cmd/greet.gois the simplest worked example (one required arg, one flag, calls intopkg/example);cmd/calc.goshows an enum-style flag (--operation) with validation;cmd/process.goshows the spinner pattern for a long-running operation. - If the command needs new business logic, add it to
pkg/<name>/(reusable outside this CLI) orinternal/<name>/(CLI-specific) — see theadd-packageskill — rather than inlining it incmd/<name>.go. - Add a unit test only if the command file itself has non-trivial logic beyond wiring (flag parsing helpers, output formatting). Most command-level testing belongs in the integration suite instead.
- Add integration test cases to
tests/integration_test.go: run the built/go runCLI viaos/exec, capture stdout/stderr, assert exit code and expected output substrings — for both the success path and at least one failure path (missing arg, invalid flag value). Follow the existing table-driven style in that file. gofmt -l cmd/<name>.goandtask test(orgo test ./...) before calling it done.- Update
README.md's command list and "Available Commands" examples if this is a user-visible command (skip for internal/hidden commands).
04 同仓库更多 Skills
agentworks-evals
How to write and run behavior-eval cases for a skill or agent in this AgentWorks project: the evals/*.yaml format, text, tool-call, and activation assertions, trigger cases, rubrics and a judge, runner protocols (text and json), repeated runs, and agentworks eval. Use when asked to test, evaluate, or regression-check how a skill or agent responds, or whether its description makes a model choose it.
agentworks-cli
How to scaffold, validate, build, test, run, export, import, and publish this project's agents, skills, MCP servers, and hooks with the agentworks CLI. Use whenever asked to add, change, check, ship, or import an artifact in this project.
agentworks-author-skill
How to write or edit a skill in this AgentWorks project: skill.md frontmatter, the description that decides when it triggers, entrypoint/scripts/tests layout, and build/test commands. Use when creating or changing anything under skills/.
agentworks-author-mcp
How to write or edit an MCP server in this AgentWorks project: mcp.md frontmatter, local (stdio) versus remote (http/sse) servers, command, args, env and auth, source and tests layout, and the build, test, doctor, and run loop. Use when creating or changing anything under mcp/.
agentworks-author-hook
How to write or edit a lifecycle hook in this AgentWorks project: hook.md frontmatter, choosing vendor-specific event names, matchers and timeouts, the command it runs, and safety review. Use when creating or changing anything under hooks/.
agentworks-author-agent
How to write or edit an agent (subagent) in this AgentWorks project: agent.md frontmatter, portable tools and model settings, writing the instructions, and resources. Use when creating or changing anything under agents/.