Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 19 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ pub fn help_markdown_custom<C: clap::CommandFactory>(

/// Format the help information for `command` as Markdown.
pub fn help_markdown_command(command: &clap::Command) -> String {
return help_markdown_command_custom(command, &Default::default());
return help_markdown_command_custom(command, &MarkdownOptions::default());
}

/// Format the help information for `command` as Markdown, with custom options.
Expand All @@ -130,7 +130,7 @@ pub fn print_help_markdown<C: clap::CommandFactory>() {

let mut buffer = String::with_capacity(100);

write_help_markdown(&mut buffer, &command, &Default::default());
write_help_markdown(&mut buffer, &command, &MarkdownOptions::default());

println!("{}", buffer);
}
Expand Down Expand Up @@ -169,17 +169,16 @@ fn write_help_markdown(
if options.show_table_of_contents {
writeln!(buffer, "**Command Overview:**\n").unwrap();

build_table_of_contents_markdown(buffer, Vec::new(), command, 0)
.unwrap();
build_table_of_contents_markdown(buffer, &[], command, 0).unwrap();

write!(buffer, "\n").unwrap();
writeln!(buffer).unwrap();
}

//----------------------------------------
// Write the commands/subcommands sections
//----------------------------------------

build_command_markdown(buffer, Vec::new(), command, 0, options).unwrap();
build_command_markdown(buffer, &[], command, 0, options).unwrap();

//-----------------
// Write the footer
Expand All @@ -198,7 +197,7 @@ fn write_help_markdown(
fn build_table_of_contents_markdown(
buffer: &mut String,
// Parent commands of `command`.
parent_command_path: Vec<String>,
parent_command_path: &[String],
command: &clap::Command,
depth: usize,
) -> std::fmt::Result {
Expand All @@ -212,7 +211,7 @@ fn build_table_of_contents_markdown(

// Append the name of `command` to `command_path`.
let command_path = {
let mut command_path = parent_command_path;
let mut command_path = parent_command_path.to_owned();
command_path.push(title_name);
command_path
};
Expand All @@ -231,7 +230,7 @@ fn build_table_of_contents_markdown(
for subcommand in command.get_subcommands() {
build_table_of_contents_markdown(
buffer,
command_path.clone(),
&command_path,
subcommand,
depth + 1,
)?;
Expand Down Expand Up @@ -288,7 +287,7 @@ fn build_table_of_contents_html(
fn build_command_markdown(
buffer: &mut String,
// Parent commands of `command`.
parent_command_path: Vec<String>,
parent_command_path: &[String],
command: &clap::Command,
depth: usize,
options: &MarkdownOptions,
Expand All @@ -303,7 +302,7 @@ fn build_command_markdown(

// Append the name of `command` to `command_path`.
let command_path = {
let mut command_path = parent_command_path.clone();
let mut command_path = parent_command_path.to_owned();
command_path.push(title_name);
command_path
};
Expand Down Expand Up @@ -342,7 +341,7 @@ fn build_command_markdown(
String::new()
} else {
let mut s = parent_command_path.join(" ");
s.push_str(" ");
s.push(' ');
s
},
command
Expand Down Expand Up @@ -391,7 +390,7 @@ fn build_command_markdown(
writeln!(buffer, "* `{title_name}` — {about}",)?;
}

write!(buffer, "\n")?;
writeln!(buffer)?;
}

//----------------------------------
Expand All @@ -405,7 +404,7 @@ fn build_command_markdown(
write_arg_markdown(buffer, pos_arg)?;
}

write!(buffer, "\n")?;
writeln!(buffer)?;
}

//----------------------------------
Expand All @@ -424,7 +423,7 @@ fn build_command_markdown(
write_arg_markdown(buffer, arg)?;
}

write!(buffer, "\n")?;
writeln!(buffer)?;
}

//----------------------------------
Expand All @@ -433,12 +432,12 @@ fn build_command_markdown(

// Include extra space between commands. This is purely for the benefit of
// anyone reading the source .md file.
write!(buffer, "\n\n")?;
writeln!(buffer, "\n")?;

for subcommand in command.get_subcommands() {
build_command_markdown(
buffer,
command_path.clone(),
&command_path,
subcommand,
depth + 1,
options,
Expand Down Expand Up @@ -568,8 +567,7 @@ fn write_arg_markdown(buffer: &mut String, arg: &clap::Arg) -> fmt::Result {
},
None => format!(" - `{}`\n", pv.get_name()),
})
.collect::<Vec<String>>()
.join("");
.collect();

writeln!(buffer, "\n Possible values:\n{text}")?;
} else {
Expand Down Expand Up @@ -601,8 +599,8 @@ fn get_canonical_name(command: &clap::Command) -> String {
command
.get_display_name()
.or_else(|| command.get_bin_name())
.map(|name| name.to_owned())
.unwrap_or_else(|| command.get_name().to_owned())
.unwrap_or_else(|| command.get_name())
.to_owned()
}

/// Indents non-empty lines. The output always ends with a newline.
Expand Down