diff options
| author | Paul Buetow <paul@buetow.org> | 2026-04-18 08:16:02 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-04-18 08:16:02 +0300 |
| commit | 9cab01cc8a3ec80a9ecede8a609b7b6e0769a446 (patch) | |
| tree | a3a118283148924d559a5445a345d447ab706947 /cmd | |
| parent | dfd6bc30afefba57a34aa2d3d2e384701c3cf28e (diff) | |
fix: stop using flag.Parse() in main.go so unknown flags reach the CLI
flag.Parse() rejected --backend and --kdbx-path as undefined flags before
the CLI could handle them. Replace with a manual -version / --version check
and pass os.Args[1:] directly to cli.New and cli.Run.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'cmd')
| -rw-r--r-- | cmd/foostore/main.go | 23 |
1 files changed, 10 insertions, 13 deletions
diff --git a/cmd/foostore/main.go b/cmd/foostore/main.go index b68b05d..82283da 100644 --- a/cmd/foostore/main.go +++ b/cmd/foostore/main.go @@ -6,7 +6,6 @@ package main import ( "context" - "flag" "fmt" "os" "os/signal" @@ -17,10 +16,14 @@ import ( ) func main() { - // -version prints the build version and exits immediately. - versionFlag := flag.Bool("version", false, "print version and exit") - flag.Parse() - if *versionFlag { + args := os.Args[1:] + + // Handle -version / --version before passing args to the CLI so that + // `foostore -version` exits early without initialising any backend. + // We check manually rather than using flag.Parse() because that package + // rejects unknown flags (e.g. --backend, --kdbx-path) before the CLI + // dispatcher gets a chance to handle them. + if len(args) == 1 && (args[0] == "-version" || args[0] == "--version") { fmt.Println(version.Version) os.Exit(0) } @@ -30,19 +33,13 @@ func main() { ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) defer stop() - // Capture the remaining arguments once so both New and Run receive the same - // slice. New parses --backend from it to select the backend at init time; - // Run strips --backend before dispatching commands. - args := flag.Args() - + // Pass all args to the CLI. New parses --backend and --kdbx-path from them + // to select the backend at init time; Run strips those flags before dispatch. c, err := cli.New(ctx, args) if err != nil { fmt.Fprintf(os.Stderr, "FATAL %v\n", err) os.Exit(3) } - // flag.Args() returns arguments after flags, so flag-aware invocations - // like `foostore -version` work while plain `foostore cat foo` still passes - // all args through unchanged. os.Exit(c.Run(ctx, args)) } |
