Skip to content
 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cliclopts

NPM

cliclopts

Command line options helper and usage printer, works well with minimist, inspired by nomnom

usage

Define the allowed options in an array and pass it to 'cliclopts'

var cliclopts = require('cliclopts')

var options = [
  {
    name: 'verbose',
    abbr: 'v'
    alias: ['loud'],
    boolean: true,
    help: 'be verbose'
  },
  {
    name: 'path',
    abbr: 'p',
    default: './dat.json'
    help: 'path to file'
  }
]

var cliOpts = cliclopts(options)

cliOpts.usage()

Returns the usage information as string:

--verbose, -v         be verbose
--path, -p            path to file (default: "dat.json")

cliOpts.print()

Prints the usage information.

cliOpts.boolean()

Returns Array of all command names that are specified as boolean.

cliOpts.alias()

Returns Object with command names as keys and alias list as value (including abbr)

cliOpts.default()

Returns Object with command names as keys and default values as values.

cliOpts.options()

Returns

{
  alias: cliOpts.alias(),
  boolean: cliOpts.boolean(),
  default: cliOpts.default()
}

Example usage with minimist

var allowedOptions = {
  {
    name: 'verbose',
    abbr: 'v'
    alias: ['cry-at-me'],
    boolean: true,
    help: 'be verbose'
  },
  {
    name: 'path',
    abbr: 'p',
    help: 'path to the file'
  },
  {
    name: 'help',
    abbr: 'h',
    help: 'show help',
    boolean: true
  }
}

var clopts = require('cliclopts')(allowedOptions)

var argv = require('minimist')(process.argv.slice(2), {
  alias: clopts.alias(),
  boolean: clopts.boolean(),
  default: clopts.default(),
})

if(minimist.help) {
  console.log('Usage: command [options]')
  clopts.print()
  process.exit()
}

yourprogram(argv)

About

CLI options helper and usage printer

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages