usage/docs/markdown/
cmd.rs

1use crate::docs::markdown::renderer::MarkdownRenderer;
2use crate::docs::models::SpecCommand;
3use crate::error::UsageErr;
4
5impl MarkdownRenderer {
6    pub fn render_cmd(&self, cmd: &crate::SpecCommand) -> Result<String, UsageErr> {
7        let mut cmd = SpecCommand::from(cmd);
8        cmd.render_md(self);
9        let mut ctx = self.clone();
10        ctx.insert("cmd", &cmd);
11        ctx.render("cmd_template.md.tera")
12    }
13}
14
15#[cfg(test)]
16mod tests {
17    use crate::docs::markdown::renderer::MarkdownRenderer;
18    use crate::test::SPEC_KITCHEN_SINK;
19    use insta::assert_snapshot;
20
21    #[test]
22    fn test_render_markdown_cmd() {
23        let ctx = MarkdownRenderer::new(SPEC_KITCHEN_SINK.clone())
24            .with_multi(true)
25            .with_replace_pre_with_code_fences(true);
26        assert_snapshot!(ctx.render_cmd(&SPEC_KITCHEN_SINK.cmd).unwrap(), @r"
27        # `mycli`
28
29        - **Usage**: `mycli [FLAGS] <ARGS>… <SUBCOMMAND>`
30
31        ## Arguments
32
33        ### `<arg1>`
34
35        arg1 description
36
37        ### `[arg2]`
38
39        arg2 description
40
41        **Choices:**
42
43        - `choice1`
44        - `choice2`
45        - `choice3`
46
47        **Default:** `default value`
48
49        ### `<arg3>`
50
51        arg3 long description
52
53        ### `<argrest>...`
54
55        ### `[with-default]`
56
57        **Default:** `default value`
58
59        ## Flags
60
61        ### `--flag1`
62
63        flag1 description
64
65        ### `--flag2`
66
67        flag2 long description
68
69        includes a code block:
70
71        ```
72        $ echo hello world
73        hello world
74
75        more code
76        ```
77
78        Examples:
79
80        ```
81        # run with no arguments to use the interactive selector
82        $ mise use
83
84        # set the current version of node to 20.x in mise.toml of current directory
85        # will write the fuzzy version (e.g.: 20)
86        ```
87
88        some docs
89
90        ```
91        $ echo hello world
92        hello world
93        ```
94
95        ### `--flag3`
96
97        flag3 description
98
99        ### `--with-default`
100
101        **Default:** `default value`
102
103        ### `--shell <shell>`
104
105        **Choices:**
106
107        - `bash`
108        - `zsh`
109        - `fish`
110
111        ## Subcommands
112
113        - [`mycli plugin <SUBCOMMAND>`](/plugin.md)
114        ");
115    }
116}