cleye

Strongly typed CLI development for Node.js

README

undefined

cleye


The intuitive command-line interface (CLI) development tool.

Features


Minimal API surface
Powerful flag parsing
Strongly typed parameters and flags
Command support
Help documentation generation (customizable too!)

Install


  1. ``` shell
  2. npm i cleye
  3. ```

About


Cleyemakes it very easy to develop command-line scripts in Node.js. It handles argv parsing to give you strongly typed parameters + flags and generates --helpdocumentation based on the provided information.

Here's an example script that simply logs: `Good morning/evening !`:

greet.js:

  1. ``` ts
  2. import { cli } from 'cleye'

  3. // Parse argv
  4. const argv = cli({
  5.     name: 'greet.js',

  6.     // Define parameters
  7.     parameters: [
  8.         '<first name>', // First name is required
  9.         '[last name]' // Last name is optional
  10.     ],

  11.     // Define flags/options
  12.     flags: {

  13.         // Parses `--time` as a string
  14.         time: {
  15.             type: String,
  16.             description: 'Time of day to greet (morning or evening)',
  17.             default: 'morning'
  18.         }
  19.     }
  20. })

  21. const name = [argv._.firstName, argv._.lastName].filter(Boolean).join(' ')

  22. if (argv.flags.time === 'morning') {
  23.     console.log(`Good morning ${name}!`)
  24. } else {
  25.     console.log(`Good evening ${name}!`)
  26. }
  27. ```

🛠 In development, type hints are provided on parsed flags and parameters:

undefined Type hints for Cleye's output are very verbose and readable

📖 Generated help documentation can be viewed with the --helpflag:

  1. ``` shell
  2. $ node greet.js --help

  3. greet.js

  4. Usage:
  5.   greet.js [flags...] <first name> [last name]

  6. Flags:
  7.   -h, --help                 Show help
  8.       --time <string>        Time of day to greet (morning or evening) (default: "morning")
  9. ```

✅ Run the script to see it in action:

  1. ``` shell
  2. $ node greet.js John Doe --time evening

  3. Good evening John Doe!
  4. ```

Examples


Want to dive right into some code? Check out some of these examples:

greet.js : Working example from above
npm install : Reimplementation of npm install 's CLI
tsc : Reimplementation of TypeScript tsc 's CLI
snap-tweet : Reimplementation of snap-tweet 's CLI
pkg-size : Reimplementation of pkg-size 's CLI