Skip to content

Commit

Permalink
feat: reorganize manpages generation
Browse files Browse the repository at this point in the history
  • Loading branch information
tranzystorekk authored and oknozor committed May 21, 2023
1 parent 8a12065 commit 1509583
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ shell-words = "^1"
which = "^4"
once_cell = "^1"
toml = "^0"
clap = { version = "4.2.4", optional = true, features = ["derive"] }
clap = { version = "4.2.4", optional = true, features = ["derive", "string"] }
clap_complete = { version = "4.0", optional = true }
clap_mangen = { version = "0.2", optional = true }
clap_complete_nushell = { version = "0.1.8", optional = true }
Expand Down
17 changes: 4 additions & 13 deletions src/bin/cog/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod commit;
mod mangen;

use std::fs;
use std::path::PathBuf;
Expand Down Expand Up @@ -294,7 +295,7 @@ enum Command {

/// Generate manpage
#[command(hide = true)]
GenerateManpage { cmd: String },
GenerateManpages { output_dir: PathBuf },
}

#[derive(Args)]
Expand Down Expand Up @@ -535,18 +536,8 @@ fn main() -> Result<()> {
Command::GenerateCompletions { shell } => {
clap_complete::generate(shell, &mut Cli::command(), "cog", &mut std::io::stdout());
}
Command::GenerateManpage { cmd } => {
let mut cog_cmd = Cli::command();
cog_cmd = cog_cmd.disable_help_subcommand(true);
let cmd = match cmd.as_str() {
"cog" => cog_cmd,
cmd => cog_cmd
.find_subcommand(cmd)
.expect("Requested non-existent subcommand")
.clone(),
};
let man = clap_mangen::Man::new(cmd);
man.render(&mut std::io::stdout())?;
Command::GenerateManpages { output_dir } => {
mangen::generate_manpages(&output_dir)?;
}
Command::Commit(CommitArgs {
typ,
Expand Down
37 changes: 37 additions & 0 deletions src/bin/cog/mangen.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use std::io::Result as IoResult;
use std::path::Path;

use clap::{Command, CommandFactory};
use clap_mangen::Man;

use crate::Cli;

pub fn generate_manpages(out_dir: &Path) -> IoResult<()> {
std::fs::create_dir_all(out_dir)?;

let cog = Cli::command();

for mut subcmd in cog.get_subcommands().filter(|c| !c.is_hide_set()).cloned() {
let name = subcmd.get_name();
let full_name = format!("cog-{}", name);
let man_name = format!("{}.1", full_name);

subcmd = subcmd.name(&full_name);

render_command(&out_dir.join(&man_name), subcmd)?;
}

render_command(&out_dir.join("cog.1"), cog)?;

Ok(())
}

fn render_command(file: &Path, cmd: Command) -> IoResult<()> {
let man = Man::new(cmd);
let mut buffer = Vec::new();

man.render(&mut buffer)?;
std::fs::write(file, buffer)?;

Ok(())
}

0 comments on commit 1509583

Please sign in to comment.