| 1 | use std::collections::HashMap;
|
| 2 | use std::path::PathBuf;
|
| 3 |
|
| 4 | use clap::{Parser, Subcommand};
|
| 5 |
|
| 6 | use crate::pipeline;
|
| 7 |
|
| 8 | #[derive(Parser, Debug)]
|
| 9 | #[command(
|
| 10 | name = "posprint",
|
| 11 | about = "POS print document designer",
|
| 12 | disable_version_flag = false
|
| 13 | )]
|
| 14 | pub struct Cli {
|
| 15 | #[command(subcommand)]
|
| 16 | pub command: Option<Commands>,
|
| 17 |
|
| 18 | /// Open the print dialog only (no editor). Exits after printing or cancel.
|
| 19 | #[arg(long)]
|
| 20 | pub print: bool,
|
| 21 |
|
| 22 | /// POS template file to open in the GUI editor
|
| 23 | #[arg(value_name = "FILE")]
|
| 24 | pub file: Option<PathBuf>,
|
| 25 | }
|
| 26 |
|
| 27 | #[derive(Subcommand, Debug)]
|
| 28 | pub enum Commands {
|
| 29 | /// Show command-line printing help (also in the GUI Help tab)
|
| 30 | #[command(name = "cli-help")]
|
| 31 | CliHelp,
|
| 32 | /// Print a .pos file to a USB-serial printer (all parameters required)
|
| 33 | Print {
|
| 34 | /// Path to the `.pos` template file
|
| 35 | file: PathBuf,
|
| 36 | /// Serial port (e.g. COM3 on Windows, /dev/ttyUSB0 on Linux)
|
| 37 | #[arg(short, long)]
|
| 38 | printer: String,
|
| 39 | /// Template parameter as NAME=VALUE (repeat for each `${Name}` / `$[Name]`;
|
| 40 | /// checkbox params accept true/false)
|
| 41 | #[arg(long = "set", value_name = "NAME=VALUE")]
|
| 42 | set: Vec<String>,
|
| 43 | },
|
| 44 | }
|
| 45 |
|
| 46 | pub fn run(cli: Cli) -> i32 {
|
| 47 | match cli.command {
|
| 48 | Some(Commands::CliHelp) => {
|
| 49 | println!("{}", crate::HELP_CLI_MARKDOWN);
|
| 50 | 0
|
| 51 | }
|
| 52 | Some(Commands::Print { file, printer, set }) => match run_print(&file, &printer, &set) {
|
| 53 | Ok(()) => 0,
|
| 54 | Err(e) => {
|
| 55 | eprintln!("{e}");
|
| 56 | 1
|
| 57 | }
|
| 58 | },
|
| 59 | None => {
|
| 60 | eprintln!(
|
| 61 | "No subcommand specified. Run `posprint --help` or start without arguments for the GUI."
|
| 62 | );
|
| 63 | 1
|
| 64 | }
|
| 65 | }
|
| 66 | }
|
| 67 |
|
| 68 | fn run_print(file: &PathBuf, printer: &str, set: &[String]) -> Result<(), String> {
|
| 69 | let params = parse_params(set)?;
|
| 70 | pipeline::print_pos_file(file, printer, ¶ms).map_err(|e| e.to_string())
|
| 71 | }
|
| 72 |
|
| 73 | pub fn parse_params(pairs: &[String]) -> Result<HashMap<String, String>, String> {
|
| 74 | let mut map = HashMap::new();
|
| 75 | for pair in pairs {
|
| 76 | let Some((name, value)) = pair.split_once('=') else {
|
| 77 | return Err(format!("invalid parameter '{pair}', expected NAME=VALUE"));
|
| 78 | };
|
| 79 | if name.is_empty() {
|
| 80 | return Err("parameter name cannot be empty".into());
|
| 81 | }
|
| 82 | map.insert(name.to_string(), value.to_string());
|
| 83 | }
|
| 84 | Ok(map)
|
| 85 | }
|
| 86 |
|
| 87 | #[cfg(test)]
|
| 88 | mod tests {
|
| 89 | use super::*;
|
| 90 |
|
| 91 | #[test]
|
| 92 | fn parses_set_pairs() {
|
| 93 | let params = parse_params(&["A=1".into(), "B=two".into()]).unwrap();
|
| 94 | assert_eq!(params.get("A").unwrap(), "1");
|
| 95 | assert_eq!(params.get("B").unwrap(), "two");
|
| 96 | }
|
| 97 |
|
| 98 | #[test]
|
| 99 | fn rejects_invalid_pair() {
|
| 100 | assert!(parse_params(&["nope".into()]).is_err());
|
| 101 | }
|
| 102 |
|
| 103 | #[test]
|
| 104 | fn accepts_print_flag_with_file() {
|
| 105 | use clap::Parser;
|
| 106 | let cli = Cli::parse_from(["posprint", "--print", "doc.pos"]);
|
| 107 | assert!(cli.print);
|
| 108 | assert_eq!(cli.file.as_ref().unwrap().to_str().unwrap(), "doc.pos");
|
| 109 | }
|
| 110 |
|
| 111 | #[test]
|
| 112 | fn cli_help_subcommand_prints_docs() {
|
| 113 | assert_eq!(run(Cli {
|
| 114 | command: Some(Commands::CliHelp),
|
| 115 | print: false,
|
| 116 | file: None,
|
| 117 | }), 0);
|
| 118 | }
|
| 119 | }
|