Skip to content

Commit ee3ca81

Browse files
qhua948erickt
authored andcommitted
Added some docs on around how to debug expanded derive marcos.
1 parent b9296f0 commit ee3ca81

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

.github/workflows/rust.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ jobs:
1616
- name: Run tests
1717
run: cargo test --verbose
1818
- name: Run clippy
19-
run: cargo clippy -- -D warnings
19+
run: cargo clippy --version && cargo clippy -- -D warnings
20+
- name: Run cargo expand
21+
run: (rustup default nightly && cd argh && cargo install cargo-expand && cargo expand --example simple_example.rs)

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,16 @@ struct SubCommandTwo {
175175
```
176176

177177
NOTE: This is not an officially supported Google product.
178+
179+
180+
## How to debug the expanded derive marco for `argh`
181+
182+
The `argh::FromArgs` derive marco can be debugged with the [cargo-expand](https://crates.io/crates/cargo-expand) crate.
183+
184+
### Expand the derive marco in `examples/simple_example.rs`
185+
186+
See [argh/examples/simple_example.rs](./argh/examples/simple_example.rs) for the example struct we wish to expand.
187+
188+
First, install `cargo-expand` by running `cargo install cargo-expand`. Note this requires the nightly build of Rust.
189+
190+
Once installed, run `cargo expand` with in the `argh` package and you can see the expanded code.

argh/examples/simple_example.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) 2022 Google LLC All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
use {argh::FromArgs, std::fmt::Debug};
6+
7+
#[derive(FromArgs, PartialEq, Debug)]
8+
/// Top-level command.
9+
struct TopLevel {
10+
#[argh(subcommand)]
11+
nested: MySubCommandEnum,
12+
}
13+
14+
#[derive(FromArgs, PartialEq, Debug)]
15+
#[argh(subcommand)]
16+
enum MySubCommandEnum {
17+
One(SubCommandOne),
18+
Two(SubCommandTwo),
19+
}
20+
21+
#[derive(FromArgs, PartialEq, Debug)]
22+
/// First subcommand.
23+
#[argh(subcommand, name = "one")]
24+
struct SubCommandOne {
25+
#[argh(option)]
26+
/// how many x
27+
x: usize,
28+
}
29+
30+
#[derive(FromArgs, PartialEq, Debug)]
31+
/// Second subcommand.
32+
#[argh(subcommand, name = "two")]
33+
struct SubCommandTwo {
34+
#[argh(switch)]
35+
/// whether to fooey
36+
fooey: bool,
37+
}
38+
39+
fn main() {}

argh_derive/src/help.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub(crate) fn help(
2626
fields: &[StructField<'_>],
2727
subcommand: Option<&StructField<'_>>,
2828
) -> TokenStream {
29+
#![allow(clippy::format_push_string)]
2930
let mut format_lit = "Usage: {command_name}".to_string();
3031

3132
let positional = fields.iter().filter(|f| f.kind == FieldKind::Positional);
@@ -225,7 +226,7 @@ fn positional_description(out: &mut String, field: &StructField<'_>) {
225226
}
226227

227228
fn positional_description_format(out: &mut String, name: &str, description: &str) {
228-
let info = argh_shared::CommandInfo { name: &*name, description };
229+
let info = argh_shared::CommandInfo { name, description };
229230
argh_shared::write_description(out, &info);
230231
}
231232

@@ -255,6 +256,6 @@ fn option_description_format(
255256
}
256257
name.push_str(long_with_leading_dashes);
257258

258-
let info = argh_shared::CommandInfo { name: &*name, description };
259+
let info = argh_shared::CommandInfo { name: &name, description };
259260
argh_shared::write_description(out, &info);
260261
}

0 commit comments

Comments
 (0)