A CLI Library for NodeJS/TypeScript

Not production ready, but working towards it. Until then, it can safely be used for hobby projects.
Uses semantic versioning.
Features
- Unopinionated - Does not enforce some folder structure or other architecture decisions.
- Works with
ts-node
or plain compiled files using tsc
. - Lightweight - Only has a small API surface and no feature bloat.
- Fully typed - Every parameter has a static type arguments are validated against.
- Fully reflective - A help text is generated automatically.
- Embedded GUI - Use
--cli::gui
to launch an html based GUI which assists with specifying arguments.
Installation
Use the following command to install the library using yarn:
yarn add @hediet/cli
Usage
This example demonstrates most of the API:
import {
types,
runDefaultCli,
cliInfoFromPackageJson,
namedParam,
positionalParam,
createDefaultCli,
} from "@hediet/cli";
import { join } from "path";
interface CmdData {
run(): Promise<void>;
}
const cli = createDefaultCli<CmdData>()
.addCmd({
name: "print",
description: "Prints selected files.",
positionalParams: [
positionalParam("files", types.arrayOf(types.string), {
description: "The files to print.",
}),
],
namedParams: {
onlyFileNames: namedParam(types.booleanFlag, {
shortName: "n",
description: "Only print filenames",
}),
count: namedParam(types.int, {
description: "The count",
}),
},
getData: args => ({
async run() {
for (const f of args.files) {
if (args.onlyFileNames) {
console.log(f);
} else {
console.log(f + " content");
}
}
},
}),
});
runDefaultCli({
info: cliInfoFromPackageJson(join(__dirname, "./package.json")),
cli,
dataHandler: data => data.run(),
});
CLI Syntax
The parser accepts the following EBNF for a single part (PART
):
PART ::= ("--"|"/") PARAM_NAME ("=" VALUE)?
| "-" SHORT_PARAM_NAME ("=" VALUE)?
| "-" SHORT_PARAM_NAME+
| POSITIONAL_SEPERATOR
| VALUE
POSITIONAL_SEPERATOR ::= "--"
SHORT_PARAM_NAME ::= [a-zA-Z_:]
PARAM_NAME ::= SHORT_PARAM_NAME [a-zA-Z_:0-9-]*
VALUE ::= .*
Parts are splitted by the underlying shell.
A single POSITIONAL_SEPERATOR
is used to treat all following parts as value.
-abc
is the same as -a -b -c
. To avoid confusion, grouped parameters must not accept values.
GUI
This library uses my highly experimental libraries @hediet/semantic-json and
@hediet/semantic-json-react which should not be used on their own right now. They have an incredible potential though, as this application demonstrates.
For the example above, the generated UI looks like this:

The UI can be launched with ts-node ./demo --cli::gui
.
Architecture
Primary goal of this library is to process the command line arguments passed to the current process.
The user writes the command line arguments as a single string in a shell of his choice:
foo bar --baz qux quux /x=y
This string is splitted by his shell and then passed as an array of strings to the launched process (foo
):
["bar", "--baz", "qux", "quux", "/x=y"]
The original process cannot reconstruct the original command line argument string as whitespaces are lost.
This string array is parsed by the Parser
class.
Each array item is classified as value or as parameter that might have a value:
[
Value("bar"),
Param("baz"),
Value("qux"),
Value("quux"),
ParamWithValue("x", "y"),
];
This information is stored in ParsedCmd
instances.
ParsedCmd
s are transformed into an AssembledCmd
by the CmdAssembler
class.
The assembler needs to know how many arguments a parameter can accept (NoValue
, SingleValue
or MultiValue
).
Depending on that, parts are assembled together into positional and named arguments.
If baz
and x
are SingleValue
-parameters, the result would be:
[
PositionalArg("bar"),
NamedArg("baz", ["qux"]),
PositionalArg("quux"),
NamedArg("x", ["y"]),
];
A Cmd
defines typed parameters and can transform a ParsedCmd
into user defined data by using the Assembler
and type parsers.
These commands are organized in instances of the Cli
class.
Given a string array, it detects the specified command and asks the command to get the user defined data.
This function takes a Cli
instance and executes a data handler, prints a help, the version or launches a GUI,
depending on the current command line args.
TODOs
See open issues on github. Feel free to contribute and ask questions! ;)