diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2020-04-09 10:38:37 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-09 10:38:37 +0900 |
commit | 1656d52d2cefb3846d968c6117484e6aefe7dabe (patch) | |
tree | ab58a99b832d2571e2168f2ee0e328bc12d9580e /src/console/commandline/CommandLineParser.ts | |
parent | c6c2da8547891b50aef2f08e5f36d258183831ff (diff) | |
parent | 5176643e64d8f4a6be5fc73f0eb48dc65322e496 (diff) |
Merge pull request #730 from ueokande/refactor-console-and-completion
Refactor console and completions
Diffstat (limited to 'src/console/commandline/CommandLineParser.ts')
-rw-r--r-- | src/console/commandline/CommandLineParser.ts | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/console/commandline/CommandLineParser.ts b/src/console/commandline/CommandLineParser.ts new file mode 100644 index 0000000..a166f49 --- /dev/null +++ b/src/console/commandline/CommandLineParser.ts @@ -0,0 +1,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, + } + } +} |