Create option-parsing helper stub
This commit is contained in:
parent
fe5f88c95e
commit
68213df945
1 changed files with 33 additions and 0 deletions
33
src/lib/options.ts
Normal file
33
src/lib/options.ts
Normal 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];
|
||||
}
|
Loading…
Reference in a new issue