Formatting
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
* CLI Argument extraction and validation.
|
||||
*
|
||||
*
|
||||
* Converts `-${key}` or `--${key}` to `key` in the args object.
|
||||
*/
|
||||
import { z } from "zod";
|
||||
@@ -15,28 +15,31 @@ import { z } from "zod";
|
||||
* verbose: "true",
|
||||
* },
|
||||
* }
|
||||
*
|
||||
*
|
||||
* @param args - The CLI args to convert.
|
||||
* @returns The key-value object.
|
||||
*/
|
||||
export function convertArgsToObject(args: string[]): { args: string[], options: Record<string, string> } {
|
||||
export function convertArgsToObject(args: string[]): {
|
||||
args: string[];
|
||||
options: Record<string, string>;
|
||||
} {
|
||||
// Map of single-character short flags to their canonical long names
|
||||
const shortToFull: Record<string, string> = {
|
||||
'm': 'mnemonicFile',
|
||||
'o': 'output',
|
||||
'v': 'verbose',
|
||||
'h': 'help',
|
||||
m: "mnemonicFile",
|
||||
o: "output",
|
||||
v: "verbose",
|
||||
h: "help",
|
||||
};
|
||||
|
||||
// Flags that are always boolean and never consume the next argument as a value.
|
||||
// Uses the canonical (expanded) names so the check works after short-form resolution.
|
||||
const booleanFlags = new Set<string>([
|
||||
'verbose',
|
||||
'help',
|
||||
'autoInputs',
|
||||
'sign',
|
||||
'broadcast',
|
||||
'install',
|
||||
"verbose",
|
||||
"help",
|
||||
"autoInputs",
|
||||
"sign",
|
||||
"broadcast",
|
||||
"install",
|
||||
]);
|
||||
|
||||
const positionalArgs: string[] = [];
|
||||
@@ -55,7 +58,9 @@ export function convertArgsToObject(args: string[]): { args: string[], options:
|
||||
// - Remove the leading `-`s
|
||||
// - Convert kebab-case to camelCase
|
||||
// - Expand known short forms to their full names
|
||||
let key = arg.replace(/^-+/, "").replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
|
||||
let key = arg
|
||||
.replace(/^-+/, "")
|
||||
.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
|
||||
key = shortToFull[key] ?? key;
|
||||
|
||||
// Known boolean flags never take a value
|
||||
@@ -78,4 +83,4 @@ export function convertArgsToObject(args: string[]): { args: string[], options:
|
||||
}
|
||||
|
||||
return { args: positionalArgs, options: optionsObject };
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user