refactor(create-pca): use parseArgs and support positional app name
This commit is contained in:
@@ -5,6 +5,7 @@ import os from "node:os";
|
|||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import readline from "node:readline";
|
import readline from "node:readline";
|
||||||
import { spawnSync } from "node:child_process";
|
import { spawnSync } from "node:child_process";
|
||||||
|
import { parseArgs } from "node:util";
|
||||||
|
|
||||||
function runAz(args, options = {}) {
|
function runAz(args, options = {}) {
|
||||||
const result = spawnSync("az", args, {
|
const result = spawnSync("az", args, {
|
||||||
@@ -32,34 +33,45 @@ function runAz(args, options = {}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
const argv = process.argv.slice(2);
|
const usageText = `Usage: ${path.basename(process.argv[1])} [options] [app-name]
|
||||||
const usageText = `Usage: ${path.basename(process.argv[1])} [options]
|
|
||||||
Options:
|
Options:
|
||||||
-n, --app-name <name> Application display name (required)
|
-n, --app-name <name> Application display name (optional if positional app-name is provided)
|
||||||
|
-c, --config <path> Write config template to file (optional)
|
||||||
-h, --help Show this help message and exit`;
|
-h, --help Show this help message and exit`;
|
||||||
let appName = "";
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
for (let i = 0; i < argv.length; i += 1) {
|
if (values.help) {
|
||||||
const arg = argv[i];
|
|
||||||
if (arg === "-h" || arg === "--help") {
|
|
||||||
console.log(usageText);
|
console.log(usageText);
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (arg === "-n" || arg === "--app-name") {
|
if (positionals.length > 1) {
|
||||||
appName = argv[i + 1] || "";
|
console.error(
|
||||||
i += 1;
|
"Error: Too many positional arguments. Only one app name positional argument is allowed.",
|
||||||
continue;
|
);
|
||||||
}
|
console.log(usageText);
|
||||||
|
|
||||||
if (arg.startsWith("-")) {
|
|
||||||
console.error(`Unknown option: ${arg}`);
|
|
||||||
console.error("Use -h or --help for usage information.");
|
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
const appName = values["app-name"] || positionals[0] || "";
|
||||||
}
|
const configPath = values.config || "";
|
||||||
|
|
||||||
if (!appName) {
|
if (!appName) {
|
||||||
console.error("Error: Application name is required.");
|
console.error("Error: Application name is required.");
|
||||||
|
|||||||
Reference in New Issue
Block a user