From eb464874ee627aadf07bcd8881019a225b032c1e Mon Sep 17 00:00:00 2001 From: Florian Ebeling Date: Thu, 7 Dec 2023 21:43:42 +0100 Subject: [PATCH] Add name function --- example.js | 28 ++++++++++++++++++++++++++++ src/synopt.test.ts | 3 ++- src/synopt.ts | 5 +++++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 example.js diff --git a/example.js b/example.js new file mode 100644 index 0000000..e3b42f9 --- /dev/null +++ b/example.js @@ -0,0 +1,28 @@ +import synopt from "./dist/synopt.js"; + +// Declare options +synopt + .name("mkwebmanifest") // optional, for usage banner + .summary("Generate icons and web manifest for web applications") + .option("-i", "--icon ICON", "source icon file") + .option("-n", "--name", "name of the web application", { repeat: true }) + .option("--config FILE", "configuration file") + .option("--outdir ", "directory path for generated files") + .option("--verbose", "more output", { boolean: true }) + .option("-h", "--help", "print help", { boolean: true }); + +// Slice off node executable and script from argument vector +const argv = process.argv.slice(2); + +// And parse arguments. No exceptions to catch, instead check result object +const { ok, error, options } = synopt.parse(argv); + +if (ok) { + // Happy case + main(options); +} else { + // Handle errors: missing value, or unknown options/typos, etc. + console.log(error); + console.log(synopt.usage()); + process.exit(1); +} \ No newline at end of file diff --git a/src/synopt.test.ts b/src/synopt.test.ts index 742e36d..7b2d340 100644 --- a/src/synopt.test.ts +++ b/src/synopt.test.ts @@ -4,11 +4,12 @@ import { beforeEach, expect, test } from "@jest/globals"; let synopt; beforeEach(() => { - synopt = createCommand("mkstuf"); + synopt = createCommand(); }); test("usage banner", () => { synopt + .name("mkstuf") .summary("Summary.") .description("Description, which is longer.") .option("-n", "--name NAME", "Name to be used") diff --git a/src/synopt.ts b/src/synopt.ts index 9c94525..b6c9e6d 100644 --- a/src/synopt.ts +++ b/src/synopt.ts @@ -1,4 +1,5 @@ interface Command { + name: (text: string) => Command; summary: (text: string) => Command; description: (text: string) => Command; option: (...args: DeclarationTuple) => Command; @@ -118,6 +119,10 @@ const createCommand = (name?: string): Command => { }; const command: Command = { + name: (text): Command => { + state.name = text; + return command; + }, summary: (text): Command => { state.summary = text; return command;