export interface CliArgs { url?: string; output: string; format: string; segments: number; } export function parseArgs(): CliArgs { const args: CliArgs = { output: "./downloads", format: "best", segments: 1, }; const rawArgs = Bun.argv; for (let i = 0; i < rawArgs.length; i++) { const arg = rawArgs[i]; const nextArg = rawArgs[i + 1]; if (arg === "-o" || arg === "--output") { args.output = nextArg || "./downloads"; i++; } else if (arg === "-f" || arg === "--format") { args.format = nextArg || "best"; i++; } else if (arg === "-n" || arg === "--segments") { args.segments = parseInt(nextArg || "1", 10); i++; } else if (arg === "-h" || arg === "--help") { console.log(`YouTube Most Watched Segments Downloader Usage: yt-segments [options] Arguments: YouTube video URL (required) Options: -o, --output Output directory (default: ./downloads) -f, --format Video format (default: best) -n, --segments Number of top segments to download (default: 1) -h, --help Show this help message Examples: yt-segments "https://www.youtube.com/watch?v=abc123" yt-segments "https://youtu.be/abc123" -o ./videos -f mp4 yt-segments "https://www.youtube.com/watch?v=abc123" -n 3 `); process.exit(0); } else if (!arg.startsWith("-") && !arg.includes("bun")) { // This is likely the URL if (!arg.startsWith("bun") && !arg.includes("node")) { args.url = arg; } } } return args; }