summaryrefslogtreecommitdiff
path: root/internal/cli/cli_flags.go
blob: a3673ad7b36ea1eb8313eebbdacda7e900adb1cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
}