Create option-parsing helper stub

This commit is contained in:
Tangent Wantwight 2023-10-18 22:55:21 -04:00
parent fe5f88c95e
commit 68213df945

33
src/lib/options.ts Normal file
View file

@ -0,0 +1,33 @@
import { AsText, TextPiece } from '../words';
type SwitchPattern = Record<string, number> & {
min?: number;
max?: number;
};
type SwitchOutput<P extends SwitchPattern> = {
[K in keyof P]: K extends "min" | "max"
? never
: P[K] extends 0
? boolean
: string[];
} & {
error?: string;
};
// TODO: tests and error handling
export function getOpt<P extends SwitchPattern>(
argv: TextPiece[],
switches: P
): [SwitchOutput<P>, ...string[]] {
const [flags, textPieces] = getOptRaw(argv, switches);
return [flags, ...textPieces.map(AsText)];
}
export function getOptRaw<P extends SwitchPattern>(
argv: TextPiece[],
switches: P
): [SwitchOutput<P>, TextPiece[]] {
const [, ...words] = argv;
// TODO: handle min/max
return [{} as SwitchOutput<P>, words];
}