A TypeScript library for building command-line interfaces (CLI) with ease.
Termify is a lightweight and flexible library for building command-line interfaces in TypeScript. It provides a simple and intuitive API for defining commands, options, and arguments, making it easy to create robust and maintainable CLI applications.
- Easy command definition: Define commands with a simple and intuitive API.
- Flexible option handling: Support for various option types, including strings, numbers, booleans, and more.
- Argument parsing: Automatically parse command arguments and provide them to your command action.
- Custom validation: Define custom validation logic for options and arguments.
- Type safety: Take advantage of TypeScript's type safety features to ensure your CLI application is robust and maintainable.
To get started with Termify, install the package using npm or yarn:
npm install @guiurm/termifyThen, import the genCommand function and define your command:
import { genCommand } from "@guiurm/termify";
const command = genCommand("my-command", options, arguments);Options are defined using an array of objects, where each object represents an option. The following properties are supported:
- name: The name of the option.
- optionType: The type of the option (e.g. string, number, boolean).
- flag: The flag for the option (e.g. -f, --foo).
- alias: An array of aliases for the option.
- defaultValue: The default value for the option.
- required: Whether the option is required.
- customValidator: A custom validation function for the option.
Defining Arguments Arguments are defined using an array of objects, where each object represents an argument. The following properties are supported:
- name: The name of the argument.
- type: The type of the argument (e.g. string, number).
- required: Whether the argument is required.
Example Here is an example of a simple CLI application using Termify:
import { genCommand } from "@guiurm/termify";
const arguments = [{ name: "url", type: "string", required: true }];
const command = genCommand({
name: "ci",
options: [
{
name: "port",
optionType: "number",
flag: "-p",
alias: ["--port"],
defaultValue: "",
required: true,
customValidator: (n) => ({ error: isNaN(Number(n)) }),
},
{
name: "env",
optionType: "string",
flag: "-e",
alias: ["--env"],
defaultValue: "",
required: false,
customValidator: (_) => ({ error: false }),
},
{
name: "ssl",
flag: "-s",
optionType: "boolean",
alias: ["--ssl"],
},
] as const,
args: [
{ name: "url", type: "string", required: true },
{ name: "token", type: "string", required: false },
] as const,
});
c.action((optionsParam, argsP) => {
console.log(optionsParam);
console.log(argsP);
});
new Termify([c]).start();This example defines a command called ci with a single option port and a single argument url. The command action logs a message to the console with the parsed option and argument values.