#[cfg(not(feature = "cli"))]
compile_error!("The `cli` feature must be enabled to build the sheets-diff binary.");
use std::process;
use clap::{Parser, ValueEnum};
use sheets_diff::{
DiffOptions, SheetsDiffError,
output::text::{render_summary, render_unified},
};
#[derive(Parser)]
#[command(
name = "sheets-diff",
about = "Structured diff engine for Excel .xlsx workbooks",
version
)]
struct Cli {
old: std::path::PathBuf,
new: std::path::PathBuf,
#[arg(short, long, value_enum, default_value_t = OutputFormat::Summary)]
format: OutputFormat,
#[arg(long)]
no_formulas: bool,
#[arg(long)]
no_warnings: bool,
}
#[derive(Copy, Clone, PartialEq, Eq, ValueEnum)]
enum OutputFormat {
Summary,
Unified,
}
fn main() {
let cli = Cli::parse();
let mut builder = DiffOptions::builder();
if cli.no_formulas {
builder = builder.formula_compare(sheets_diff::FormulaCompareMode::Ignore);
}
let opts = match builder.build() {
Ok(o) => o,
Err(e) => {
eprintln!("sheets-diff: invalid options: {e}");
process::exit(2);
}
};
match sheets_diff::compare_paths_with_options(&cli.old, &cli.new, opts) {
Ok(diff) => {
let output = match cli.format {
OutputFormat::Summary => render_summary(&diff),
OutputFormat::Unified => render_unified(&diff),
};
print!("{output}");
if diff.summary.cells_changed > 0
|| diff.summary.sheets_added > 0
|| diff.summary.sheets_removed > 0
|| diff.summary.sheets_renamed > 0
{
process::exit(1);
}
}
Err(e) => {
eprintln!("sheets-diff: {e}");
if let Some(src) = std::error::Error::source(&e) {
eprintln!(" caused by: {src}");
}
process::exit(2);
}
}
}