diff --git a/scripts/create-pca.js b/scripts/create-pca.js index 00fe3d9..4f47028 100755 --- a/scripts/create-pca.js +++ b/scripts/create-pca.js @@ -5,6 +5,7 @@ import os from "node:os"; import path from "node:path"; import readline from "node:readline"; import { spawnSync } from "node:child_process"; +import { parseArgs } from "node:util"; function runAz(args, options = {}) { const result = spawnSync("az", args, { @@ -32,35 +33,46 @@ function runAz(args, options = {}) { } async function main() { - const argv = process.argv.slice(2); - const usageText = `Usage: ${path.basename(process.argv[1])} [options] + const usageText = `Usage: ${path.basename(process.argv[1])} [options] [app-name] Options: - -n, --app-name Application display name (required) + -n, --app-name Application display name (optional if positional app-name is provided) + -c, --config Write config template to file (optional) -h, --help Show this help message and exit`; - let appName = ""; - - for (let i = 0; i < argv.length; i += 1) { - const arg = argv[i]; - if (arg === "-h" || arg === "--help") { - console.log(usageText); - process.exit(0); - } - - if (arg === "-n" || arg === "--app-name") { - appName = argv[i + 1] || ""; - i += 1; - continue; - } - - if (arg.startsWith("-")) { - console.error(`Unknown option: ${arg}`); - console.error("Use -h or --help for usage information."); - process.exit(1); - } - - break; + let values; + let positionals; + try { + ({ values, positionals } = parseArgs({ + args: process.argv.slice(2), + options: { + help: { type: "boolean", short: "h" }, + "app-name": { type: "string", short: "n" }, + config: { type: "string", short: "c" }, + }, + strict: true, + allowPositionals: true, + })); + } catch (err) { + console.error(`Error: ${err.message}`); + console.log(usageText); + process.exit(1); } + if (values.help) { + console.log(usageText); + process.exit(0); + } + + if (positionals.length > 1) { + console.error( + "Error: Too many positional arguments. Only one app name positional argument is allowed.", + ); + console.log(usageText); + process.exit(1); + } + + const appName = values["app-name"] || positionals[0] || ""; + const configPath = values.config || ""; + if (!appName) { console.error("Error: Application name is required."); console.log(usageText);