///|
pub fn usage_text() -> String {
  "GerberGuard " +
  VERSION +
  "\n\n" +
  "Usage:\n" +
  "  gerberguard inspect  [--json]\n" +
  "  gerberguard check  [--json]\n" +
  "  gerberguard --help\n" +
  "  gerberguard --version\n\n" +
  "Commands:\n" +
  "  inspect   Show Gerber metadata and statistics\n" +
  "  check     Run structural preflight checks\n\n" +
  "Options:\n" +
  "  --json    Output JSON instead of text\n" +
  "  --help    Show help\n" +
  "  --version Show version\n"
}

///|
pub fn version_text() -> String {
  "GerberGuard " + VERSION
}

///|
pub fn parse_cli(args : ArrayView[String]) -> Result[CliConfig, CliError] {
  if args.length() == 0 {
    return Err(Usage("missing command"))
  }
  if args.length() == 1 {
    let a0 = args[0]
    if a0 == "--help" {
      return Ok({ command: Help, file: None, json: false })
    }
    if a0 == "--version" {
      return Ok({ command: Version, file: None, json: false })
    }
  }
  let command = if args[0] == "inspect" {
    Inspect
  } else if args[0] == "check" {
    Check
  } else {
    return Err(Usage("unknown command: " + args[0]))
  }
  let mut file : String? = None
  let mut json = false
  let mut i = 1
  while i < args.length() {
    let arg = args[i]
    if arg == "--json" {
      if json {
        return Err(Usage("duplicate --json"))
      }
      json = true
      i = i + 1
      continue
    }
    if has_prefix(arg, "-") {
      return Err(Usage("unknown option: " + arg))
    }
    if file is None {
      file = Some(arg)
    } else {
      return Err(Usage("exactly one input file is required"))
    }
    i = i + 1
  }
  match file {
    None => Err(Usage("missing input file"))
    Some(f) => Ok({ command, file: Some(f), json })
  }
}

///|
pub fn exit_code_for_report(report : GerberReport) -> Int {
  match report.status {
    Pass | PassWithWarnings => 0
    Fail => 1
  }
}