diff options
| author | Paul Buetow <paul@buetow.org> | 2026-04-18 08:39:04 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-04-18 08:39:04 +0300 |
| commit | 7bca02bddcc88002e8edf72d2f20f526cf9d2668 (patch) | |
| tree | 7bd907e787628393d52a3281ce751d2f807421ac /internal/cli/cli_flags.go | |
| parent | 5874a7a5d3cac75b76bdf2f7793a05ce3c45b4d7 (diff) | |
refactor: split cli.go into focused files by responsibility
Extract cli.go into four dedicated files to improve separation of concerns
and readability: cli_flags.go (flag parsing), cli_backend.go (backend init),
cli_commands.go (command handlers), cli_dispatch.go (command routing).
Fix inaccurate comments on logMsg/warn that claimed declaration order matters
in Go; package-level functions are resolved across the whole file.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'internal/cli/cli_flags.go')
| -rw-r--r-- | internal/cli/cli_flags.go | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/internal/cli/cli_flags.go b/internal/cli/cli_flags.go new file mode 100644 index 0000000..a3673ad --- /dev/null +++ b/internal/cli/cli_flags.go @@ -0,0 +1,43 @@ +// Package cli — flag parsing helpers. +// +// This file contains the low-level argv scanning functions that extract +// global flags (--backend, --kdbx-path) before the command is dispatched. +// They are intentionally separate from the backend factory and the CLI struct +// so that flag parsing can be tested and reasoned about in isolation. +package cli + +// parseBackendFlag scans argv for a "--backend VALUE" pair and returns the +// backend name and the remaining argv with that pair removed. Returns ("", argv) +// when no --backend flag is present. The flag may appear anywhere in argv. +// +// Note: only the space-separated form "--backend VALUE" is supported. +// The equals form "--backend=VALUE" is NOT parsed and will be silently ignored +// (treated as an unknown argument that propagates to the command dispatcher). +func parseBackendFlag(argv []string) (string, []string) { + return parseFlagValue("--backend", argv) +} + +// parseKDBXPathFlag scans argv for a "--kdbx-path VALUE" pair and returns the +// path and the remaining argv with that pair removed. Returns ("", argv) when +// no --kdbx-path flag is present. Overrides cfg.KDBXPath for the process +// lifetime without modifying the config file. +// +// Note: only the space-separated form "--kdbx-path VALUE" is supported. +func parseKDBXPathFlag(argv []string) (string, []string) { + return parseFlagValue("--kdbx-path", argv) +} + +// parseFlagValue is the shared implementation for single-value flag extraction. +// It scans argv for a "FLAG VALUE" pair, removes it, and returns the value and +// the remaining argv. Returns ("", argv) when the flag is absent. +func parseFlagValue(flag string, argv []string) (string, []string) { + for i, arg := range argv { + if arg == flag && i+1 < len(argv) { + remaining := make([]string, 0, len(argv)-2) + remaining = append(remaining, argv[:i]...) + remaining = append(remaining, argv[i+2:]...) + return argv[i+1], remaining + } + } + return "", argv +} |
