All Projects → 75lb → command-line-commands

75lb / command-line-commands

Licence: MIT license
Add a git-like command interface to your app.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to command-line-commands

option-parser
A Lightweight, header-only CLI option parser for C++
Stars: ✭ 16 (-60%)
Mutual labels:  option-parser, command-line-parser
Argagg
A simple C++11 command line argument parser
Stars: ✭ 180 (+350%)
Mutual labels:  option-parser, command-line-parser
Kotlin Argparser
Easy to use and concise yet powerful and robust command line argument parsing for Kotlin
Stars: ✭ 431 (+977.5%)
Mutual labels:  option-parser, command-line-parser
Programoptions.hxx
Single-header program options parsing library for C++11
Stars: ✭ 112 (+180%)
Mutual labels:  option-parser, command-line-parser
declarative-parser
Modern, declarative argument parser for Python 3.6+
Stars: ✭ 31 (-22.5%)
Mutual labels:  option-parser, command-line-parser
argparser
Simple command-line parser for C/C++ programs
Stars: ✭ 50 (+25%)
Mutual labels:  option-parser, command-line-parser
Clikt
Multiplatform command line interface parsing for Kotlin
Stars: ✭ 1,658 (+4045%)
Mutual labels:  option-parser, command-line-parser
CmdLine2
Command line argument parser (C++14)
Stars: ✭ 18 (-55%)
Mutual labels:  option-parser, command-line-parser
dropt
dropt is yet another C library for parsing command-line options.
Stars: ✭ 39 (-2.5%)
Mutual labels:  option-parser, command-line-parser
Command Line Args
A mature, feature-complete library to parse command-line options.
Stars: ✭ 525 (+1212.5%)
Mutual labels:  option-parser, command-line-parser
harg
Haskell program configuration using higher kinded data
Stars: ✭ 23 (-42.5%)
Mutual labels:  option-parser
Slop
Simple Lightweight Option Parsing - ✨ new contributors welcome ✨
Stars: ✭ 1,067 (+2567.5%)
Mutual labels:  option-parser
Console CommandLine
Full featured command line options and arguments parser.
Stars: ✭ 19 (-52.5%)
Mutual labels:  option-parser
go-getoptions
Fully featured Go (golang) command line option parser with built-in auto-completion support.
Stars: ✭ 41 (+2.5%)
Mutual labels:  option-parser
Samovar
Stars: ✭ 23 (-42.5%)
Mutual labels:  option-parser
Lyra
A simple to use, composable, command line parser for C++ 11 and beyond
Stars: ✭ 238 (+495%)
Mutual labels:  option-parser
Docopt.nim
Command line arguments parser that will make you smile (port of docopt to Nim)
Stars: ✭ 170 (+325%)
Mutual labels:  option-parser
Optparse Applicative
Applicative option parser
Stars: ✭ 707 (+1667.5%)
Mutual labels:  option-parser
Clipp
easy to use, powerful & expressive command line argument parsing for modern C++ / single header / usage & doc generation
Stars: ✭ 687 (+1617.5%)
Mutual labels:  option-parser
Flag
Flag is a simple but powerful command line option parsing library for Go support infinite level subcommand
Stars: ✭ 114 (+185%)
Mutual labels:  option-parser

view on npm npm module downloads Build Status Coverage Status Dependency Status 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-21 Lloyd Brookes <[email protected]>. Documented by jsdoc-to-markdown.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].