// @ts-nocheck
export function parseArgs(argv) {
  const args = { _: [] };

  for (let index = 0; index < argv.length; index += 1) {
    const arg = argv[index];

    if (!arg.startsWith("--")) {
      args._.push(arg);
      continue;
    }

    const [rawKey, inlineValue] = arg.slice(2).split("=", 2);
    const key = rawKey.trim();

    if (inlineValue !== undefined) {
      args[key] = coerceValue(inlineValue);
      continue;
    }

    const next = argv[index + 1];
    if (next && !next.startsWith("--")) {
      args[key] = coerceValue(next);
      index += 1;
    } else {
      args[key] = true;
    }
  }

  return args;
}

function coerceValue(value) {
  if (value === "true") return true;
  if (value === "false") return false;
  return value;
}
