args.py python
43 lines 1.2 KB
Raw
sha256:c07f2f34a0db9f43fe866f157d1935322008545ff8940c06c7c921eb219c55ab NXP-b DONE: independent BV-r2 pass + ISR (SD-17) Human minor ⚠ breaking 19 hours ago
1 """Argv preprocessing so global flags work before or after subcommands."""
2
3 from __future__ import annotations
4
5 GLOBAL_OPTIONS: dict[str, int] = {
6 "-C": 1,
7 "--repo": 1,
8 "--config": 1,
9 "--json": 0,
10 "-q": 0,
11 "--quiet": 0,
12 "-v": 0,
13 "--verbose": 0,
14 "--no-color": 0,
15 }
16
17 STANDALONE_GLOBALS = frozenset({"-h", "--help", "--version"})
18
19
20 def extract_global_args(argv: list[str]) -> tuple[list[str], list[str]]:
21 """Pull global flags out of ``argv`` regardless of position."""
22 global_args: list[str] = []
23 rest: list[str] = []
24 index = 0
25 while index < len(argv):
26 token = argv[index]
27 if token in STANDALONE_GLOBALS:
28 global_args.append(token)
29 index += 1
30 continue
31 if token in GLOBAL_OPTIONS:
32 global_args.append(token)
33 value_count = GLOBAL_OPTIONS[token]
34 for _ in range(value_count):
35 index += 1
36 if index >= len(argv):
37 break
38 global_args.append(argv[index])
39 index += 1
40 continue
41 rest.append(token)
42 index += 1
43 return global_args, rest
File History 1 commit
sha256:c07f2f34a0db9f43fe866f157d1935322008545ff8940c06c7c921eb219c55ab NXP-b DONE: independent BV-r2 pass + ISR (SD-17) Human minor 19 hours ago