Added documentation on cli

This commit is contained in:
Kevin FEDYNA
2025-01-22 14:24:55 +01:00
parent 8a5461827e
commit 75a9591f6a
8 changed files with 175 additions and 94 deletions
+40
View File
@@ -0,0 +1,40 @@
import {
CLI,
CLIAsyncCommand,
CLICommand,
displayHelp,
} from "$root/toolbox/cli/help.ts";
/**
* Run the command given by arguments.
* @param commands The given arguments.
* @param cli The CLI (sub-)configuration object.
* @returns The command or the help if commands are not valid.
*/
export function runCommand(
commands: Array<string | number>,
cli: CLI,
): never | {
command: CLICommand | CLIAsyncCommand;
args: Array<string | number>;
} {
if (commands.length == 0) {
displayHelp(cli, `No command provided.`);
}
const command = commands.shift()!.toString();
if (cli[command] == undefined) {
displayHelp(cli, `Command "${command}" doesn't exist.`);
}
if (typeof cli[command] == "object") {
return runCommand(commands, cli[command]);
}
if (cli[command].length != commands.length) {
displayHelp(cli[command], `Wrong usage of command "${command}".`);
}
return { command: cli[command], args: commands };
}
+70
View File
@@ -0,0 +1,70 @@
export type CLICommand = (...args: Array<string>) => void;
export type CLIAsyncCommand = (...args: Array<string>) => Promise<void>;
export interface CLI {
[command: string]: CLI | CLICommand | CLIAsyncCommand;
}
/**
* Display the help message for the CLI.
* @param cli The CLI (sub-)configuration object.
* @param errorMessage The error message to display.
*/
export function displayHelp(
cli: CLI | CLICommand | CLIAsyncCommand,
errorMessage?: string,
): never {
const loggingFunction = errorMessage ? console.error : console.log;
if (errorMessage) {
console.error(errorMessage);
}
if (typeof cli == "function") {
loggingFunction("Usage:");
displayFunctionHelp(cli, loggingFunction);
} else {
loggingFunction("Commands:");
displayObjectHelp(cli, loggingFunction);
}
Deno.exit(errorMessage ? 1 : 0);
}
/**
* Display object help recursivelly.
* @param cli The CLI (sub-)configuration object.
* @param loggingFunction The logging function to use.
* @param level The tab level.
*/
function displayObjectHelp(
cli: CLI,
loggingFunction: typeof console.log,
level: number = 1,
) {
for (const [key, value] of Object.entries(cli)) {
if (typeof value == "function") {
displayFunctionHelp(value, loggingFunction, level);
} else {
loggingFunction(`${" ".repeat(level * 2)}${key}`);
displayObjectHelp(value, loggingFunction, level + 1);
}
}
}
/**
* Display function help.
* @param cli The CLI function.
* @param loggingFunction The logging function to use.
* @param level The tab level.
*/
function displayFunctionHelp(
cli: CLICommand | CLIAsyncCommand,
loggingFunction: typeof console.log,
level: number = 1,
) {
const command = cli.name;
const matched = cli.toString().match(/(?<=^\()[^\)]+(?=\))/);
const args = matched?.[0].split(",").map((arg) => `<${arg.trim()}>`);
loggingFunction(
`${" ".repeat(level * 2)}${command} ${args?.join(" ") ?? ""}`,
);
}
+14
View File
@@ -0,0 +1,14 @@
import { parseArgs, ParseOptions } from "@std/cli/parse-args";
import { runCommand } from "$root/toolbox/cli/command.ts";
import { CLI } from "$root/toolbox/cli/help.ts";
/**
* Runs the CLI.
* @param cli The CLI configuration object.
* @param argSpec The Parse options for args.
*/
export function main(cli: CLI, argSpec: ParseOptions) {
const argv = parseArgs(Deno.args, argSpec);
const { command, args } = runCommand(argv._, cli);
command(...args.map((element) => element.toString()));
}