Note: cli-kit is under development.
cli-kit

Everything you need to make awesome Command Line Interfaces.
Features
- Command line parsing
- Support for command hierarchies
- Auto-generated help
Installation
yarn add cli-kit --save
or
npm i cli-kit --save
Usage
import CLI from 'cli-kit';
new CLI({
options: {
'-f, --force': 'use the force',
'--timeout [value]': {
desc: 'the timeout duration',
type: 'int'
}
}
})
.exec()
.then(({ argv, _ }) => {
console.log('options:', argv);
console.log('args:', _);
})
.catch(console.error);
Architecture
In cli-kit, commands and options are grouped into "contexts". The main CLI instance defines the
"global context". Each command defines a new context. Each context can have its own commands,
options, and arguments. What you end up with is a hierarchy of contexts.
When cli-kit parses the command line arguments, it will check each argument against the global
context to see if the argument can be identified as a known command, option, or argument. If it
finds a command, it adds the command's context to a stack and re-parses any unidentified arguments.
This allows you to create deep and dynamic hierarchies of commands, options, and arguments.
API
class CLI
A CLI
intance defines a global context for which you add commands, options, and arguments.
Extends Context
> HookEmitter
.
constuctor(opts)
Example
const cli = new CLI({
args: [
'<arg1>',
'[arg2]',
'arg3',
{
name: 'arg4',
desc: undefined,
hidden: false,
multiple: false,
required: false,
type: 'string'
},
'arg4...'
],
camelCase: true,
commands: {
'some-command': {
action({ argv, _ }) {
console.log('options:', argv);
console.log('args:', _);
},
aliases: [ 'another-command' ],
args: [],
camelCase: true,
commands: {},
desc: undefined,
hidden: false,
options: {},
title: undefined
}
},
defaultCommand: undefined,
desc: undefined,
help: true,
helpExitCode: undefined,
name: 'program',
options: {
},
title: 'Global',
version: null
});
exec(args)
Parses the command line args and executes a command, if found.
-
args
: Array<String>
(optional)
An array of arguments. Each argument is expected to be a string.
Defaults to process.argv.slice(2)
.
Returns a Promise
that resolves an Arguments
object. This object will contain the parsed options
in argv
and arguments in _
.
Example
cli.exec()
.then(({ argv, _ }) => {
console.log('options:', argv);
console.log('args:', _);
});
class Context
Base class for CLI
and Command
classes.
Extends HookEmitter
.
argument(arg)
Adds an argument to a CLI
or Command
.
-
arg
: Argument
, Object
, or String
.
An argument descriptor. Either an Argument
instance or an Object
to pass into a Argument
constructor.
An argument requires a name
.
Returns a reference to the CLI
or Command
.
Example
cli.argument('foo');
cli.argument('[wiz]');
cli.argument('<pow>');
cli.argument({
name: 'bar',
type: 'int'
});
cli.argument(new Argument('baz'));
command(cmd, opts)
Adds a command to a CLI
or Command
.
TODO
option(optOrFormat, group, params)
Adds an option or group of options to a CLI
or Command
.
TODO
cli-kit vs other libraries
NOTE: Following data is incomplete and research is pending.
Parsers
| cli-kit | commander.js | yargs | Caporal.js | n-args | oclif | meow | dashdash | nomnom | optimist | minimist | mri |
---|
Actively maintained (within last year) | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | | :white_check_mark: | :warning: Last release Nov 2016 | :x: | :x: | :x: | |
Open Source | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :x: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | |
Language | JavaScript | JavaScript | JavaScript | JavaScript | JavaScript | TypeScript | JavaScript | JavaScript | JavaScript | JavaScript | JavaScript | |
Parse callbacks | :white_check_mark: | | | | :x: | | | | | | | |
Command support | :white_check_mark: | :white_check_mark: | :white_check_mark: | | :x: | :white_check_mark: | | | | | | |
Options support | :white_check_mark: | :white_check_mark: | :white_check_mark: | | :white_check_mark: | | | | | | | |
Option validation | Coming soon! | :white_check_mark: | | | :white_check_mark: | | | | | | | |
Default values | :white_check_mark: | | | | | | | | | | | |
Flags support | :white_check_mark: | :white_check_mark: | :white_check_mark: | | :x: | | | | | | | |
Argument support | :white_check_mark: | :white_check_mark: | :white_check_mark: | | :white_check_mark: | | | | | | | |
Argument validation | Coming soon! | :white_check_mark: | | | :white_check_mark: | | | | | | | |
Dynamic command hierarchies | :white_check_mark: | | | | | | | | | | | |
Automatic parsed value data type coercion | :white_check_mark: | :white_check_mark: | Numbers only | | Numbers and Booleans | | | | | | | |
Parsed value transforming | Coming soon! | | | | | | | | | | | |
Auto-generated help screen | :white_check_mark: | :white_check_mark: | | | :x: | | :white_check_mark: | | | | | |
Help exits with code | :white_check_mark: | | | | | | :white_check_mark: | | | | | |
Internal hook system | :white_check_mark: | | | | | | | | | | | |
Prompters
| cli-kit | Caporal.js | oclif | meow | inquirer | promptly | fields |
---|
Actively maintained (within last year) | :white_check_mark: | :white_check_mark: | | :white_check_mark: | :white_check_mark: | :white_check_mark: | :x: |
Open Source | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: |
Language | JavaScript | JavaScript | TypeScript | JavaScript | JavaScript | JavaScript | JavaScript |
Automatic prompt value data type coercion | Coming soon! | | | | :x: | | |
Prompt value transforming | Coming soon! | | | | :white_check_mark: (via filters) | | |
Prompt value validation | Coming soon! | | | | :white_check_mark: | | |
Built-in prompt types | Coming soon! | | | | :white_check_mark: | | :white_check_mark: |
Internal hook system | :white_check_mark: | | | | :x: | | :x: |
License
MIT