Skip to content

Commit

Permalink
docs: Add Usage Convention section to README.md
Browse files Browse the repository at this point in the history
This documents the convention for having a a hidden option or
subcommand to generate the Markdown output, and saving it to
CommandLineHelp.md.
  • Loading branch information
ConnorGray committed Dec 26, 2022
1 parent 125fa23 commit 2f584dd
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
45 changes: 44 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,49 @@ See `clap` example programs and the corresponding Markdown generated by
|------------------------------------------------------|--------------------------------------------------|
| [`./complex_app.rs`](./docs/examples/complex_app.rs) | [complex-app.md](./docs/examples/complex-app.md) |

### Usage Convention: `CommandLineHelp.md`

This section describes a suggested convention for using `clap-markdown` to
generate a `CommandLineHelp.md` file, which can be comitted to source control
and viewed as online documentation.

1. Add a hidden `--markdown-help` option to your `clap` application:

```rust
use clap::Parser;

#[derive(Parser)]
struct Cli {
#[arg(long, hide = true)]
markdown_help: bool,
}

fn main() {
let args = Cli::parse();

// Invoked as: `$ my-app --markdown-help`
if args.markdown_help {
clap_markdown::print_help_markdown::<Cli>();
}
}
```

2. Invoke `--markdown-help` to generate a `CommandLineHelp.md` file:

```shell
$ cargo run -- --markdown-help > docs/CommandLineHelp.md
```

3. Save `CommandLineHelp.md` in git, and link to it from the projects README.md
or other relevant documentation.

> For projects that have multiple associated commands, consider using the
> command name as a suffix. For example: `CommandLineHelp-your-app.md`,
> `CommandLineHelp-other-app.md`.
> Comitting `CommandLineHelp.md` to version control makes it easy to track
> user-visible changes to the command-line interface.
## Compatibility with clap

When this crate adds support for a new MAJOR version of `clap`, the MAJOR
Expand Down Expand Up @@ -66,4 +109,4 @@ See [**Development.md**](./docs/Development.md) for instructions on how to
perform common development tasks.

See [*Maintenance.md*](./docs/Maintenance.md) for instructions on how to
maintain this project.
maintain this project.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fn write_help_markdown(buffer: &mut String, command: &clap::Command) {

let title_name = match command.get_display_name() {
Some(display_name) => display_name.to_owned(),
None => format!("`{}`", command.get_name())
None => format!("`{}`", command.get_name()),
};

writeln!(buffer, "# Command-Line Help for {title_name}\n").unwrap();
Expand Down

0 comments on commit 2f584dd

Please sign in to comment.