refactor: remove unused usage functions and migrate argument parsing to commander.js
This commit is contained in:
@@ -6,7 +6,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";
|
||||
import { Command } from "commander";
|
||||
|
||||
type RunAzResult = {
|
||||
status: number;
|
||||
@@ -40,50 +40,17 @@ function runAz(args: string[], quiet = false, allowFailure = false): RunAzResult
|
||||
}
|
||||
|
||||
async function main(): Promise<void> {
|
||||
const usageText = `Usage: ${path.basename(process.argv[1])} [options] <app-name>
|
||||
Options:
|
||||
-c, --config <path> Write JSON config to file (optional)
|
||||
-h, --help Show this help message and exit`;
|
||||
const program = new Command(path.basename(process.argv[1]));
|
||||
program
|
||||
.description("Create or update public client app and print config template")
|
||||
.argument("<app-name>", "Application name")
|
||||
.option("-c, --config <path>", "Write JSON config to file (optional)")
|
||||
.allowExcessArguments(false)
|
||||
.parse(process.argv);
|
||||
|
||||
let values: Record<string, string | boolean | undefined>;
|
||||
let positionals: string[];
|
||||
try {
|
||||
({ values, positionals } = parseArgs({
|
||||
args: process.argv.slice(2),
|
||||
options: {
|
||||
help: { type: "boolean", short: "h" },
|
||||
config: { type: "string", short: "c" },
|
||||
},
|
||||
strict: true,
|
||||
allowPositionals: true,
|
||||
}));
|
||||
} catch (err) {
|
||||
console.error(`Error: ${(err as Error).message}`);
|
||||
console.error(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.error(usageText);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const appName = positionals[0] || "";
|
||||
const configPath = typeof values.config === "string" ? values.config : "";
|
||||
|
||||
if (!appName) {
|
||||
console.error("Error: Application name is required.");
|
||||
console.error(usageText);
|
||||
process.exit(1);
|
||||
}
|
||||
const appName = program.args[0] as string;
|
||||
const options = program.opts<{ config?: string }>();
|
||||
const configPath = options.config ?? "";
|
||||
|
||||
let appId = runAz([
|
||||
"ad",
|
||||
|
||||
Reference in New Issue
Block a user