cli-er
v0.16.0
Published
Tool for building advanced cli applications
Downloads
111
Maintainers
Readme
cli.js:
import Cli from "cli-er";
const definition = {...}
new Cli(definition).run();
Invocation:
node cli.js [namespace(s)|command] [OPTIONS]
Features
- Help generation: help is generated for all commands and options.
- Routing: routes are generated where command handlers are expected to be found.
- Configuration file support: define the list of configuration file-names for your cli, and
cli-er
will try to find, read its contents (fromprocess.cwd()
up), and use it insideCli.run
. - Intl support: support for internationalized messages.
- Bash completion: a command is created to generate the
bash-completions
script for the cli. - Debug mode: validate the definition and options, especially when upgrading to a new version.
- Typescript support: build the cli with typescript.
Example
Given the following definition (docker.js):
const definition = {
builder: {
description: "Manage builds",
options: {
build: {
description: "Build an image from a Dockerfile",
options: {
source: {
positional: 0,
required: true,
description: "Path or Url to the Dockerfile"
}
}
},
},
},
debug: {
type: "boolean",
aliases: ["D", "debug"],
default: false,
},
};
it will allow us to structure the code as follows:
.
├─ docker.js
└─ builder
└── build.js
so we can then execute:
node docker.js builder build . -- someother-external-option
which will try to invoke, in order:
/builder/build/index.js
/builder/build.js
/builder/index.js
/builder.js
/index.js
/docker.js
with the parsed options (only the first two are default imports, the rest are named imports using the command name, in this case build
).
This allows us to organize and structure the logic nicely.
You can check the full docker-based example for a more in-depth demo.
Installation
npm install cli-er
Usage
cli-er
default-exports a class, which takes in a definition object and an optional argument CliOptions. The available methods are the following:
- parse(args): parse the given arguments and return an object containing the options, errors and calculated location.
- run(args?): parse the given arguments and execute the corresponding script found in the calculated location. Integrates help and version generation.
- help(location?): generate help based on the definition. Can be scoped to a namespace/command.
- version(): generate a formatted version of the application's version.
- completions(): output bash-completion script contents.
- configContent(): find the contents of configuration files (e.g.
.prettierrc.json
).
Glossary
- Namespace: is used to group commands, but cannot be invoked. Can contain other namespaces, commands or options.
- Command: Is the final invocable element. Can only contain options.
- Option: arguments that hold values of different types, like string, boolean, list...