man command

This commit is contained in:
Daniel Kluge 2021-12-17 19:04:34 +01:00
parent c25a0e34e2
commit a24692e3ed

View File

@ -1,5 +1,9 @@
import type { Command, Flag } from "./types"; import type { Command, Flag } from "./types";
function getCommandByName(name: string): Command|undefined {
return commandList.find(cmd => cmd.name === name);
}
function illegalUse(raw: string, cmd: Command): string[] { function illegalUse(raw: string, cmd: Command): string[] {
return [ return [
"Syntax error!", "Syntax error!",
@ -54,10 +58,10 @@ export function printSyntax(cmd: Command): string[] {
if (cmd.subcommands && cmd.subcommands.length > 0) { if (cmd.subcommands && cmd.subcommands.length > 0) {
subcmdOption = " ["; subcmdOption = " [";
subcmdDesc.push(""); subcmdDesc.push("");
subcmdDesc.push("Subcommands:"); subcmdDesc.push("Arguments:");
cmd.subcommands.forEach((subcmd => { cmd.subcommands.forEach((subcmd => {
subcmdOption += `${subcmd.name}|`; subcmdOption += `${subcmd.name}|`;
subcmdDesc.push(`\t${subcmd.name}\t${subcmd.desc}`); subcmdDesc.push(`\t${subcmd.name}\t\t${subcmd.desc}`);
})); }));
subcmdOption = subcmdOption.substring(0, subcmdOption.length-1) + "]"; subcmdOption = subcmdOption.substring(0, subcmdOption.length-1) + "]";
} }
@ -113,4 +117,19 @@ const help: Command = {
} }
}; };
export const commandList = [about, help]; const man: Command = {
name: "man",
desc: "Provides a manual for a command.",
subcommands: [{name: "command", desc: "Name of a command"}],
execute: (_flags, args, _raw) => {
if (args.length !== 1) {
return printSyntax(man);
} else {
const cmd = getCommandByName(args[0]);
if (!cmd) return [`Cannot find command '${args[0]}'.`];
else return printSyntax(cmd);
}
}
};
export const commandList = [about, help, man];