(args: string[] = Deno.args)
| 773 | * @param args Command line arguments |
| 774 | */ |
| 775 | export async function main(args: string[] = Deno.args): Promise<void> { |
| 776 | try { |
| 777 | // Parse command and options |
| 778 | if (args.length === 0) { |
| 779 | printHelp(); |
| 780 | return; |
| 781 | } |
| 782 | |
| 783 | const commandName = args[0]; |
| 784 | |
| 785 | if (commandName === "--help" || commandName === "-h") { |
| 786 | printHelp(); |
| 787 | return; |
| 788 | } |
| 789 | |
| 790 | if (commandName === "--version" || commandName === "-v") { |
| 791 | console.log(`SPARC2 CLI v${VERSION}`); |
| 792 | return; |
| 793 | } |
| 794 | |
| 795 | const command = commands.find((cmd) => cmd.name === commandName); |
| 796 | |
| 797 | if (!command) { |
| 798 | console.error(`Unknown command: ${commandName}`); |
| 799 | printHelp(); |
| 800 | Deno.exit(1); |
| 801 | } |
| 802 | |
| 803 | // Check if command-specific help is requested |
| 804 | if (args.includes("--help") || args.includes("-h")) { |
| 805 | printCommandHelp(command); |
| 806 | return; |
| 807 | } |
| 808 | |
| 809 | // Parse command options |
| 810 | const options: Record<string, any> = {}; |
| 811 | const commandArgs: Record<string, any> = {}; |
| 812 | |
| 813 | for (let i = 1; i < args.length; i++) { |
| 814 | const arg = args[i]; |
| 815 | |
| 816 | if (arg.startsWith("--")) { |
| 817 | // Long option |
| 818 | const optionName = arg.slice(2); |
| 819 | const option = command.options.find((opt) => opt.name === optionName); |
| 820 | |
| 821 | if (!option) { |
| 822 | console.error(`Unknown option: ${arg}`); |
| 823 | Deno.exit(1); |
| 824 | } |
| 825 | |
| 826 | if (option.type === "boolean") { |
| 827 | options[optionName] = true; |
| 828 | } else { |
| 829 | if (i + 1 >= args.length) { |
| 830 | console.error(`Option ${arg} requires a value`); |
| 831 | Deno.exit(1); |
| 832 | } |
no test coverage detected