package
Everything you need to create awesome command line interfaces
npm install cli-kitGitHub-flavored package documentation.
Across all versions, last 30 days.
9,270 in 30 days
Runtime metadata.
Tagged topics.
A command line application toolkit for Node.js.
npm install cli-kit --saveimport CLI from 'cli-kit';
(async () => {
const { argv, _ } = await new CLI({
options: {
'-f, --force': 'use the force',
'--timeout [value]': {
desc: 'the timeout duration',
type: 'int'
}
}
}).exec();
console.log('options:', argv);
console.log('args:', _);
})();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.
cli-kit extensions can be native binary executables or other Node.js scripts. When the extension is
a native executable, then it is executed using Node's spawn(). Note that spawned child processes
do not have a TTY and thus things like prompting will not work.
CLIA CLI intance defines a global context for which you add commands, options, and arguments.
Extends Context > HookEmitter.
constuctor(opts)opts : Object (optional)
Various options to initialize the CLI instance.
const cli = new CLI({
// An array of argument definitions. They are parsed in the order they are defined.
args: [
// An argument can be as simple as its name. Wrapping the name with `<` and `>` signifies
// that the argument is required.
'<arg1>',
// To define an optional arguemnt, you can use `[` and `]`.
'[arg2]',
// Or simply omit the brackets
'arg3',
// For more options, you can specify an argument descriptor
{
// The argument name. Follows the same rules as above.
name: 'arg4',
// The argument's description to show in the help output.
desc: undefined,
// When `true`, hides the argument from usage string in the help output.
hidden: false,
// When `true`, captures all subsequent argument values into an array
multiple: false,
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 _.
cli.exec()
.then(({ argv, _ }) => {
console.log('options:', argv);
console.log('args:', _);
});ContextBase 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.
// define a non-required argument "foo"
cli.argument('foo');
// define a non-required argument "wiz"
cli.argument('[wiz]');
// define a required argument "pow"
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
MIT
cb1kenobi
chris@cb1inc.com
Estimated unpacked size across 16 packages.
596.1 kB
Includes dependencies.
^4.3.0
4.3.0
^1.0.12
1.0.16