普通 CLI 工具

无状态一次性 CLI 工具的开发指南 — WinClaw CLI 工具生态中最常见、最简单的类型。

普通 CLI 是最常见的类型,适用于一次性、无状态的操作。每次调用都是独立的:接收输入,执行操作,返回结果,然后退出。

阅读本页前,请先了解适用于所有工具类型的通用规范

适用场景

  • 发送消息(agent_feishu)
  • 文件格式转换(markdown2word, markdown2ppt)
  • 数据导出(mysql_meta_exporter, pg_meta_exporter)
  • 文档操作(agent_excel, agent_word)
  • 搜索查询(redis_cli)

架构特点

┌─────────┐    参数    ┌───────────┐    结果    ┌─────────┐
│  调用方  │  ──────►  │  CLI 工具  │  ──────►  │  输出   │
│ (AI/用户)│           │  执行+退出 │           │ (JSON)  │
└─────────┘           └───────────┘           └─────────┘

每次调用完全独立,不需要管理进程生命周期或保存状态。这是最简单、最容易组合的工具类型。

项目结构

agent_feishu/
├── main.go
├── go.mod
├── cmd/
│   ├── root.go           # 根命令
│   ├── skill.go          # --skill 输出
│   ├── send_text.go      # 发送文本子命令
│   ├── send_image.go     # 发送图片子命令
│   ├── search_users.go   # 搜索用户子命令
│   └── initapi.go        # 初始化凭证子命令
└── internal/
    └── output/
        └── output.go     # 统一输出

示例:agent_feishu

var rootCmd = &cobra.Command{
    Use:   "agent_feishu",
    Short: "A CLI tool for Feishu bot operations, designed for AI agents",
    Long: `agent_feishu is a command-line tool that provides Feishu (Lark) bot operations...

Quick Start:
  agent_feishu initapi --app-id "cli_xxx" --app-secret "xxx"
  agent_feishu send-text --receive-id "ou_xxx" --receive-type "open_id" --text "Hello!"

Use 'agent_feishu --skill' or 'agent_feishu skill' for detailed command specifications.`,
    Version: version,
    Run: func(cmd *cobra.Command, args []string) {
        if showSkill {
            fmt.Print(skillSpec)
            return
        }
        cmd.Help()
    },
}

func init() {
    rootCmd.CompletionOptions.DisableDefaultCmd = true
    rootCmd.PersistentFlags().BoolVar(&jsonOutput, "json", false,
        "Output in JSON format: {success, data, error}")
    rootCmd.Flags().BoolVar(&showSkill, "skill", false,
        "Show the detailed command specification for agent_feishu")
}

开发检查清单

  • 根命令有 --help(Long 描述含功能概述、Quick Start、命令列表)
  • 根命令有 --skill 标志和 skill 子命令
  • 每个子命令有完善的 --help(含参数描述和示例)
  • 支持 --json 全局标志
  • 输出格式遵循 {success, data, error} 规范
  • version 子命令或 --version 标志