Skip to content

Commit 658061b

Browse files
committed
feat: improve openBrowser behavior
1 parent 1113c50 commit 658061b

2 files changed

Lines changed: 15 additions & 10 deletions

File tree

internal/cli/cli.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,35 @@ const progName = "openagent"
2626
// EarlyDispatch handles CLI-only commands (help, version) and normalizes argv for
2727
// optional serve-style subcommands. If it returns handled=true, the process should
2828
// exit with code exitCode. When handled is false, InitFlag may parse remaining flags
29-
// (e.g. -createDatabase).
30-
func EarlyDispatch() (handled bool, exitCode int) {
29+
// (e.g. -createDatabase). openBrowser is true only when the user explicitly passed a
30+
// serve-style subcommand (serve / gateway / run / start); bare invocation returns false
31+
// so the browser is not opened automatically.
32+
func EarlyDispatch() (handled bool, exitCode int, openBrowser bool) {
3133
for {
3234
if len(os.Args) <= 1 {
33-
return false, 0
35+
return false, 0, false
3436
}
3537

3638
first := os.Args[1]
3739

3840
switch first {
3941
case "-v", "--version", "version":
4042
printVersion()
41-
return true, 0
43+
return true, 0, false
4244
case "-h", "--help", "help":
4345
printHelp()
44-
return true, 0
46+
return true, 0, false
4547
case "serve", "gateway", "run", "start":
4648
// Match OpenClaw-style explicit "gateway" while keeping zero-arg default as serve.
4749
os.Args = append([]string{os.Args[0]}, os.Args[2:]...)
48-
continue
50+
return false, 0, true
4951
default:
5052
if len(first) > 0 && first[0] == '-' {
5153
// Legacy/global flags such as -createDatabase — leave argv for flag.Parse.
52-
return false, 0
54+
return false, 0, false
5355
}
5456
fmt.Fprintf(os.Stderr, "%s: unknown command %q\nRun '%s help' for usage.\n", progName, first, progName)
55-
return true, 2
57+
return true, 2, false
5658
}
5759
}
5860
}

main.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ import (
3232
)
3333

3434
func main() {
35-
if handled, code := cli.EarlyDispatch(); handled {
35+
handled, code, openBrowser := cli.EarlyDispatch()
36+
if handled {
3637
os.Exit(code)
3738
}
3839

@@ -107,7 +108,9 @@ func main() {
107108

108109
go object.ClearThroughputPerSecond()
109110

110-
go util.OpenBrowser(fmt.Sprintf("http://127.0.0.1:%v/", port))
111+
if openBrowser {
112+
go util.OpenBrowser(fmt.Sprintf("http://127.0.0.1:%v/", port))
113+
}
111114

112115
beego.Run(fmt.Sprintf(":%v", port))
113116
}

0 commit comments

Comments
 (0)