From f1a4a6d18c13f33fd306efe8400bf7f1150a86a0 Mon Sep 17 00:00:00 2001 From: ZTatman Date: Wed, 8 Apr 2026 14:40:49 -0400 Subject: [PATCH] refactor: use parseArgs from node:util to do basic parsing --- packages/cli/src/commands/init.ts | 3 +- packages/cli/src/index.ts | 53 +++++++++++++++++++++++-------- 2 files changed, 40 insertions(+), 16 deletions(-) diff --git a/packages/cli/src/commands/init.ts b/packages/cli/src/commands/init.ts index ed7800e..ff4d5cc 100644 --- a/packages/cli/src/commands/init.ts +++ b/packages/cli/src/commands/init.ts @@ -39,7 +39,6 @@ function validateHttpUrlOrHost( try { const normalized = normalizeHttpUrl(value); const url = new URL(normalized); - if (!url.hostname) { return `${label} must include a domain or hostname`; } @@ -125,7 +124,7 @@ async function preflightWriteTargets(filePaths: string[]): Promise { * @param _args - The command arguments. * @returns {Promise} A promise that resolves when the initialization is complete. */ -export async function initCommand(_args: string[]): Promise { +export async function initCommand(args?: string[]): Promise { p.intro(c.bold("myst init")); p.note( [ diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index e4f9b7d..e5a5f61 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -1,4 +1,6 @@ #!/usr/bin/env node +import { parseArgs } from "node:util"; + import * as c from "yoctocolors"; import pkg from "../package.json" with { type: "json" }; @@ -7,40 +9,63 @@ import { getHelpText } from "./help.js"; type CommandName = "init"; -type CommandHandler = (args: string[]) => Promise; +type CommandHandler = (args?: string[]) => Promise; const commandHandlers: Record = { init: initCommand, }; -function isKnownCommand(value: string): value is CommandName { - return value in commandHandlers; -} - async function main(): Promise { - const [command, ...options] = process.argv.slice(2); - const flags = options.filter((o) => o.startsWith("-")); + const { values, positionals } = parseArgs({ + args: process.argv.slice(2), + options: { + version: { + type: "boolean", + short: "v", + }, + help: { + type: "boolean", + short: "h", + }, + output: { + type: "string", + short: "o", + }, + }, + allowPositionals: true, + }); - if (flags.includes("-v") || flags.includes("--version")) { - process.stdout.write(`${pkg.version}\n`); - process.exitCode = 0; - return; - } + const command = positionals[0]; - if (!command || flags.includes("-h") || flags.includes("--help")) { + if (!command) { process.stdout.write(getHelpText()); process.exitCode = 0; return; } + const isKnownCommand = (cmd: string): cmd is CommandName => + cmd in commandHandlers; + if (!isKnownCommand(command)) { process.stderr.write(`Unknown command: ${command}\n`); process.exitCode = 1; return; } + if (values.help) { + process.stdout.write(getHelpText()); + process.exitCode = 0; + return; + } + + if (values.version) { + process.stdout.write(`${pkg.version}`); + process.exitCode = 0; + return; + } + try { - await commandHandlers[command](options); + await commandHandlers[command](); } catch (error) { const message = error instanceof Error ? error.message : "Unknown error"; process.stderr.write(`${c.red("Error:")} ${message}\n`);