Skip to content

Commit

Permalink
cli: new jj util markdown-help outputs jj help for all commands
Browse files Browse the repository at this point in the history
This uses the [`clap-markdown`] library. It's not very flexible, but seems to
work. Its implementation is not difficult. If needed, we could later
reimplement the part that iterates over all subcommands and have a different
output (e.g., the boring version of each help text inside its own code block,
or a different file per subcommand, or some fancy template in handlebars or
another rust-supported templating language). I don't want to do it right now,
though.

The library does turn out to have some annoying bugs, e.g.
ConnorGray/clap-markdown#18, but I think that's not a
deal-braker.  The fix seems to be 3 lines; if the fix doesn't get merged soon, we
could vendor the library maybe?

[`clap-markdown`]: https://docs.rs/clap-markdown/latest/clap_markdown/
  • Loading branch information
ilyagr committed Jan 30, 2024
1 parent 6be5cf3 commit a197409
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ clap = { version = "4.4.18", features = [
"string",
] }
clap_complete = "4.4.9"
clap-markdown = "0.1.3"
clap_mangen = "0.2.10"
chrono = { version = "0.4.33", default-features = false, features = [
"std",
Expand Down
1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ cargo_metadata = { workspace = true }
[dependencies]
chrono = { workspace = true }
clap = { workspace = true }
clap-markdown = { workspace = true }
clap_complete = { workspace = true }
clap_mangen = { workspace = true }
config = { workspace = true }
Expand Down
18 changes: 18 additions & 0 deletions cli/src/commands/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub(crate) enum UtilCommand {
Completion(UtilCompletionArgs),
Gc(UtilGcArgs),
Mangen(UtilMangenArgs),
MarkdownHelp(UtilMarkdownHelp),
ConfigSchema(UtilConfigSchemaArgs),
}

Expand Down Expand Up @@ -79,6 +80,10 @@ pub(crate) struct UtilGcArgs {
#[derive(clap::Args, Clone, Debug)]
pub(crate) struct UtilMangenArgs {}

/// Print the CLI help for all subcommands in Markdown
#[derive(clap::Args, Clone, Debug)]
pub(crate) struct UtilMarkdownHelp {}

/// Print the JSON schema for the jj TOML config format.
#[derive(clap::Args, Clone, Debug)]
pub(crate) struct UtilConfigSchemaArgs {}
Expand All @@ -93,6 +98,7 @@ pub(crate) fn cmd_util(
UtilCommand::Completion(args) => cmd_util_completion(ui, command, args),
UtilCommand::Gc(args) => cmd_util_gc(ui, command, args),
UtilCommand::Mangen(args) => cmd_util_mangen(ui, command, args),
UtilCommand::MarkdownHelp(args) => cmd_util_markdownhelp(ui, command, args),
UtilCommand::ConfigSchema(args) => cmd_util_config_schema(ui, command, args),
}
}
Expand Down Expand Up @@ -152,6 +158,18 @@ fn cmd_util_mangen(
Ok(())
}

fn cmd_util_markdownhelp(
ui: &mut Ui,
command: &CommandHelper,
_args: &UtilMarkdownHelp,
) -> Result<(), CommandError> {
// If we ever need more flexibility, the code of `clap_markdown` is simple and
// readable. We could reimplement the parts we need without trouble.
let markdown = clap_markdown::help_markdown_command(command.app()).into_bytes();
ui.stdout_formatter().write_all(&markdown)?;
Ok(())
}

fn cmd_util_config_schema(
ui: &mut Ui,
_command: &CommandHelper,
Expand Down

0 comments on commit a197409

Please sign in to comment.