cli-ck
v0.2.1
Published
CLI framework for Node.js that's qui-ck and easy
Downloads
72
Maintainers
Readme
cli-ck
CLI framework for Node.js that's qui-ck and easy
cli-ck
provides a simple and flexible interface for creating cli apps in Node.js.
Inspired by the Yargs api, cli-ck
provides an API that is flexible and composeable,
making it a breeze to write simple cli tools or complex, interactive REPL's.
Synopsis
npm install cli-ck
easy.js
#!/usr/bin/env node
var Click = require('../lib/cli-ck')
var cli = new Click()
.description('demonstrates the cli-ck module')
.version('1.0.0')
.option('fruit', {
alias: 'f',
desc: 'Type of fruit',
choices: [ 'apple', 'banana', 'peach', 'pear' ]
})
.command('say', { desc: 'Say words in different ways' }, require('./say'))
.handler(function(args, opts) {
console.log('please choose a command')
})
cli.run(process.argv)
say.js
#!/usr/bin/env node
var Click = require('../lib/cli-ck')
var cli = new Click()
.usage('$0 [--volume {soft,medium,loud}] <...words>')
.option('volume', {
alias: 'v',
desc: 'how loud do you want to say it? [loud, medium, soft]',
choices: [ 'loud', 'medium', 'soft' ],
defaultValue: 'medium'
})
.handler(function (args, opts) {
if (opts.volume === 'loud') {
args = args.map(function(x) { return x.toUpperCase() })
} else if (opts.volume === 'soft') {
args = args.map(function(x) { return x.toLowerCase() })
}
console.log.apply(null, args)
})
cli.run(process.argv.slice(2))
Try these in your terminal:
~$ chmod u+x ./easy.js
~$ ./easy.js --help
~$ ./easy.js help
~$ ./easy.js help say
~$ ./easy.js say hi there
~$ ./easy.js say -v loud hey out there
~$ ./easy.js --repl
> help
> say hi
> exit
~$
Summary
- Simple, chaining interface for easy and clear cli specification
- Batteries included!
- Auto-generated help/usage output
- Default commands & options provided (
help
,exit
,--version
,--help
) - Robust validation of commands, options, and argument values
- Auto-included repl allows you to run your cli as an interactive repl
API
Running your program
Configuration
Options and Commands
var Click = require('cli-ck')
new Click(config)
Creates a new Click instance.
config
(optional):noHelp
: set to true to exclude help command
.parse(argv)
Parses a line and returns a parseResult
Parameters
argv
- CLI input to parse- expects
string
,array
, or instance ofArgConsumer
(internal class used for parsing)
- expects
Returns:
Most often you just want the args and opts parsed.
parse
returns an Object of the form:
{
args: [... non-option arguments ...],
opts: {... dict of optName: optValue ...},
command: string of full command e.g. 'say loudly',
context: <Click instance used to parse>
lastContext: <Click instance for actually executing command>
}
.run(argv)
Parses and runs the CLI handler for the given input.
run
will only the handler for the lowest-down sub-command parsed from the input.
Parameters
argv
- CLI input to parse- expects a
string
, or anarray
of strings
- expects a
.repl(argv)
Starts an interactive repl session using the Click instance's cli specification.
Each line submitted will execute Click.run()
on the line, and print the results.
Click has built-in tab-completion in the repl context. Click will do best effort completion for the following:
- command names
- option names
- option values (for options with choices specified)
You can also enter the repl mode by passing the --repl
in your input to .run()
.validate(argv)
.complete(argv)
CLI Metadata
Note that all methods from here down can be chained (i.e. they all return the Click instance)
.name(name)
.description(desc)
.version(versionStr)
.usage(usageStr)
.nargs(min, max)
.handler(handlerFn)
Parameters
handlerFn
: Handler function called when Click.run() is calledfunction cb(args, opts, argv, context, finalContext) { ... }
Options
.option(name, config)
Parameters
name
- name of optionconfig
- dict of configs for this option (see below)
Option's support the following config keys:
demand
/required
- Option is required. Calling run without opt value will throw error.desc
/describe
/description
- Text description of option's purposealias
- Eitherstring
orarray
of alternate names for option.choices
-array
of string or number values allowed for the option.defaultValue
- value assigned to option if not specifiedtype
-string
,count
,boolean
, ornumber
boolean
- set to true to set type toboolean
count
- set to true to set type tocount
number
- set to true to set type tonumber
string
- set to true to set type tostring
.optionSet(configs)
Sub-Commands
Click provides sophisticated support for sub-commands and an API that lets you easily compose multiple sub-cli's together to nest your commands as deep as you want.
When parsing a line, once a command token is encountered, the parser uses the commands context to parse the rest of the line. This means, e.g. that your commands can be configured to support or require options that your top level program does not.
.command(name, config, context)
Registers a command name with a given context.
Parameters
name
- name of commandconfig
- dict of configs for this option (see below)context
- CLI context for your sub-command- expects either
function(cli) {}
or aClick
instance
- expects either
Commands's support the following config keys:
desc
/describe
/description
- Text description of option's purpose
Example
var cli = new Click()
// passing in a Click instance as `context`
.command('foo', { 'desc': 'foo command' }, new Click()
.handler(function(args, opts) {
console.log('foo')
}))
// passing in a setupFn as `context`
.command('bar', { 'desc': 'bar command' }, function(barCli) {
barCli.handler(function(args, opts) {
console.log('bar')
}
})
You could also define and export your commands' sub-cli's in separate modules to make your top-level module much cleaner.
var cli = new Click()
// passing in a Click instance as `context`
.command('foo', { 'desc': 'foo command' }, require('./commands/foo')))
.command('bar', { 'desc': 'bar command' }, require('./commands/bar')))
Development
Main dev commands:
# setup
npm install
# compile src
npm run build
# watch src files and re-build on change
npm run watch
# run tests, outputs result to mocha-test.html
npm test
# watch compiled files, and re-run tests on change
npm run testwatch