Skip to content

Commit

Permalink
Derive doc clap ordering for multiple Clap
Browse files Browse the repository at this point in the history
  • Loading branch information
pickfire committed Jun 10, 2021
1 parent b4d9f95 commit 1c1cf5a
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 3 deletions.
1 change: 1 addition & 0 deletions clap_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ proc-macro-error = "1"
clap = { path = "../" }
clap_generate = { path = "../clap_generate" }
trybuild = "1.0"
regex = "1.0"
rustversion = "1"
version-sync = "0.9"

Expand Down
2 changes: 1 addition & 1 deletion clap_derive/src/derives/into_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,8 @@ pub fn gen_app_augmentation(
let app_methods = parent_attribute.top_level_methods();
let version = parent_attribute.version();
quote! {{
let #app_var = #app_var#app_methods;
#( #args )*
let #app_var = #app_var#app_methods;
#subcmd
#app_var#version
}}
Expand Down
41 changes: 40 additions & 1 deletion clap_derive/tests/flatten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
// commit#ea76fa1b1b273e65e3b0b1046643715b49bec51f which is licensed under the
// MIT/Apache 2.0 license.

use clap::Clap;
mod utils;

use clap::{Clap, IntoApp};

#[test]
fn flatten() {
Expand Down Expand Up @@ -160,3 +162,40 @@ fn update_subcommands_with_flatten() {
opt.update_from(&["test", "command2", "43"]);
assert_eq!(Opt::parse_from(&["test", "command2", "43"]), opt);
}

#[test]
fn docstrings_ordering_with_multiple_clap() {
/// this is the docstring for Flattened
#[derive(Clap)]
struct Flattened {
#[clap(long)]
foo: bool,
}

/// this is the docstring for Command
#[derive(Clap)]
struct Command {
#[clap(flatten)]
flattened: Flattened,
}

static ISSUE_2527: &str = "clap_derive
this is the docstring for Command
USAGE:
command [FLAGS]
FLAGS:
--foo
-h, --help Prints help information
-V, --version Prints version information
";

assert!(utils::compare_output(
Command::into_app(),
"command --help",
ISSUE_2527,
false
));
}
46 changes: 45 additions & 1 deletion clap_derive/tests/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
// Accept and endure. Do not touch.
#![allow(unused)]

use clap::IntoApp;
use std::io::{Cursor, Write};

use regex::Regex;

use clap::{App, IntoApp};

pub fn get_help<T: IntoApp>() -> String {
let mut output = Vec::new();
Expand Down Expand Up @@ -52,3 +56,43 @@ pub fn get_subcommand_long_help<T: IntoApp>(subcmd: &str) -> String {

output
}

fn compare<S, S2>(l: S, r: S2) -> bool
where
S: AsRef<str>,
S2: AsRef<str>,
{
let re = Regex::new("\x1b[^m]*m").unwrap();
// Strip out any mismatching \r character on windows that might sneak in on either side
let ls = l.as_ref().trim().replace("\r", "");
let rs = r.as_ref().trim().replace("\r", "");
let left = re.replace_all(&*ls, "");
let right = re.replace_all(&*rs, "");
let b = left == right;
if !b {
println!();
println!("--> left");
println!("{}", left);
println!("--> right");
println!("{}", right);
println!("--")
}
b
}

pub fn compare_output(l: App, args: &str, right: &str, stderr: bool) -> bool {
let mut buf = Cursor::new(Vec::with_capacity(50));
let res = l.try_get_matches_from(args.split(' ').collect::<Vec<_>>());
let err = res.unwrap_err();
write!(&mut buf, "{}", err).unwrap();
let content = buf.into_inner();
let left = String::from_utf8(content).unwrap();
assert_eq!(
stderr,
err.use_stderr(),
"Should Use STDERR failed. Should be {} but is {}",
stderr,
err.use_stderr()
);
compare(left, right)
}

0 comments on commit 1c1cf5a

Please sign in to comment.