rust: add nice command-line argument parsing#4397
Conversation
Summary: This patch outfits `//tensorboard/data/server` with a nice CLI, courtesy of [`clap`]. The flags are a subset of the main TensorBoard flags, but with kebab-case rather than snake-case (per everyone-other-than-Google style, via `clap` defaults). Since we now accept a `--port` argument, which may be `0`, we also print the port to which the server is bound. The `clap` approach to argument parsing is “define a struct all of whose fields implement `FromStr`, then mark it `#[derive(Clap)]` to generate an argument parser”. This is great on both the static typing and runtime performance fronts. To add more arguments, we just add more fields to `struct Opts`. See <https://docs.rs/clap/3.0.0-beta.2> for details (this syntax is introduced in `clap` v3.x, which is still in beta). [`clap`]: https://crates.io/crates/clap Test Plan: Run `bazel run //tensorboard/data/server` with `-h` and `--help` to see short and long help text, respectively. Then play around with each argument in turn. wchargin-branch: rust-cli wchargin-source: 957fd21bf78382a279b705077d0b3917fc240e55
stephanwlee
left a comment
There was a problem hiding this comment.
Looks good but I would like to hear your response :)
| #[clap(long, default_value = "::0")] | ||
| host: IpAddr, |
There was a problem hiding this comment.
While I am glad validation is built-in to clap and typing can make sure we do not have, say, negative port number, I am wondering who is in charge of validating the IpAddr. Is it part of IpAddr#FromStr? If so, how about PathBuf?
There was a problem hiding this comment.
Is it part of
IpAddr#FromStr? If so, how aboutPathBuf?
Yep: in all cases, the FromStr implementation is used, so, for example,
<IpAddr as FromStr>, <PathBuf as FromStr>. (On the doc pages,
you can click [src] at right to see the implementations if you want.)
This is also why we impl FromStr for Seconds below.
There was a problem hiding this comment.
Or, maybe this helps: you can tell that the FromStr implementations
are responsible for validation because of the types:
fn from_str(s: &str) -> Result<Self, Self::Err>;The docs for from_str also say this explicitly, but the signature is
enough to imply it: a method that returns a Result is generally
expected to perform validation of some kind and signal that through the
return value rather than panicking.
Summary:
This patch outfits
//tensorboard/data/serverwith a nice CLI, courtesyof
clap. The flags are a subset of the main TensorBoard flags, butwith kebab-case rather than snake-case (per everyone-other-than-Google
style, via
clapdefaults). Since we now accept a--portargument,which may be
0, we also print the port to which the server is bound.The
clapapproach to argument parsing is “define a struct all of whosefields implement
FromStr, then mark it#[derive(Clap)]to generatean argument parser”. This is great on both the static typing and runtime
performance fronts. To add more arguments, we just add more fields to
struct Opts. See https://docs.rs/clap/3.0.0-beta.2 for details (thissyntax is introduced in
clapv3.x, which is still in beta).Test Plan:
Run
bazel run //tensorboard/data/serverwith-hand--helpto seeshort and long help text, respectively. Then play around with each
argument in turn.
wchargin-branch: rust-cli