Parse KEY=VALUE strings into a typed dict, coercing bool/int/float where applicable.
(pairs: tuple[str, ...])
| 72 | |
| 73 | |
| 74 | def parse_kv_pairs(pairs: tuple[str, ...]) -> dict[str, Any]: |
| 75 | """Parse KEY=VALUE strings into a typed dict, coercing bool/int/float where applicable.""" |
| 76 | result: dict[str, Any] = {} |
| 77 | for pair in pairs: |
| 78 | if "=" not in pair: |
| 79 | raise click.BadParameter( |
| 80 | f"Expected KEY=VALUE, got: {pair!r}", |
| 81 | param_hint="--params/--metrics", |
| 82 | ) |
| 83 | key, _, raw = pair.partition("=") |
| 84 | key = key.strip() |
| 85 | raw = raw.strip() |
| 86 | if raw.lower() == "true": |
| 87 | result[key] = True |
| 88 | elif raw.lower() == "false": |
| 89 | result[key] = False |
| 90 | else: |
| 91 | try: |
| 92 | result[key] = int(raw) |
| 93 | except ValueError: |
| 94 | try: |
| 95 | result[key] = float(raw) |
| 96 | except ValueError: |
| 97 | result[key] = raw |
| 98 | return result |
| 99 | |
| 100 | |
| 101 | def now_iso() -> str: |