aboutsummaryrefslogtreecommitdiff
path: root/src/console/commandline/CommandLineParser.ts
blob: a166f49416eda84d9f2cdde71ff89cf4ce80053e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import CommandParser from "./CommandParser";
import { Command } from "../../shared/Command";

export type CommandLine = {
  readonly command: Command,
  readonly args: string
}

export enum InputPhase {
  OnCommand,
  OnArgs,
}

export default class CommandLineParser {
  private commandParser: CommandParser = new CommandParser();

  inputPhase(line: string): InputPhase {
    line = line.trimLeft();
    if (line.length == 0) {
      return InputPhase.OnCommand
    }
    const command = line.split(/\s+/, 1)[0];
    if (line.length == command.length) {
      return InputPhase.OnCommand
    }
    return InputPhase.OnArgs;
  }

  parse(line: string): CommandLine {
    const trimLeft = line.trimLeft();
    const command = trimLeft.split(/\s+/, 1)[0];
    const args = trimLeft.slice(command.length).trimLeft();
    return {
      command: this.commandParser.parse(command),
      args: args,
    }
  }
}