npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

cli-define

v0.6.13

Published

Chainable argument builder for a command line interface

Downloads

573

Readme

Define

Chainable argument builder for a command line interface.

Install

npm install cli-define

Test

npm test

API

var path = require('path');
var cli = require('cli-define')(path.join(__dirname, 'package.json'));
cli
  .option('-f --file <file...>', 'files to copy')
  .option('-v --verbose', 'print more information')
  .version()
  .help()
cli.command('cp')
  .description('copy files')
  .action(function(cmd, options, raw) {
    // execute copy logic here, scope is the program instance
    console.dir(this.file);
  })

The recommended way to define options is to use the self-documenting name convention:

-v                            // => flag
--verbose                     // => flag
-v --verbose                  // => flag
-v, --verbose                 // => flag
-v | --verbose                // => flag
--option [value]              // => option (optional) 
--option <value>              // => option (required)
--option [value...]           // => option (optional, repeatable)
--option <value...>           // => option (required, repeatable)

Module

.(package, [name], [description])

Initialize the program.

  • package: Path to the module package.json used to extract default program version and description.
  • name: Specific name for the program, overrides package.json.
  • description: Specific description for the program, overrides package.json.

Returns a Program instance.

Program(package, [name], [description])

The root of the definition hierarchy, Program extends Command.

converter([fn])

A function used to coerce the value of unparsed arguments.

  • fn: A function used to coerce type or validate it's value.

description([value])

Get or set the program description.

name([value])

Get or set the program name.

Command(name, [description], [options])

Represents a command option.

action([fn])

Get or set a callback function for the command.

  • fn: The callback function.

command(name, [description], [options])

cli.command('install', 'install a package')
cli.command('install')
  .description('install a package')
  .action(function(cmd, args){})

Adds a command option.

  • name: The name of the command.
  • description: The command description.
  • options: The argument options.

If description is specified returns the Program otherwise the Command instance.

commands()

Get a map of the defined commands.

description([value])

Get or set the description for the command.

  • description: The argument description.

flag(name, [description], [options])

cli.flag('-v --verbose', 'print more information')
cli.flag('-v, --verbose', 'print more information')
cli.flag('-v | --verbose', 'print more information')

Explicitly adds a flag option to the command.

  • name: The name of the flag.
  • description: The flag description.
  • options: The argument options.

Returns the parent Command for chaining.

key([value])

Get or set the key for the command automatically generated based on the command name.

name([value])

Get or set the full string name of the command.

names()

Array of name components (read only).

option(name, [description], [options])

cli.option('-d', 'debug')                                 // => Flag
cli.option('--debug', 'debug')                            // => Flag
cli.option('-v --verbose', 'verbose')                     // => Flag
cli.option('--port [n]', 'port')                          // => Optional option
cli.option('--port [n]', 'port', 8080)                    // => Optional option w/default
cli.option('--port <n>', 'port', parseInt)                // => Required option w/coercion
cli.option('--port [n]', 'port', 8080, parseInt)          // => Optional option w/default+coercion
cli.option('--port [n]', 'port', parseInt, 8080)          // => Optional option w/coercion+default

Adds an option to the command.

  • name: The name of the option.
  • description: The option description.
  • options: The argument options.

Returns the parent Command for chaining.

options()

Get a map of the defined options.

Option(name, [description], [options])

Represents an option that expects a value, shares all the functionality of the Argument super class.

Flag(name, [description], [options])

Represents an option that does not expect a value and is treated as a boolean, shares all the functionality of the Argument super class.

Argument(name, [description], [options])

Abstract super class for all argument definition classes.

You may specify any of the fields below on the options object and they will be transferred to the instance provided they are writable.

action([fn])

Get or set a callback function for the argument, this is typically used by Command arguments but can also be specified for other arguments.

  • fn: The callback function.

converter([fn])

A function used to coerce the argument value.

  • fn: A function used to coerce the arguments type or validate it's value.

description([value])

Get or set the description for the argument.

  • description: The argument description.

extra([value])

A string representing the remainder of an argument name, given a name of -i --integer <n>, extra will equal <n>.

key([value])

Get or set the key for the argument automatically generated based on the argument name.

-v            // => v
-v --verbose  // => verbose
-p --port <n> // => port

multiple([value])

A boolean indicating that the argument may be repated, default is false.

name([value])

Get or set the full string name of the argument.

-v
-v --verbose
-v, --verbose
-v | --verbose
-p --port <n>

names()

Array of name components, does not include the extra() value (read only).

optional([value])

A boolean indicating that the argument is optional, default is true.

value([value])

Get or set the value assigned to the instance after argument parsing, this may be used to set the default value for an argument.

License

Everything is MIT. Read the license if you feel inclined.