JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 26376
  • Score
    100M100P100Q145935F
  • License MIT

Add a git-like command interface to your app.

Package Exports

  • command-line-commands

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (command-line-commands) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

view on npm npm module downloads Build Status Dependency Status js-standard-style

command-line-commands

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

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)

console.log('command: %s', command)
console.log('argv:    %s', JSON.stringify(argv))

Assuming the above script is installed as example:

$ example
command: null
argv:    []

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

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

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:

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

  // pass in the `argv` returned by `commandLineCommands()``
  const options = cli.parse(argv)

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

Or, process an install command:

if (command === 'install') {
  const commandLineArgs = require('command-line-args')
  const cli = commandLineArgs([
    { name: 'save', type: Boolean },
    { name: 'dir', type: String }
  ])

  // pass in the `argv` returned by `commandLineCommands()``
  const options = cli.parse(argv)

  // perform installation to the directory specified by `options.dir`.
  installSomething(options.dir)
}

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. See here for an example (uses v3 of command-line-usage which is still in progress).

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-16 Lloyd Brookes <75pound@gmail.com>. Documented by jsdoc-to-markdown.