import process from "node:process";
import { commands, getCommandByName } from "./commands/index.js";
import { parseArgs } from "./utils/args.js";

const main = async (): Promise<void> => {
  const args = parseArgs();
  const activeCommand =
    getCommandByName(args.command) ?? getCommandByName("help");
  if (!activeCommand) {
    throw new Error("No command available to run");
  }
  await activeCommand.handler({ args, commands });
};

main().catch((error) => {
  console.error("Failed to run CLI:", error);
  process.exit(1);
});
