mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-16 06:18:42 +02:00
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.
36 lines
1.5 KiB
TypeScript
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);
|
|
}
|