mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-21 00:32:51 +02:00
more anys
This commit is contained in:
@@ -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)) {
|
||||
|
||||
Reference in New Issue
Block a user