diff --git a/doc/source/basicgameplay/terminal.rst b/doc/source/basicgameplay/terminal.rst index 638ab3320..1ee640f24 100644 --- a/doc/source/basicgameplay/terminal.rst +++ b/doc/source/basicgameplay/terminal.rst @@ -432,10 +432,10 @@ empty file will be created. ps ^^ - $ ps [-g pattern] + $ ps [-g, --grep pattern] Prints all scripts that are currently running on the current server. -The :code:`-g pattern` option will only output running scripts where the name matches the provided pattern. +The :code:`-g, --grep pattern` option will only output running scripts where the name matches the provided pattern. rm ^^ diff --git a/src/Terminal/commands/ps.ts b/src/Terminal/commands/ps.ts index cf122193f..695d9a71d 100644 --- a/src/Terminal/commands/ps.ts +++ b/src/Terminal/commands/ps.ts @@ -2,6 +2,7 @@ import { ITerminal } from "../ITerminal"; import { IRouter } from "../../ui/Router"; import { IPlayer } from "../../PersonObjects/IPlayer"; import { BaseServer } from "../../Server/BaseServer"; +import * as libarg from "arg" export function ps( terminal: ITerminal, @@ -10,14 +11,24 @@ export function ps( server: BaseServer, args: (string | number | boolean)[], ): void { - if (args.length === 1 || args.length === 2 || args.length > 3) { - terminal.error("Incorrect usage of ps command. Usage: ps [| grep ]"); + let flags; + try{ + flags = libarg({ + '--grep': String, + '-g': '--grep' + }, + { argv: args } + ) + }catch(e){ + // catch passing only -g / --grep with no string to use as the search + terminal.error("Incorrect usage of ps command. Usage: ps [-g, --grep pattern]"); return; } - if(args[0] === '|' && args[1] === 'grep'){ - const pattern = new RegExp(`${args[2].toString()}`) - const matching = server.runningScripts.filter((x) => pattern.test(x.filename)) - for(let i = 0; i < matching.length; i++){ + const pattern = flags['--grep'] + if (pattern) { + const re = new RegExp(pattern.toString()) + const matching = server.runningScripts.filter((x) => re.test(x.filename)) + for (let i = 0; i < matching.length; i++) { const rsObj = matching[i]; let res = `(PID - ${rsObj.pid}) ${rsObj.filename}`; for (let j = 0; j < rsObj.args.length; ++j) { @@ -25,7 +36,8 @@ export function ps( } terminal.print(res); } - }else if(args.length === 0){ + } + if(args.length === 0){ for (let i = 0; i < server.runningScripts.length; i++) { const rsObj = server.runningScripts[i]; let res = `(PID - ${rsObj.pid}) ${rsObj.filename}`; @@ -34,8 +46,5 @@ export function ps( } terminal.print(res); } - }else{ - terminal.error("Incorrect usage of ps command. Usage: ps [| grep ]"); - return; } }