///|
/// CLI 命令定义与参数解析(argparse)
/// 传入 .ofd 文件路径,输出发票 JSON

///|
pub fn command() -> @argparse.Command {
  @argparse.Command(
    "ofd-invoice",
    about="解析 OFD 增值税电子发票,输出结构化 JSON",
    options=[
      @argparse.OptionArg(
        "indent",
        short='i',
        about="JSON 缩进空格数(默认 2)",
      ),
    ],
    positionals=[
      @argparse.PositionArg(
        "file",
        about="OFD 文件路径(.ofd)",
        num_args=@argparse.ValueRange(lower=1, upper=1),
      ),
    ],
  )
}

///|
/// 解析后的 CLI 配置
pub(all) struct CliConfig {
  file : String
  indent : Int
}

///|
pub fn parse_config(argv : ArrayView[String]) -> CliConfig raise {
  let matches = command().parse(argv~)
  let values : Map[String, Array[String]] = matches.values
  guard values is { "file": [file, ..], "indent"? : indent_vals, .. } else {
    fail("missing file argument")
  }
  let indent = match indent_vals {
    Some([n, ..]) => @string.parse_int(n) catch { _ => 2 }
    _ => 2
  }
  { file, indent }
}