use std::path::PathBuf;
use std::process;
use clap::{Parser, Subcommand};
mod commands;
#[derive(Parser)]
#[command(name = "rdocx", version, about = "CLI tool for DOCX files")]
struct Cli {
#[command(subcommand)]
command: Command,
}
#[derive(Subcommand)]
enum Command {
Inspect {
file: PathBuf,
#[arg(long)]
json: bool,
},
Text {
file: PathBuf,
},
Convert {
file: PathBuf,
#[arg(long, short = 't')]
to: String,
#[arg(long, short = 'o')]
output: Option<PathBuf>,
#[arg(long, default_value = "150")]
dpi: u32,
#[arg(long)]
font_dir: Option<PathBuf>,
},
Diff {
file_a: PathBuf,
file_b: PathBuf,
},
Replace {
file: PathBuf,
#[arg(long, short = 'p')]
placeholder: String,
#[arg(long, short = 'v')]
value: String,
#[arg(long, short = 'o')]
output: PathBuf,
},
Validate {
file: PathBuf,
},
Render {
file: PathBuf,
#[arg(long, short = 'o')]
output_dir: Option<PathBuf>,
#[arg(long, default_value = "150")]
dpi: f64,
#[arg(long)]
page: Option<usize>,
},
}
fn main() {
let cli = Cli::parse();
if let Command::Validate { file } = &cli.command {
match commands::validate(file) {
Ok(true) => return,
Ok(false) => process::exit(1),
Err(e) => {
eprintln!("Error: {e}");
process::exit(1);
}
}
}
let result = match cli.command {
Command::Inspect { file, json } => commands::inspect(&file, json),
Command::Text { file } => commands::text(&file),
Command::Convert {
file,
to,
output,
dpi,
font_dir,
} => commands::convert(&file, &to, output.as_deref(), dpi, font_dir.as_deref()),
Command::Diff { file_a, file_b } => commands::diff(&file_a, &file_b),
Command::Replace {
file,
placeholder,
value,
output,
} => commands::replace(&file, &placeholder, &value, &output),
Command::Validate { .. } => unreachable!(),
Command::Render {
file,
output_dir,
dpi,
page,
} => commands::render(&file, output_dir.as_deref(), dpi, page),
};
if let Err(e) = result {
eprintln!("Error: {e}");
process::exit(1);
}
}