Files
bitburner-src/src/Terminal/Parser.ts
David Walker 8f4313b180 Revert "PIPE: Add pipe support for passing data into and out of terminal commands (#2395)" (#2524)
This reverts commit 92b8b58588.

Accidental merge on my part - the code is in decent shape, but isn't meant to go in for 3.0.
2026-02-22 11:28:10 -08:00

36 lines
1.5 KiB
TypeScript

import { trimQuotes } from "../utils/helpers/string";
import { substituteAliases } from "../Alias";
// Helper function to parse individual arguments into number/boolean/string as appropriate
function parseArg(arg: string): string | number | boolean {
if (arg === "true") return true;
if (arg === "false") return false;
const argAsNumber = Number(arg);
if (!isNaN(argAsNumber)) return argAsNumber;
return trimQuotes(arg);
}
/** split a commands string into a commands array */
export function splitCommands(commandsText: string): string[] {
// regex to match each entire command separately, without the semicolon included.
const commandRegex = /(?:'[^']*'|"[^"]*"|[^;])*/g;
const commands = commandsText.match(commandRegex);
if (!commands) return [];
return commands.map((command) => command.trim());
}
/** parse a commands string, including alias substitution, into a commands array */
export function parseCommands(commandsText: string): string[] {
// Split the commands, apply aliases once, then split again and filter out empty commands.
const commands = splitCommands(commandsText).map(substituteAliases).flatMap(splitCommands).filter(Boolean);
return commands;
}
/** get a commandArgs array from a single command string */
export function parseCommand(command: string): (string | number | boolean)[] {
// Match every command arg in a given command string
const argDetection = /(?:([^ ;"']*"[^"]*"|[^ ;"']*'[^']*'|[^\s]+))/g;
const commandArgs = command.match(argDetection);
if (!commandArgs) return [];
return commandArgs.map(parseArg);
}