Skip to main content

runx_cli/export/
parser.rs

1use std::ffi::OsString;
2
3use crate::cli_args::{os_arg, split_flag};
4
5use super::{ExportPlan, Target};
6
7// rust-style-allow: long-function because this parser mirrors the flat native
8// router grammar and keeps all export flags in one auditable pass.
9pub fn parse_export_plan(args: &[OsString]) -> Result<ExportPlan, String> {
10    let target = match os_arg(args, 1, "export")? {
11        "claude" => Target::Claude,
12        "codex" => Target::Codex,
13        value => {
14            return Err(format!(
15                "runx export target must be claude or codex, got {value}"
16            ));
17        }
18    };
19    let mut refs = Vec::new();
20    let mut project = false;
21    let mut json = false;
22    let mut index = 2;
23
24    while index < args.len() {
25        let token = os_arg(args, index, "export")?;
26        if !token.starts_with("--") {
27            refs.push(token.to_owned());
28            index += 1;
29            continue;
30        }
31
32        let (flag, inline_value) = split_flag(token);
33        match flag {
34            "--json" => {
35                if inline_value.is_some() {
36                    return Err("--json does not take a value".to_owned());
37                }
38                json = true;
39            }
40            "--project" => {
41                if inline_value.is_some() {
42                    return Err("--project does not take a value".to_owned());
43                }
44                project = true;
45            }
46            _ => return Err(format!("unknown export flag {flag}")),
47        }
48        index += 1;
49    }
50
51    Ok(ExportPlan {
52        target,
53        refs,
54        project,
55        json,
56    })
57}