Skip to content

Commit

Permalink
feat(cli): add man page generation subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
kaspar030 committed Dec 18, 2022
1 parent 25a8b9a commit aada3cc
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 11 deletions.
17 changes: 17 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
Expand Up @@ -36,6 +36,7 @@ url = "2.3.1"
rust-embed = "6.4.2"
task_partitioner = "0.1.1"
clap_complete = "4.0.6"
clap_mangen = "0.2.5"


[profile.release]
Expand Down
12 changes: 12 additions & 0 deletions src/cli.rs
Expand Up @@ -207,4 +207,16 @@ pub fn clap() -> clap::Command {
)
.hide(true),
)
.subcommand(
Command::new("manpages")
.about("Generate laze manpages.")
.arg(
Arg::new("outdir")
.help("directory in which to create manpage files")
.value_parser(value_parser!(PathBuf))
.value_hint(ValueHint::DirPath)
.required(true),
)
.hide(true),
)
}
57 changes: 46 additions & 11 deletions src/main.rs
Expand Up @@ -119,19 +119,54 @@ fn try_main() -> Result<i32> {

// handle completion subcommand here, so the project specific
// stuff is skipped
if let Some(("completion", matches)) = matches.subcommand() {
fn print_completions<G: clap_complete::Generator>(gen: G, cmd: &mut clap::Command) {
clap_complete::generate(gen, cmd, cmd.get_name().to_string(), &mut std::io::stdout());
match matches.subcommand() {
Some(("completion", matches)) => {
fn print_completions<G: clap_complete::Generator>(gen: G, cmd: &mut clap::Command) {
clap_complete::generate(
gen,
cmd,
cmd.get_name().to_string(),
&mut std::io::stdout(),
);
}
if let Some(generator) = matches
.get_one::<clap_complete::Shell>("generator")
.copied()
{
let mut cmd = cli::clap();
eprintln!("Generating completion file for {}...", generator);
print_completions(generator, &mut cmd);
}
return Ok(0);
}
if let Some(generator) = matches
.get_one::<clap_complete::Shell>("generator")
.copied()
{
let mut cmd = cli::clap();
eprintln!("Generating completion file for {}...", generator);
print_completions(generator, &mut cmd);
Some(("manpages", matches)) => {
fn create_manpage(cmd: clap::Command, outfile: &Path) -> Result<(), Error> {
let man = clap_mangen::Man::new(cmd);
let mut buffer: Vec<u8> = Default::default();
man.render(&mut buffer)?;

std::fs::write(outfile, buffer)?;
Ok(())
}
let mut outpath: PathBuf = matches.get_one::<PathBuf>("outdir").unwrap().clone();
let cmd = cli::clap();

outpath.push("laze.1");
create_manpage(cmd.clone(), &outpath)?;

for subcommand in cmd.get_subcommands() {
if subcommand.is_hide_set() {
continue;
}
let name = subcommand.get_name();
outpath.pop();
outpath.push(format!("laze-{}.1", name));
create_manpage(subcommand.clone(), &outpath)?;
}

return Ok(0);
}
return Ok(0);
_ => (),
}

if let Some(dir) = matches.get_one::<PathBuf>("chdir") {
Expand Down

0 comments on commit aada3cc

Please sign in to comment.