Files
openchamber-android-native/third_party/mygo/internal/automationcli/args.go
T
OpenChamber 6c0ee61750 feat: port the client to MyGo native UI (0.3.0)
Switch from Asuka to MyGo, keeping the API client and the feature set:

- servers: saved servers, passwordless and password login, proxy,
  self-signed certificates, per-server device tokens (Keystore), connect
  form, instance list sheet with switch/disconnect
- sessions: virtualized list, search, new session, rename/archive/delete,
  running indicator, event stream keeps it live
- chat: pushed window with system back bar, streaming Markdown via
  ui/markdown, reasoning and tool rows with image previews, footer menu
  (read aloud via a Kotlin TTS plugin, copy, fork), user message menu
  (copy, revert, fork), context usage ring and detail sheet, attachments
  (photos/files via the document picker), permissions
- release builds allow plain http:// LAN servers (Go's net/http dials
  directly, no cleartext manifest needed)
- a local fork of MyGo (third_party/mygo) lowers the Android host's
  minSdk to 24 (Android 7.0); Go 1.27.4 via mise

Version 0.3.0.
2026-11-18 13:22:59 +08:00

85 lines
2.1 KiB
Go

package automationcli
import (
"fmt"
"strings"
)
// usageError is a command line the command does not take.
type usageError struct{ msg string }
func (e *usageError) Error() string { return e.msg }
func usagef(format string, args ...any) error {
return &usageError{fmt.Sprintf(format, args...)}
}
// args are a command's arguments: its positional ones and its flags.
type args struct {
pos []string
bools map[string]bool
vals map[string]string
}
// flagSpec lists the flags a command takes: true for a flag with a
// value, false for a switch.
type flagSpec map[string]bool
// aliases are the short spellings of flags.
var aliases = map[string]string{"i": "interactive"}
// parseArgs parses a command's arguments, with flags anywhere among them
// (press @e2 --settle), written -f, --f, --f=v or --f v. "--" ends the
// flags; a negative number, as wheel's -120, and "-" alone are
// positional.
func parseArgs(in []string, spec flagSpec) (*args, error) {
a := &args{bools: map[string]bool{}, vals: map[string]string{}}
for i := 0; i < len(in); i++ {
s := in[i]
if s == "--" {
a.pos = append(a.pos, in[i+1:]...)
break
}
if len(s) < 2 || s[0] != '-' || (s[1] >= '0' && s[1] <= '9') || s[1] == '.' {
a.pos = append(a.pos, s)
continue
}
name := strings.TrimLeft(s, "-")
val, hasVal := "", false
if k, v, ok := strings.Cut(name, "="); ok {
name, val, hasVal = k, v, true
}
if long, ok := aliases[name]; ok {
name = long
}
takesValue, ok := spec[name]
if !ok {
return nil, usagef("unknown flag %s", s)
}
switch {
case takesValue && !hasVal:
if i+1 >= len(in) {
return nil, usagef("flag --%s needs a value", name)
}
i++
a.vals[name] = in[i]
case takesValue:
a.vals[name] = val
case hasVal:
return nil, usagef("flag --%s takes no value", name)
default:
a.bools[name] = true
}
}
return a, nil
}
// want checks that the command got between min and max positional
// arguments, max -1 for any number.
func (a *args) want(min, max int, usage string) error {
if len(a.pos) < min || (max >= 0 && len(a.pos) > max) {
return usagef("usage: mygo-automation %s", usage)
}
return nil
}