Skip to content

Commit 083e3fa

Browse files
gautamg795Convex, Inc.
authored andcommitted
CLI key=value parser side quest (#30799)
There are a bunch of places where we needed to parse arguments of the type `key=value` into various different types. The code had been duplicated a few times now, so this moves it into `common/` and uses some type magic to make it more versatile. GitOrigin-RevId: eed6adc871910219068e31cdd02bd9c76df67a02
1 parent 0144f9a commit 083e3fa

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/cmd_util/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ license = "LicenseRef-FSL-1.1-Apache-2.0"
88
doctest = false
99

1010
[dependencies]
11+
anyhow = { workspace = true }
1112
tracing = { workspace = true }
1213
tracing-appender = { workspace = true }
1314
tracing-subscriber = { workspace = true }

crates/cmd_util/src/keyvalue.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use std::str::FromStr;
2+
3+
use anyhow::Context;
4+
5+
pub fn parse_key_value<K, V, Output>(s: &str) -> anyhow::Result<Output>
6+
where
7+
K: FromStr<Err: std::error::Error + Send + Sync + 'static>,
8+
V: FromStr<Err: std::error::Error + Send + Sync + 'static>,
9+
Output: TryFrom<(K, V)>,
10+
anyhow::Error: From<<Output as TryFrom<(K, V)>>::Error>,
11+
{
12+
let pos = s
13+
.find('=')
14+
.ok_or_else(|| anyhow::anyhow!("invalid key=value: no `=` found in `{s}`"))?;
15+
let key = &s[..pos];
16+
let value = &s[pos + 1..];
17+
Ok((
18+
key.parse()
19+
.with_context(|| format!("Failed to parse key {key}"))?,
20+
value
21+
.parse()
22+
.with_context(|| format!("Failed to parse value {value}"))?,
23+
)
24+
.try_into()?)
25+
}

crates/cmd_util/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
#![feature(lazy_cell)]
22
pub mod env;
3+
pub mod keyvalue;

0 commit comments

Comments
 (0)