Skip to content

Commit

Permalink
added help message
Browse files Browse the repository at this point in the history
  • Loading branch information
Paolo Infante committed May 31, 2022
1 parent 75c6373 commit 3659f1c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@paolo-projects/terminal-emulator",
"version": "0.2.0",
"version": "0.2.1",
"description": "",
"main": "build/index.js",
"types": "build/index.d.ts",
Expand Down
29 changes: 27 additions & 2 deletions src/CommandScheme/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import FlagArgument from '../Argument/FlagArgument';
import PositionalArgument from '../Argument/PositionalArgument';
import ValueArgument from '../Argument/ValueArgument';
import Command from '../Command';
import OutputStream from '../OutputStream';
import ArgumentScheme, {
FlagArgumentScheme,
PositionalArgumentScheme,
Expand All @@ -15,16 +16,24 @@ export type CommandSchemeMatchCallback = (
) => Promise<boolean>;

export default class CommandScheme {
private outputStream?: OutputStream;

private constructor(
public name: string,
public argSchemes: ArgumentScheme[],
public matchCallback: CommandSchemeMatchCallback
public matchCallback: CommandSchemeMatchCallback,
public helpMessage?: string
) {}

setOutputStream(stream: OutputStream) {
this.outputStream = stream;
}

static Builder = class {
name: string;
args: ArgumentScheme[] = [];
schemeCallback?: CommandSchemeMatchCallback;
helpMsg?: string;

constructor(commandName: string) {
this.name = commandName;
Expand Down Expand Up @@ -56,6 +65,11 @@ export default class CommandScheme {
return this;
}

withHelpMessage(helpMessage: string) {
this.helpMsg = helpMessage;
return this;
}

callback(callback: CommandSchemeMatchCallback) {
this.schemeCallback = callback;
return this;
Expand All @@ -65,7 +79,8 @@ export default class CommandScheme {
return new CommandScheme(
this.name,
this.args,
this.schemeCallback!!
this.schemeCallback!!,
this.helpMsg
);
}
};
Expand All @@ -75,6 +90,16 @@ export default class CommandScheme {
return false;
}

if (
this.helpMessage &&
command.args.filter(
(arg) => arg instanceof FlagArgument && arg.name === 'help'
).length
) {
this.outputStream?.writeLine(this.helpMessage);
return true;
}

for (const argScheme of this.argSchemes) {
switch (argScheme.argType) {
case 'positional':
Expand Down
1 change: 1 addition & 0 deletions src/TerminalEmulator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default class TerminalEmulator {
private commandNotFoundHandler: CommandCallback | null = null;

command(scheme: CommandScheme): TerminalEmulator {
scheme.setOutputStream(this.outputStream);
this.commandSchemes.push(scheme);
return this;
}
Expand Down

0 comments on commit 3659f1c

Please sign in to comment.