use clap::{Parser, Subcommand};
use std::path::PathBuf;
use crate::commands::{intlib::IntLibCommands, pcbdoc::PcbDocCommands, pcblib::PcbLibCommands, prjpcb::PrjPcbCommands, schdoc::SchDocCommands, schlib::SchLibCommands};
#[derive(Parser)]
#[command(name = "altium-cli")]
#[command(version, about = "Command-line tool for Altium Designer files", long_about = None)]
struct Cli {
#[command(subcommand)]
command: Commands,
#[arg(long, global = true)]
json: bool,
#[arg(long, global = true)]
pretty: bool,
#[arg(short, long, global = true)]
verbose: bool,
#[arg(short, long, global = true)]
quiet: bool,
}
#[derive(Subcommand)]
enum Commands {
Inspect {
path: PathBuf,
},
Query {
path: PathBuf,
selector: String,
},
Edit {
path: PathBuf,
#[arg(short = 'c', long = "command")]
operation: String,
#[arg(short, long)]
output: Option<PathBuf>,
},
#[command(name = "schdoc")]
SchDoc {
#[command(subcommand)]
command: SchDocCommands,
},
#[command(name = "schlib")]
SchLib {
#[command(subcommand)]
command: SchLibCommands,
},
#[command(name = "pcbdoc")]
PcbDoc {
#[command(subcommand)]
command: PcbDocCommands,
},
#[command(name = "pcblib")]
PcbLib {
#[command(subcommand)]
command: PcbLibCommands,
},
#[command(name = "prjpcb")]
PrjPcb {
#[command(subcommand)]
command: PrjPcbCommands,
},
#[command(name = "intlib")]
IntLib {
#[command(subcommand)]
command: IntLibCommands,
},
Completions {
shell: String,
},
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let cli = Cli::parse();
let format = if cli.pretty || cli.json {
if cli.pretty { "json-pretty" } else { "json" }
} else {
"text"
};
match cli.command {
Commands::Inspect { path } => {
crate::commands::inspect::run(&path, format, cli.verbose)?;
}
Commands::Query { path, selector } => {
crate::commands::query::run(&path, &selector, format)?;
}
Commands::Edit {
path,
operation,
output,
} => {
crate::commands::edit::run(&path, &operation, format, output.as_deref())?;
}
Commands::SchDoc { command } => {
crate::commands::schdoc::run(&command, format)?;
}
Commands::SchLib { command } => {
crate::commands::schlib::run(&command, format)?;
}
Commands::PcbDoc { command } => {
crate::commands::pcbdoc::run(&command, format)?;
}
Commands::PcbLib { command } => {
crate::commands::pcblib::run(&command, format)?;
}
Commands::PrjPcb { command } => {
crate::commands::prjpcb::run(&command, format)?;
}
Commands::IntLib { command } => {
crate::commands::intlib::run(&command, format)?;
}
Commands::Completions { shell } => {
use clap::CommandFactory;
use clap_complete::{Shell, generate};
let shell_type = match shell.to_lowercase().as_str() {
"bash" => Shell::Bash,
"zsh" => Shell::Zsh,
"fish" => Shell::Fish,
"powershell" => Shell::PowerShell,
_ => return Err(format!("Unsupported shell: {}", shell).into()),
};
generate(
shell_type,
&mut Cli::command(),
"altium-cli",
&mut std::io::stdout(),
);
}
}
Ok(())
}
mod commands;
mod output;