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:
parent
6be5cf3dfc
commit
a197409b2b
4 changed files with 30 additions and 0 deletions
10
Cargo.lock
generated
10
Cargo.lock
generated
|
@ -367,6 +367,15 @@ dependencies = [
|
|||
"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]]
|
||||
name = "clap_builder"
|
||||
version = "4.4.18"
|
||||
|
@ -1578,6 +1587,7 @@ dependencies = [
|
|||
"cargo_metadata",
|
||||
"chrono",
|
||||
"clap",
|
||||
"clap-markdown",
|
||||
"clap_complete",
|
||||
"clap_mangen",
|
||||
"config",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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 }
|
||||
|
|
|
@ -29,6 +29,7 @@ pub(crate) enum UtilCommand {
|
|||
Completion(UtilCompletionArgs),
|
||||
Gc(UtilGcArgs),
|
||||
Mangen(UtilMangenArgs),
|
||||
MarkdownHelp(UtilMarkdownHelp),
|
||||
ConfigSchema(UtilConfigSchemaArgs),
|
||||
}
|
||||
|
||||
|
@ -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 {}
|
||||
|
@ -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),
|
||||
}
|
||||
}
|
||||
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue