0.4.0-beta2 • Published 8 years ago

cli-kit v0.4.0-beta2

Weekly downloads
800
License
MIT
Repository
github
Last release
8 years ago

Note: cli-kit is under development.

cli-kit

Greenkeeper badge

NPM Version NPM Downloads Travis CI Build Test Coverage Code Climate Deps Dev Deps

Everything you need to make awesome Command Line Interfaces.

Features

  • Command line parsing
  • Support for command hierarchies
  • Auto-generated help
  • CLI template engine
  • Markdown/kramdown rendering
  • Extension support (e.g. wrap external CLI's)

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)

  • opts : Object (optional)

    Various options to initialize the CLI instance.

Example
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,

			// Overrides the brackets and forces the argument to be required or optional.
			required: false,

			// There are several built-in types. See the "types" section below for more info.
			type: 'string'
		},

		// Adding `...` will capture all subsequent argument values into an array
		'arg4...'
	],

	// Global flag to camel case property names derived from multi-word options/arguments.
	// Defaults to true, can be overwritten by the option/argument.
	camelCase: true,

	// An object of command names to command descriptors.
	commands: {
		'some-command': {
			// The action to perform when the command is parsed.
			action({ argv, _ }) {
				console.log('options:', argv);
				console.log('args:', _);
			},

			// An array of alternate command names.
			aliases: [ 'another-command' ],

			// Command specific args. See `args` section above.
			args: [],

			// When `true`, camel case all option and argument names in the `argv` result.
			camelCase: true,

			// An object of subcommand names to subcommand descriptors.
			commands: {},

			// The command description.
			desc: undefined,

			// When `true`, hides the command from the help output.
			hidden: false,

			// An object of option formats to option descriptors. See the `options` section below.
			options: {},

			// The command name to display in the help output. Defaults to the command name.
			title: undefined
		}
	},

	// The default command `exec()` should run if no command was found during parsing.
	// If `help` is `true` and no default command is specified, it will default to displaying the
	// help screen. If you want help, but do not want to default to the help command, then set the
	// `defaultCommand` to `null`.
	defaultCommand: undefined,

	// The CLI description to print on the help screen between the usage and commands/options/args.
	desc: undefined,

	// Adds the `-h, --help` to the global flags and enables the auto-generated help screen.
	// Defaults to `true`.
	help: true,

	// The exit code to return when the help screen is displayed. This is useful if you want to
	// force the program to exit if `--help` is specified and the user is chaining commands together
	// or after displaying the help screen and prevent further execution in the CLI's promise chain.
	helpExitCode: undefined,

	// The name of the program used by the help screen to display the command's usage.
	// Defaults to "program".
	name: 'program',

	// An object of option formats to option descriptors or an array of sorted group names and
	// objects of option formats to option descriptors.
	options: {
		//
	},

	// The title for the top-level (or "Global") context. This title is displayed on the help screen
	// when displaying the list of options.
	title: 'Global',

	// When set, it will automatically wire up the `-v, --version` option. Upon calling with your
	// program with `--version`, it will display the version and exit with a success (zero) exit
	// code.
	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
// 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

cli-kit vs other libraries

NOTE: Following data is incomplete and research is pending.

Parsers

cli-kitcommander.jsyargsCaporal.jsn-argsoclifmeowdashdashnomnomoptimistminimistmri
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:
LanguageJavaScriptJavaScriptJavaScriptJavaScriptJavaScriptTypeScriptJavaScriptJavaScriptJavaScriptJavaScriptJavaScript
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:white_check_mark::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:white_check_mark::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 onlyNumbers and Booleans
Parsed value transforming:white_check_mark:
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-kitCaporal.jsoclifmeowinquirerpromptlyfields
Actively maintained (within last year):white_check_mark::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:
LanguageJavaScriptJavaScriptTypeScriptJavaScriptJavaScriptJavaScriptJavaScript
Automatic prompt value data type coercionComing soon!:x:
Prompt value transformingComing soon!:white_check_mark: (via filters)
Prompt value validationComing soon!:white_check_mark:
Built-in prompt typesComing soon!:white_check_mark::white_check_mark:
Internal hook system:white_check_mark::x::x:

Who Uses cli-kit?

License

MIT

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.16.0

4 years ago

1.15.0

4 years ago

1.14.1

5 years ago

1.14.0

5 years ago

1.13.0

5 years ago

1.11.4

5 years ago

1.11.3

5 years ago

1.12.0

5 years ago

1.11.2

5 years ago

1.11.1

5 years ago

1.11.0

5 years ago

1.10.0

5 years ago

1.9.3

5 years ago

1.9.2

5 years ago

1.9.1

5 years ago

1.9.0

5 years ago

1.8.9

6 years ago

1.8.8

6 years ago

1.8.7

6 years ago

1.8.6

6 years ago

1.8.5

6 years ago

1.8.4

6 years ago

1.8.3

6 years ago

1.8.2

6 years ago

1.8.1

6 years ago

1.8.0

6 years ago

1.7.0

6 years ago

1.6.2

6 years ago

1.6.1

6 years ago

1.6.0

6 years ago

1.5.2

6 years ago

1.5.1

6 years ago

1.5.0

6 years ago

1.4.0

6 years ago

1.3.0

6 years ago

1.2.4

6 years ago

1.2.3

6 years ago

1.2.2

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.16.1

6 years ago

0.16.2

6 years ago

0.16.0

6 years ago

0.15.2

6 years ago

0.15.1

6 years ago

0.15.0

6 years ago

0.14.0

6 years ago

0.13.2

6 years ago

0.13.1

6 years ago

0.13.0

6 years ago

0.12.1

6 years ago

0.12.0

7 years ago

0.11.2

7 years ago

0.11.1

7 years ago

0.11.0

7 years ago

0.10.3

7 years ago

0.10.2

7 years ago

0.10.1

7 years ago

0.10.0

7 years ago

0.9.0

7 years ago

0.8.2

7 years ago

0.8.1

7 years ago

0.8.0

7 years ago

0.7.5

7 years ago

0.7.4

7 years ago

0.7.3

7 years ago

0.7.2

7 years ago

0.7.1

7 years ago

0.7.0

7 years ago

0.6.2

7 years ago

0.6.1

7 years ago

0.6.0

7 years ago

0.5.4

7 years ago

0.5.3

7 years ago

0.5.2

7 years ago

0.5.1

7 years ago

0.5.0

7 years ago

0.4.1

7 years ago

0.4.0

8 years ago

0.4.0-beta5

8 years ago

0.4.0-beta4

8 years ago

0.4.0-beta3

8 years ago

0.4.0-beta2

8 years ago

0.4.0-beta

8 years ago

0.3.0

8 years ago

0.2.0

8 years ago

0.1.2

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago

0.0.12

8 years ago

0.0.11

8 years ago

0.0.10

8 years ago

0.0.9

8 years ago

0.0.8

8 years ago

0.0.7

9 years ago

0.0.6

9 years ago

0.0.5

9 years ago

0.0.4

9 years ago

0.0.3

9 years ago

0.0.2

9 years ago

0.0.1

9 years ago

0.0.0

11 years ago