add -g, --grep flags to ps command, documentation

This commit is contained in:
maxtimum 2021-12-23 22:31:07 -08:00
parent f28ddd3b3f
commit 07b00bc377
2 changed files with 21 additions and 12 deletions

@ -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
^^

@ -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 <pattern>]");
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 <pattern>]");
return;
}
}