-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Ava edited this page Dec 30, 2022
·
6 revisions
API for Version: v0.3.0
StrictArgs is a NodeJS library intended to make the process of creating command line tools in NodeJS straightforward and end-user friendly.
StrictArgs is published as strict-args
on NPM.
See the navigation sidebar for pages on specific parts of the API.
To start, import strict-args
as a (non-dev) dependency:
npm install --save strict-args
Then declare the available commands and flags for your tool:
import {StrictArgs, FlagType} from "strict-args";
const strictArgs = new StrictArgs(
// The name of your tool. This should match how the tool is invoked in the
// command line, ie. if one types "fakecli" then the name here should be
// "fakecli".
/* name= */ "fakecli",
// The description of your tool. This is printed on the various help screens
// that StrictArgs will render if needed.
/* description= */ "A fake CLI that does something interesting.");
// Declare commands, including optional flags for those commands.
strictArgs.registerCommand({
name: "start",
description: "Starts the webserver on port 8080.",
flags: [
{
name: "debug",
description: "If present, enables debug mode.",
type: FlagType.FLAG, // flags don't take values
},
{
name: "port",
description: "Optional port for the server to run on. Defaults to 8080.",
default: "8080",
type: FlagType.PROPERTY, // properties take values
},
],
});
strictArgs.registerCommand({
name: "stop",
description: "Stops any running webserver.",
});
// Optionally register global flags, ie. flags that are available for any
// command.
strictArgs.registerGlobalFlag({
name: "some-flag",
description: "Does something interesting when present.",
});
// Register "command listeners". Command listeners are classes which implement
// a common interface, and have a class method which is called whenever the
// specified command is run.
// See the section on command listeners for more information.
strictArgs.addCommandListener("start", (a) => new StartCommandListener(a));
// Once all setup and registration is complete, run parse() to run the program.
// Command listeners will automatically be called if the user specifies a
// command and flags correctly, otherwise the program will exit with a message
// explaining to the user how to use the application / what they did wrong.
strictArgs.parse(process.argv);