summaryrefslogtreecommitdiff
path: root/subprojects/clap/tests/derive/utils.rs
blob: c120a96a050f39e67a8695d705086cbbe975f31b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Hi, future me (or whoever you are)!
//
// Yes, we do need this attr.
// No, the warnings cannot be fixed otherwise.
// Accept and endure. Do not touch.
#![allow(unused)]

use clap::CommandFactory;
use snapbox::assert_data_eq;

pub(crate) const FULL_TEMPLATE: &str = "\
{before-help}{name} {version}
{author-with-newline}{about-with-newline}
{usage-heading} {usage}

{all-args}{after-help}";

pub(crate) fn get_help<T: CommandFactory>() -> String {
    let output = <T as CommandFactory>::command().render_help().to_string();

    eprintln!("\n%%% HELP %%%:=====\n{output}\n=====\n");
    eprintln!("\n%%% HELP (DEBUG) %%%:=====\n{output:?}\n=====\n");

    output
}

pub(crate) fn get_long_help<T: CommandFactory>() -> String {
    let output = <T as CommandFactory>::command()
        .render_long_help()
        .to_string();

    eprintln!("\n%%% LONG_HELP %%%:=====\n{output}\n=====\n");
    eprintln!("\n%%% LONG_HELP (DEBUG) %%%:=====\n{output:?}\n=====\n");

    output
}

pub(crate) fn get_subcommand_long_help<T: CommandFactory>(subcmd: &str) -> String {
    let output = <T as CommandFactory>::command()
        .get_subcommands_mut()
        .find(|s| s.get_name() == subcmd)
        .unwrap()
        .render_long_help()
        .to_string();

    eprintln!("\n%%% SUBCOMMAND `{subcmd}` HELP %%%:=====\n{output}\n=====\n",);
    eprintln!("\n%%% SUBCOMMAND `{subcmd}` HELP (DEBUG) %%%:=====\n{output:?}\n=====\n",);

    output
}

#[track_caller]
pub(crate) fn assert_output<P: clap::Parser + std::fmt::Debug>(
    args: &str,
    expected: impl snapbox::IntoData,
    stderr: bool,
) {
    let res = P::try_parse_from(args.split(' ').collect::<Vec<_>>());
    let err = res.unwrap_err();
    let actual = err.render().to_string();
    assert_eq!(
        stderr,
        err.use_stderr(),
        "Should Use STDERR failed. Should be {} but is {}",
        stderr,
        err.use_stderr()
    );
    assert_data_eq!(actual, expected.raw());
}