...
This commit is contained in:
+19
-15
@@ -107,25 +107,29 @@ export function parseCommand(input: string): ParsedCommand {
|
||||
|
||||
let pastEndOfOptions = false;
|
||||
|
||||
tokens.shift();
|
||||
|
||||
for (const token of tokens) {
|
||||
if (token === "--") pastEndOfOptions = true;
|
||||
for (let i = 1; i < tokens.length; i++) {
|
||||
const token = tokens[i]!;
|
||||
|
||||
if (pastEndOfOptions) {
|
||||
args.push(token);
|
||||
} else {
|
||||
if (token.startsWith("--")) {
|
||||
flags.push(token.slice(2));
|
||||
} else if (token.startsWith("-")) {
|
||||
const newToken = token.slice(1);
|
||||
const chars = newToken.split("");
|
||||
for (const flag of chars) {
|
||||
flags.push(flag);
|
||||
}
|
||||
} else {
|
||||
args.push(token);
|
||||
continue;
|
||||
}
|
||||
if (token === "--") {
|
||||
pastEndOfOptions = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (token.startsWith("--")) {
|
||||
flags.push(token.slice(2));
|
||||
continue;
|
||||
} else if (token.startsWith("-")) {
|
||||
for (const char of token.slice(1)) {
|
||||
flags.push(char);
|
||||
}
|
||||
continue;
|
||||
} else {
|
||||
args.push(token);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,25 +1,2 @@
|
||||
import { parseCommand } from "@core/util/parser";
|
||||
|
||||
const message: string = "Hello, World!";
|
||||
console.log(message);
|
||||
|
||||
// prettier-ignore
|
||||
{
|
||||
//console.log(parseCommand('ls'))
|
||||
//console.log(parseCommand('ls /tmp'))
|
||||
//console.log(parseCommand('ls -l'))
|
||||
//console.log(parseCommand('ls -la'))
|
||||
//console.log(parseCommand('ls -la /tmp'))
|
||||
//console.log(parseCommand('ls -l -a'))
|
||||
//console.log(parseCommand('grep --color=auto'))
|
||||
//console.log(parseCommand('grep --color=auto -r'))
|
||||
console.log(parseCommand('echo -- --version'))
|
||||
console.log(parseCommand('echo -- -f'))
|
||||
console.log(parseCommand('echo --'))
|
||||
console.log(parseCommand("echo 'hello world'"))
|
||||
console.log(parseCommand(`echo \\"hello world\\"`))
|
||||
console.log(parseCommand(`echo 'it\\'s a test'`))
|
||||
console.log(parseCommand("echo \"it's a \\\"test\\\"\""))
|
||||
console.log(parseCommand("ls -la -- --tmp"))
|
||||
console.log(parseCommand("rm -rf --no-preserve-root /"))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user