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 🙏

© 2024 – Pkg Stats / Ryan Hefner

command-line-commands

v4.0.1

Published

Add a git-like command interface to your app.

Downloads

71,407

Readme

view on npm npm module downloads Gihub repo dependents Gihub package dependents Node.js CI js-standard-style

command-line-commands

A lightweight module to help build a git-like command interface for your app.

Its job is to extract the command (the first argument, unless it's an option), check it's valid and either return it or throw. From there, you can parse the remaining args using your preferred option parser (e.g. command-line-args, minimist etc.).

Synopsis

Create a list of valid commands (null represents "no command"). Supply it to commandLineCommands(), receiving back an object with two properties: command (the supplied command) and argv (the remainder of the command line args):

const commandLineCommands = require('command-line-commands')

const validCommands = [ null, 'clean', 'update', 'install' ]
const { command, argv } = commandLineCommands(validCommands)

/* print the command and remaining command-line args */
console.log('command: %s', command)
console.log('argv:    %s', JSON.stringify(argv))

We'll assume the above script is installed as example. Since the validCommands list includes null, running it without a command is valid:

$ example
command: null
argv:    []

Running example with no command and one option:

$ example --verbose
command: null
argv:    ["--verbose"]

Running example with both a command and an option:

$ example install --save something
command: install
argv:    ["--save","something"]

Running example without a valid command will cause commandLineCommands() to throw.

From here, you can make a decision how to proceed based on the command and argv received. For example, if no command (null) was passed, you could parse the remaining argv for general options (in this case using command-line-args):

if (command === null) {
  const commandLineArgs = require('command-line-args')
  const optionDefinitions = [
    { name: 'version', type: Boolean }
  ]

  // pass in the `argv` returned by `commandLineCommands()`
  const options = commandLineArgs(optionDefinitions, { argv })

  if (options.version) {
    console.log('version 1.0.1')
  }
}

The same example, using minimist:

if (command === null) {
  const minimist = require('minimist')

  // pass in the `argv` returned by `commandLineCommands()``
  const options = minimist(argv)

  if (options.version) {
    console.log('version 1.0.1')
  }
}

More examples

Both examples use command-line-args for option-parsing.

  • Simple: A basic app with a couple of commands.
  • Advanced: A more complete example, implementing part of the git command interface.

Usage guides

Usage guides can be generated by command-line-usage. Here is a simple example (code):

usage

API Reference

Example

const commandLineCommands = require('command-line-commands')

commandLineCommands(commands, [argv]) ⇒ Object

Parses the argv value supplied (or process.argv by default), extracting and returning the command and remainder of argv. The command will be the first value in the argv array unless it is an option (e.g. --help).

Kind: Exported function
Throws:

  • INVALID_COMMAND - user supplied a command not specified in commands.

| Param | Type | Description | | --- | --- | --- | | commands | string | Array.<string> | One or more command strings, one of which the user must supply. Include null to represent "no command" (effectively making a command optional). | | [argv] | Array.<string> | An argv array, defaults to the global process.argv if not supplied. |


© 2015-25 Lloyd Brookes <[email protected]>. Documented by jsdoc-to-markdown.