Skip to main content

rsp_cli/
cli.rs

1use crate::error::RspError;
2use crate::peeler::Peeler;
3use clap::{Arg, ArgMatches, Command};
4
5pub struct Cli;
6
7impl Default for Cli {
8    fn default() -> Self {
9        Self::new()
10    }
11}
12
13impl Cli {
14    pub fn new() -> Self {
15        Self
16    }
17
18    pub fn run(&self) -> Result<(), RspError> {
19        let matches = self.build_cli().get_matches();
20
21        match matches.subcommand() {
22            Some(("peel", sub_matches)) => self.handle_peel_command(sub_matches),
23            _ => {
24                eprintln!("No command provided. Use --help for available commands.");
25                Ok(())
26            }
27        }
28    }
29
30    fn build_cli(&self) -> Command {
31        Command::new("rsp")
32            .about("Raw String Peeler - Convert escaped strings in YAML to readable format")
33            .version(env!("CARGO_PKG_VERSION"))
34            .subcommand(
35                Command::new("peel")
36                    .about("Peel raw strings from YAML files")
37                    .arg(
38                        Arg::new("file")
39                            .help("The YAML file to process (use stdin if not provided)")
40                            .required(false)
41                            .value_name("FILE"),
42                    )
43                    .arg(
44                        Arg::new("output")
45                            .short('o')
46                            .long("output")
47                            .help("Output file (default: stdout)")
48                            .value_name("OUTPUT_FILE"),
49                    ),
50            )
51    }
52
53    fn handle_peel_command(&self, matches: &ArgMatches) -> Result<(), RspError> {
54        let output_file = matches.get_one::<String>("output");
55        let peeler = Peeler::new();
56
57        match matches.get_one::<String>("file") {
58            Some(input_file) => peeler.peel_file(input_file, output_file),
59            None => peeler.peel_stdin(output_file),
60        }
61    }
62}