ok/jj
1
0
Fork 0
forked from mirrors/jj

cli: new jj util markdown-help outputs jj help for all commands

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.
https://github.com/ConnorGray/clap-markdown/pull/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/
This commit is contained in:
Ilya Grigoriev 2024-01-25 18:35:28 -08:00
parent 6be5cf3dfc
commit a197409b2b
4 changed files with 30 additions and 0 deletions

10
Cargo.lock generated
View file

@ -367,6 +367,15 @@ dependencies = [
"clap_derive", "clap_derive",
] ]
[[package]]
name = "clap-markdown"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "325f50228f76921784b6d9f2d62de6778d834483248eefecd27279174797e579"
dependencies = [
"clap",
]
[[package]] [[package]]
name = "clap_builder" name = "clap_builder"
version = "4.4.18" version = "4.4.18"
@ -1578,6 +1587,7 @@ dependencies = [
"cargo_metadata", "cargo_metadata",
"chrono", "chrono",
"clap", "clap",
"clap-markdown",
"clap_complete", "clap_complete",
"clap_mangen", "clap_mangen",
"config", "config",

View file

@ -32,6 +32,7 @@ clap = { version = "4.4.18", features = [
"string", "string",
] } ] }
clap_complete = "4.4.9" clap_complete = "4.4.9"
clap-markdown = "0.1.3"
clap_mangen = "0.2.10" clap_mangen = "0.2.10"
chrono = { version = "0.4.33", default-features = false, features = [ chrono = { version = "0.4.33", default-features = false, features = [
"std", "std",

View file

@ -32,6 +32,7 @@ cargo_metadata = { workspace = true }
[dependencies] [dependencies]
chrono = { workspace = true } chrono = { workspace = true }
clap = { workspace = true } clap = { workspace = true }
clap-markdown = { workspace = true }
clap_complete = { workspace = true } clap_complete = { workspace = true }
clap_mangen = { workspace = true } clap_mangen = { workspace = true }
config = { workspace = true } config = { workspace = true }

View file

@ -29,6 +29,7 @@ pub(crate) enum UtilCommand {
Completion(UtilCompletionArgs), Completion(UtilCompletionArgs),
Gc(UtilGcArgs), Gc(UtilGcArgs),
Mangen(UtilMangenArgs), Mangen(UtilMangenArgs),
MarkdownHelp(UtilMarkdownHelp),
ConfigSchema(UtilConfigSchemaArgs), ConfigSchema(UtilConfigSchemaArgs),
} }
@ -79,6 +80,10 @@ pub(crate) struct UtilGcArgs {
#[derive(clap::Args, Clone, Debug)] #[derive(clap::Args, Clone, Debug)]
pub(crate) struct UtilMangenArgs {} 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. /// Print the JSON schema for the jj TOML config format.
#[derive(clap::Args, Clone, Debug)] #[derive(clap::Args, Clone, Debug)]
pub(crate) struct UtilConfigSchemaArgs {} pub(crate) struct UtilConfigSchemaArgs {}
@ -93,6 +98,7 @@ pub(crate) fn cmd_util(
UtilCommand::Completion(args) => cmd_util_completion(ui, command, args), UtilCommand::Completion(args) => cmd_util_completion(ui, command, args),
UtilCommand::Gc(args) => cmd_util_gc(ui, command, args), UtilCommand::Gc(args) => cmd_util_gc(ui, command, args),
UtilCommand::Mangen(args) => cmd_util_mangen(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), UtilCommand::ConfigSchema(args) => cmd_util_config_schema(ui, command, args),
} }
} }
@ -152,6 +158,18 @@ fn cmd_util_mangen(
Ok(()) 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( fn cmd_util_config_schema(
ui: &mut Ui, ui: &mut Ui,
_command: &CommandHelper, _command: &CommandHelper,