Skip to content

Commit

Permalink
feat(cli): man page generation
Browse files Browse the repository at this point in the history
  • Loading branch information
vic1707 committed Jun 2, 2024
1 parent 86c255a commit b04426b
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 5 deletions.
24 changes: 24 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion rens-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ edition = "2021"
rust-version = "1.75.0"

[dependencies]
clap = { version = "4.5.4", features = ["derive"] }
anyhow = "1.0.86"
clap = { version = "4.5.4", features = ["derive", "string"] }
clap-verbosity-flag = "2.2.0"
clap_complete_command = "0.5.1"
clap_mangen = "0.2.20"
dunce = "1.0.4"
env_logger = "0.11.3"
log = "0.4.21"
Expand Down
12 changes: 11 additions & 1 deletion rens-cli/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/* Modules */
pub mod renaming;
/* Built-in imports */
use std::path::PathBuf;
/* Crate imports */
use renaming::Mode;
/* Dependencies */
use clap::{Parser, Subcommand};
use clap::{Parser, Subcommand, ValueHint};
use clap_verbosity_flag::Verbosity;

#[derive(Debug, Parser)]
Expand All @@ -25,6 +27,14 @@ pub enum Commands {
/// The shell to generate the completions for
shell: clap_complete_command::Shell,
},
/// Generate man page
Man {
/// The dir path to generate man-pages to.
///
/// Note: Will get created if doesn't exist.
#[arg(value_hint = ValueHint::DirPath)]
path: PathBuf,
},
}

#[cfg(test)]
Expand Down
25 changes: 22 additions & 3 deletions rens-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
mod cli;
mod utils;
/* Built-in imports */
use std::io;
use std::{fs, io};
/* Crate imports */
use cli::{
renaming::{ConfirmOption, Options},
Expand All @@ -19,7 +19,8 @@ use rens_common::{
};
use tap::{Pipe, Tap};

fn main() {
#[allow(clippy::too_many_lines)]
fn main() -> anyhow::Result<()> {
let Cli {
command,
verbose: _,
Expand All @@ -34,6 +35,21 @@ fn main() {
Commands::Completions { shell } => {
shell.generate(&mut Cli::command(), &mut io::stdout());
},
Commands::Man { path } => {
let command = Cli::command();
if !path.exists() {
fs::create_dir_all(&path)?;
}

for subcommand in command.get_subcommands() {
let subcommand_filename =
format!("{}-{}", command.get_name(), subcommand.get_name());
let cmd = subcommand.clone().name(subcommand_filename);
clap_mangen::Man::new(cmd).generate_to(&path)?;
}

clap_mangen::Man::new(command).generate_to(path)?;
},
Commands::Renaming(mode) => {
let (
strategy,
Expand Down Expand Up @@ -101,7 +117,8 @@ fn main() {
if confirmations.confirm == ConfirmOption::Once
&& !ask_for_confirm("All good ?")
{
return println!("Canceled...");
println!("Canceled...");
return Ok(());
}

files
Expand All @@ -119,4 +136,6 @@ fn main() {
.for_each(|err| error!("{err}"));
},
}

Ok(())
}

0 comments on commit b04426b

Please sign in to comment.