SkillAgentSearch skills...

Cleye

👁‍🗨 Strongly typed CLI development for Node.js

Install / Use

/learn @privatenumber/Cleye
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

<p align="center"> <img width="110" src=".github/logo.webp"> </p> <h1 align="center"> cleye <br> <a href="https://npm.im/cleye"><img src="https://badgen.net/npm/v/cleye"></a> <a href="https://npm.im/cleye"><img src="https://badgen.net/npm/dm/cleye"></a> </h1>

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!)

Try it out online

<br> <p align="center"> <a href="https://github.com/sponsors/privatenumber/sponsorships?tier_id=398771"><img width="412" src="https://raw.githubusercontent.com/privatenumber/sponsors/master/banners/assets/donate.webp"></a> <a href="https://github.com/sponsors/privatenumber/sponsorships?tier_id=397608"><img width="412" src="https://raw.githubusercontent.com/privatenumber/sponsors/master/banners/assets/sponsor.webp"></a> </p> <p align="center"><sup><i>Already a sponsor?</i> Join the discussion in the <a href="https://github.com/pvtnbr/cleye">Development repo</a>!</sup></p>

Install

npm i cleye

About

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

Here's an example script that simply logs: Good morning/evening <name>!:

greet.js:

import { cli } from 'cleye'

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

    // Define parameters
    parameters: [
        '<first name>', // First name is required
        '[last name]' // Last name is optional
    ],

    // Define flags/options
    flags: {

        // Parses `--time` as a string
        time: {
            type: String,
            description: 'Time of day to greet (morning or evening)',
            default: 'morning'
        }
    }
})

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

if (argv.flags.time === 'morning') {
    console.log(`Good morning ${name}!`)
} else {
    console.log(`Good evening ${name}!`)
}

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

<p align="center"> <br> <img src=".github/typed-flags.png" width="600"> <br> <i>Type hints for Cleye's output are very verbose and readable</i> <br> <br> </p>

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

$ node greet.js --help

greet.js

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

Flags:
  -h, --help                 Show help
      --time <string>        Time of day to greet (morning or evening) (default: "morning")

✅ Run the script to see it in action:

$ node greet.js John Doe --time evening

Good evening John Doe!

Examples

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

Usage

Arguments

Arguments are values passed into the script that are not associated with any flags/options.

For example, in the following command, the first argument is file-a.txt and the second is file-b.txt:

$ my-script file-a.txt file-b.txt

Arguments can be accessed from the _ array-property of the returned object.

Example:

const argv = cli({ /* ... */ })

// $ my-script file-a.txt file-b.txt

argv._ // => ["file-a.txt", "file-b.txt"] (string[])

Parameters

Parameters (aka positional arguments) are the names that map against argument values. Think of parameters as variable names and arguments as values associated with the variables.

Parameters can be defined in the parameters array-property to make specific arguments accessible by name. This is useful for writing more readable code, enforcing validation, and generating help documentation.

Parameters are defined in the following formats:

  • Required parameters are indicated by angle brackets (eg. <parameter name>).
  • Optional parameters are indicated by square brackets (eg. [parameter name]).
  • Spread parameters are indicated by ... suffix (eg. <parameter name...> or [parameter name...]).

Note, required parameters cannot come after optional parameters, and spread parameters must be last.

Parameters can be accessed in camelCase on the _ property of the returned object.

Example:

const argv = cli({
    parameters: [
        '<required parameter>',
        '[optional parameter]',
        '[optional spread...]'
    ]
})

// $ my-script a b c d

argv._.requiredParameter // => "a" (string)
argv._.optionalParameter // => "b" (string | undefined)
argv._.optionalSpread // => ["c", "d"] (string[])

End-of-flags

End-of-flags (--) (aka end-of-options) allows users to pass in a subset of arguments. This is useful for passing in arguments that should be parsed separately from the rest of the arguments or passing in arguments that look like flags.

An example of this is npm run:

$ npm run <script> -- <script arguments>

The -- indicates that all arguments afterwards should be passed into the script rather than npm.

All end-of-flag arguments will be accessible from argv._['--'].

Additionally, you can specify -- in the parameters array to parse end-of-flags arguments.

Example:

const argv = cli({
    name: 'npm-run',
    parameters: [
        '<script>',
        '--',
        '[arguments...]'
    ]
})

// $ npm-run echo -- hello world

argv._.script // => "echo" (string)
argv._.arguments // => ["hello", "world] (string[])

Flags

Flags (aka Options) are key-value pairs passed into the script in the format --flag-name <value>.

For example, in the following command, --file-a has value data.json and --file-b has value file.txt:

$ my-script --file-a data.json --file-b=file.txt

Parsing features

Cleye's flag parsing is powered by type-flag and comes with many features:

  • Array & Custom types
  • Flag delimiters: --flag value, --flag=value, --flag:value, and --flag.value
  • Combined aliases: -abcd 2-a -b -c -d 2
  • End of flags: Pass in -- to end flag parsing
  • Unknown flags: Unexpected flags stored in unknownFlags

Read the type-flag docs to learn more.

Defining flags

Flags can be specified in the flag object-property, where the key is the flag name, and the value is a flag type function or an object that describes the flag.

The flag name is recommended to be in camelCase as it will be interpreted to parse kebab-case equivalents.

The flag type function can be any function that accepts a string and returns the parsed value. Default JavaScript constructors should cover most use-cases: String, Number, Boolean, etc.

The flag description object can be used to store additional information about the flag, such as alias, default, and description. To accept multiple values for a flag, wrap the type function in an array.

All of the provided information will be used to generate better help documentation.

Example:

const argv = cli({
    flags: {
        someBoolean: Boolean,

        someString: {
            type: String,
            description: 'Some string flag',
            default: 'n/a'
        },

        someNumber: {
            // Wrap the type function in an array to allow multiple values
            type: [Number],
            alias: 'n',
            description: 'Array of numbers. (eg. -n 1 -n 2 -n 3)'
        }
    }
})

// $ my-script --some-boolean --some-string hello --some-number 1 -n 2

argv.flags.someBoolean // => true (boolean | undefined)
argv.flags.someString // => "hello" (string)
argv.flags.someNumber // => [1, 2] (number[])

Boolean flag negation

To support --no-<flag> syntax for boolean flags, enable booleanFlagNegation:

cli({
    flags: {
        verbose: Boolean
    },
    booleanFlagNegation: true
})
$ my-script --no-verbose
# argv.flags.verbose => false

Last-wins semantics apply between --flag and --no-flag:

$ my-script --verbose --no-verbose
# argv.flags.verbose => false

$ my-script --no-verbose --verbose
# argv.flags.verbose => true

Only applies to flags defined as Boolean. For non-boolean flags, --no-<flag> is treated as an unknown flag.

Commands inherit booleanFlagNegation from the parent CLI, but can override it.

Inverting boolean flags

Alternatively, a boolean flag can be set to false by passing the value using the = operator:

$ my-script --some-boolean=false

Without =, the false will be parsed as a separate argument:

$ my-script --some-boolean false
# argv.flags.someBoolean => true
# argv._ => ['false']

Custom flag types & validation

Custom flag types can be created to validate flags and narrow types. Simply create a new function that

View on GitHub
GitHub Stars622
CategoryDevelopment
Updated1d ago
Forks16

Languages

TypeScript

Security Score

100/100

Audited on Mar 22, 2026

No findings