Files
interactive-clap/examples/struct_with_subargs.rs
dj8yf0μl 962ddb9561 feat: propagate doc comments on flags and arguments to --help/-h + structs derive refactor (#26)
the crux of the pr is
https://github.com/near-cli-rs/interactive-clap/pull/26/commits/0c3ce3af6fa98bf597f0ee7e9da3de0663a59fe3
and
https://github.com/near-cli-rs/interactive-clap/pull/26/commits/912744d4365a93590794d1a19abdd3f4755447fe

the rest is some refactoring and splitting big chunks of code into
smaller and nested modules/functions

---

the changes to the logic of derives aren't many and are well summarised
by
the following list of commits with snapshot differences
from the same tests as added to `master` branch in  

https://github.com/dj8yfo/interactive-clap/commits/backporting_tests_into_master/
branch:


*
[test_simple_struct](https://github.com/dj8yfo/interactive-clap/blob/985a46571b95d8ca2f289426dd5f47e93715ce9f/interactive-clap-derive/src/tests/test_simple_struct.rs#L4-L26)
=>
https://github.com/near-cli-rs/interactive-clap/pull/26/commits/eb4b1243f1b6d74a0afd113173b4952d86f19e8a
(fragments reorder)
*
[test_simple_struct_with_named_arg](https://github.com/dj8yfo/interactive-clap/blob/985a46571b95d8ca2f289426dd5f47e93715ce9f/interactive-clap-derive/src/tests/test_simple_struct.rs#L29-L49)
=>
https://github.com/near-cli-rs/interactive-clap/pull/26/commits/ddb38910a99251f9f115a41f328dfaec80ecb146
(fragments reorder)
*
[test_doc_comments_propagate](https://github.com/dj8yfo/interactive-clap/blob/985a46571b95d8ca2f289426dd5f47e93715ce9f/interactive-clap-derive/src/tests/test_simple_struct.rs#L151-L208)
=>
https://github.com/near-cli-rs/interactive-clap/pull/26/commits/6461299b8ee2926c634ba4d9f69e30e372c0c32f
(doc comments propagated, `clap(verbatim_doc_comment)` propagated,
fragments reordered)
*
[test_simple_enum](https://github.com/dj8yfo/interactive-clap/blob/985a46571b95d8ca2f289426dd5f47e93715ce9f/interactive-clap-derive/src/tests/test_simple_enum.rs#L4-L28)
=> no change
*
[test_simple_enum_with_strum_discriminants](https://github.com/dj8yfo/interactive-clap/blob/985a46571b95d8ca2f289426dd5f47e93715ce9f/interactive-clap-derive/src/tests/test_simple_enum.rs#L31-L61)
=> no change
* also a bug was found when integrating new functionality:
* [new
test](https://github.com/dj8yfo/interactive-clap/blob/985a46571b95d8ca2f289426dd5f47e93715ce9f/interactive-clap-derive/src/tests/test_simple_struct.rs#L51-L75)
* [snapshot change in `InteractiveClap`
derive](https://github.com/near-cli-rs/interactive-clap/pull/26/commits/19b20993c19a13d79eab7696d1be6f0f7805fe02),
test passing
* [input change for 2nd stage derive of
`ToCliArgs`](https://github.com/near-cli-rs/interactive-clap/pull/26/commits/29c9aeaf7f219a7b7cc0fbaa8430d4586190d84d)
resulted in [test
failing](https://github.com/near-cli-rs/interactive-clap/actions/runs/13120732036/job/36605779439)


---

second (or 3rd) sub-summary mentions 2 commits
https://github.com/near-cli-rs/interactive-clap/pull/26/commits/382cc33d615128b520d19a2421d63081bfd4a2e5,
https://github.com/near-cli-rs/interactive-clap/pull/26/commits/3db17e1e90c56ab7707c875d6d527ff0387c08e9
which made
a testing step automatic and not requiring to manually copy-paste a
fragment
from snapshot generated with first test assertion.
No snapshots changed as result of these latter 2 commits.

---------

Co-authored-by: dj8yf0μl <noreply@nowhere.org>
Co-authored-by: Artur Yurii Korchynskyi <42449190+akorchyn@users.noreply.github.com>
2025-02-11 12:50:41 +02:00

67 lines
2.5 KiB
Rust

// This example shows additional functionality of the "interactive-clap" macro for parsing command-line data into a structure using the macro's subargs attributes.
// 1) build an example: cargo build --example struct_with_subargs
// 2) go to the `examples` folder: cd target/debug/examples
// 3) run an example: ./struct_with_subargs (without parameters) => entered interactive mode
// ./struct_with_subargs QWERTY 18 => account: CliAccount { social_db_folder: None, account: Some(CliSender { sender_account_id: Some("QWERTY"), age: Some(18) }) }
// To learn more about the parameters, use "help" flag: ./struct_with_subargs --help
use interactive_clap::{ResultFromCli, ToCliArgs};
#[derive(Debug, Clone, interactive_clap::InteractiveClap)]
struct Account {
/// Change SocialDb prefix
///
/// It's a paraghraph, describing, this argument usage in more detail
/// than just the headline
#[interactive_clap(long)]
#[interactive_clap(skip_interactive_input)]
#[interactive_clap(verbatim_doc_comment)]
social_db_folder: Option<String>,
/// Sender account
#[interactive_clap(subargs)]
account: Sender,
}
#[derive(Debug, Clone, interactive_clap::InteractiveClap)]
pub struct Sender {
/// What is the sender account ID?
sender_account_id: String,
/// How old is the sender?
age: u64,
}
fn main() -> color_eyre::Result<()> {
let mut cli_account = Account::parse();
let context = (); // default: input_context = ()
loop {
let account = <Account as interactive_clap::FromCli>::from_cli(Some(cli_account), context);
match account {
ResultFromCli::Ok(cli_account) | ResultFromCli::Cancel(Some(cli_account)) => {
println!("account: {cli_account:?}");
println!(
"Your console command: {}",
shell_words::join(cli_account.to_cli_args())
);
return Ok(());
}
ResultFromCli::Cancel(None) => {
println!("Goodbye!");
return Ok(());
}
ResultFromCli::Back => {
cli_account = Default::default();
}
ResultFromCli::Err(cli_account, err) => {
if let Some(cli_account) = cli_account {
println!(
"Your console command: {}",
shell_words::join(cli_account.to_cli_args())
);
}
return Err(err);
}
}
}
}