more anys

This commit is contained in:
Olivier Gagnon
2022-07-19 23:26:21 -04:00
parent 30291f7899
commit 71d8e35bb5
5 changed files with 30 additions and 21 deletions
+13 -10
View File
@@ -1,17 +1,20 @@
import { toNative } from "./toNative";
import libarg from "arg";
import { ScriptArg } from "../Netscript/ScriptArg";
export function Flags(vargs: string[]): any {
return () =>
(data: any): any => {
data = toNative(data);
// We always want the help flag.
type FlagType = StringConstructor | NumberConstructor | BooleanConstructor | StringConstructor[];
type FlagsRet = { [key: string]: ScriptArg };
export function Flags(vargs: string[]): () => (data: unknown) => FlagsRet {
return (/* ctx: NetscriptContext */) =>
(schema: unknown): FlagsRet => {
schema = toNative(schema);
if (!Array.isArray(schema)) throw new Error("flags schema passed in is invalid.");
const args: {
[key: string]: any;
[key: string]: FlagType;
} = {};
for (const d of data) {
let t: any = String;
for (const d of schema) {
let t: FlagType = String;
if (typeof d[1] === "number") {
t = Number;
} else if (typeof d[1] === "boolean") {
@@ -22,8 +25,8 @@ export function Flags(vargs: string[]): any {
const numDashes = d[0].length > 1 ? 2 : 1;
args["-".repeat(numDashes) + d[0]] = t;
}
const ret = libarg(args, { argv: vargs });
for (const d of data) {
const ret: FlagsRet = libarg(args, { argv: vargs });
for (const d of schema) {
if (!ret.hasOwnProperty("--" + d[0]) || !ret.hasOwnProperty("-" + d[0])) ret[d[0]] = d[1];
}
for (const key of Object.keys(ret)) {