Skip to content

Commit

Permalink
Add name function
Browse files Browse the repository at this point in the history
  • Loading branch information
febeling committed Dec 7, 2023
1 parent ce9581d commit eb46487
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
28 changes: 28 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
@@ -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>", "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);
}
3 changes: 2 additions & 1 deletion src/synopt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
5 changes: 5 additions & 0 deletions src/synopt.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
interface Command {
name: (text: string) => Command;
summary: (text: string) => Command;
description: (text: string) => Command;
option: (...args: DeclarationTuple) => Command;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit eb46487

Please sign in to comment.