Files
magpie/internal/cli/help_cli.go
T
yetone 23e764d0e0 cli: magpie's commands move from the root into internal/cli; the root keeps only main.go
The root held 89 .go files (30 commands, 59 tests). They move as they are
to internal/cli (package cli), with git mv so history follows them. The
only code change is main() becoming cli.Main(version): the root's new
main.go keeps `var version` in package main, so -ldflags "-X
main.version=…" in the Makefile, the Dockerfile and magpie-releases, and
every workflow, work unchanged. go-winres's rsrc_windows_*.syso still land
next to package main in the root.

Tests that read testdata by a root-relative path now go up one level
(../plugin/testdata/fake, ../sessions/testdata). Five subsystem doc links
point at internal/cli/.

Run:
- go build ./..., gofmt -l, go vet on darwin, linux and windows (nogui)
- builds with tags production, dev, "production nogui"; windows
  -H windowsgui; linux CGO_ENABLED=0
- make release-cli TARGETS="darwin/arm64 linux/amd64 windows/amd64
  android/arm64" VERSION=9.9.9-tidy: all four built, binary prints
  "magpie 9.9.9-tidy"
- docker build --build-arg VERSION=7.7.7-tidy: container prints
  "magpie 7.7.7-tidy"
- go test ./internal/cli (default and nogui): 152 tests, the same count
  as the root on main
- go test ./...: all packages pass (agent, gateway, library, provider
  timed out at 10m under other sessions' load and passed rerun alone
  with -timeout 40m; none of them changed)
- the built binary in a sandbox HOME: version, help, ls, providers,
  claude, and an unknown command's error
2026-10-12 20:40:22 +08:00

136 lines
3.7 KiB
Go

package cli
import (
"fmt"
"strconv"
"strings"
"github.com/yetone/magpie/internal/agent"
)
// helpAsked is whether args ask for help rather than for the command to
// run: -h or --help anywhere, or help as the word after the command. It
// is looked at before run does anything, since a command that took
// --help for a value acted on it: `magpie snow --help` set Snow's model
// to "--help", which took magpie out of it (#1457), and `magpie serve
// --help` started a gateway.
func helpAsked(args []string) bool {
// what magpie is started with by others (Claude Code's MCP helper, the
// Agents page's dry run, a link, Windows' -Embedding) never carries
// an exact -h, --help or help
for _, a := range args {
if a == "-h" || a == "--help" {
return true
}
}
return len(args) > 1 && args[1] == "help"
}
// helpFor is what `magpie <command> --help` prints: the command's own
// usage where it has one, its lines of magpie's usage otherwise, an
// agent's forms and fields for an agent, and the whole usage for
// anything else.
func helpFor(args []string) string {
cmd := args[0]
switch cmd {
case "-h", "--help", "help":
return usage
case "model":
return modelUsage
case "group", "groups":
if len(args) > 1 && (args[1] == "rule" || args[1] == "rules") {
return ruleUsage
}
return groupUsage
case "library", "lib":
return " " + libraryUsage
case "plugin", "plugins":
return pluginUsage
case "quota", "quotas":
return quotaUsage
case "sessions":
return sessionsUsage
case "search":
return searchUsage
case "visible":
return visibleUsage
case "webdav", "dav":
return webdavUsage
case "s3":
return s3Usage
case "claude-code":
return claudeCodeUsage
case "list":
cmd = "ls"
case "account":
cmd = "accounts"
case "app", "gui":
cmd = ""
}
if cmd != "" && !strings.HasPrefix(cmd, "-") {
if lines := usageLines(cmd); lines != "" {
return lines
}
}
if a, err := agent.Find(cmd); err == nil && cmd != "" {
return agentHelp(a)
}
return usage
}
// usageLines are the lines of magpie's usage for `magpie <cmd>`, with the
// lines that carry on from them.
func usageLines(cmd string) string {
var b strings.Builder
in := false
for _, l := range strings.Split(usage, "\n") {
if rest, ok := strings.CutPrefix(l, " magpie "); ok {
in = rest == cmd || strings.HasPrefix(rest, cmd+" ")
} else if !strings.HasPrefix(l, " ") {
in = false
}
if in {
b.WriteString(l + "\n")
}
}
return b.String()
}
// agentHelp is `magpie <agent> --help`: the forms that set it, and its
// fields.
func agentHelp(a *agent.Agent) string {
var b strings.Builder
id := a.ID
fmt.Fprintf(&b, "%s\n\n", a.Name)
fmt.Fprintf(&b, " magpie %s show it\n", id)
if len(a.Fields) == 0 {
return b.String()
}
fmt.Fprintf(&b, " magpie %s <model> set its model (magpie models %s lists them)\n", id, id)
fmt.Fprintf(&b, " magpie %s <field> <value> set another field\n", id)
fmt.Fprintf(&b, " magpie %s default take magpie out: back on what it had before\n", id)
fmt.Fprintf(&b, " magpie %s <field> default that field back to its own default\n", id)
var keys []string
for _, f := range a.Fields {
keys = append(keys, f.Key)
}
fmt.Fprintf(&b, "\nfields: %s\n", strings.Join(keys, ", "))
return b.String()
}
// flagArg is an argument to `magpie <agent> …` that is a flag, which no
// agent form takes: it is said to be one, rather than set as a model or a
// field's value. A negative number is a value.
func flagArg(args []string) string {
for _, a := range args {
if !strings.HasPrefix(a, "-") {
continue
}
if _, err := strconv.ParseFloat(a, 64); err == nil {
continue
}
return a
}
return ""
}