-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: basic support for markdown generation in lib
- Loading branch information
Showing
9 changed files
with
163 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
{% set deprecated = "" %}{% if cmd.deprecated %}{% set deprecated = "~~" %}{% endif %} | ||
{{ header }} {{deprecated}}`{{ bin }}{{ cmd.full_cmd | join(sep=" ") }}`{{deprecated}}{% if cmd.deprecated %} [deprecated]{% endif -%} | ||
|
||
{% if cmd.before_long_help %} | ||
|
||
{{ cmd.before_long_help | trim }} | ||
|
||
{% elif cmd.before_help %} | ||
{{ cmd.before_help | trim }} | ||
{% endif -%} | ||
|
||
{% if cmd.aliases %} | ||
|
||
{{ header }}# Aliases: `{{ cmd.aliases | join(sep="`, `") }}`{{""-}} | ||
{% endif -%} | ||
|
||
{% if cmd.long_help %} | ||
|
||
{{ cmd.long_help | trim -}} | ||
{% elif cmd.help %} | ||
|
||
{{ cmd.help | trim -}} | ||
{% endif -%} | ||
|
||
{% for name, cmd in cmd.subcommands -%} | ||
{% if loop.first %} | ||
{{ header }}# Subcommands | ||
{% endif %} | ||
* `{{ cmd.usage }}` - {{ cmd.help -}} | ||
{% endfor -%} | ||
|
||
{% if cmd.args -%} | ||
{% for arg in cmd.args %} | ||
|
||
{{ header }}# Arg `{{ arg.usage }}` | ||
|
||
{% if arg.required %}(required) {% endif -%} | ||
{{ arg.long_help | default(value=arg.help) -}} | ||
|
||
{% endfor -%} | ||
{% endif -%} | ||
{% if cmd.flags -%} | ||
{% for flag in cmd.flags %} | ||
|
||
{% if flag.deprecated -%} | ||
{{ header }}# Flag ~~`{{ flag.usage }}`~~ [deprecated] | ||
{% else -%} | ||
{{ header }}# Flag `{{ flag.usage }}` | ||
{% endif %} | ||
{{ flag.long_help | default(value=flag.help) -}} | ||
{% endfor -%} | ||
{% endif -%} | ||
|
||
{% for ex in cmd.examples -%} | ||
{% if loop.first %} | ||
|
||
{{ header}}# Examples | ||
{% endif %} | ||
{% if ex.header -%} | ||
{{ header }}# {{ ex.header }} | ||
{% endif %} | ||
```{{ ex.lang | default(value="") }} | ||
{{ ex.code }} | ||
``` | ||
{% if ex.help %} | ||
{{ ex.help -}} | ||
{% endif -%} | ||
{% endfor -%} | ||
|
||
{% if cmd.after_long_help %} | ||
|
||
{{ cmd.after_long_help | trim }} | ||
{% elif cmd.after_help %} | ||
|
||
{{ cmd.after_help | trim }} | ||
{% endif -%} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use crate::error::UsageErr; | ||
use tera::{Context, Tera}; | ||
|
||
const SPEC_TEMPLATE: &str = include_str!("cmd_template.tera"); | ||
|
||
impl crate::spec::Spec { | ||
pub fn render_markdown(&self) -> Result<String, UsageErr> { | ||
let mut ctx = Context::new(); | ||
ctx.insert("header", "#"); | ||
ctx.insert("bin", &self.bin); | ||
ctx.insert("cmd", &self.cmd); | ||
let out = Tera::one_off(SPEC_TEMPLATE, &ctx, false)?; | ||
Ok(out) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::Spec; | ||
use insta::assert_snapshot; | ||
|
||
#[test] | ||
fn test_render_markdown() { | ||
let spec: Spec = r#" | ||
bin "mycli" | ||
arg "arg1" help="arg1 description" | ||
arg "arg2" help="arg2 description" default="default value" { | ||
choices "choice1" "choice2" "choice3" | ||
} | ||
arg "arg3" help="arg3 description" required=true long_help="arg3 long description" | ||
arg "argrest" var=true | ||
flag "--flag1" help="flag1 description" | ||
flag "--flag2" help="flag2 description" long_help="flag2 long description" | ||
flag "--flag3" help="flag3 description" negate="--no-flag3" | ||
"# | ||
.parse() | ||
.unwrap(); | ||
assert_snapshot!(spec.render_markdown().unwrap()); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
lib/src/docs/markdown/snapshots/usage__docs__markdown__tests__render_markdown.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--- | ||
source: lib/src/docs/markdown/mod.rs | ||
expression: spec.render_markdown().unwrap() | ||
--- | ||
# `mycli` | ||
|
||
## Arg `<arg1>` | ||
(required) arg1 description | ||
## Arg `<arg2>` | ||
(required) arg2 description | ||
## Arg `<arg3>` | ||
(required) arg3 long description | ||
## Arg `<argrest>...` | ||
(required) | ||
## Flag `--flag1` | ||
flag1 description | ||
## Flag `--flag2` | ||
flag2 long description | ||
## Flag `--flag3` | ||
flag3 description |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mod markdown; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters