项目作者: cb1kenobi

项目描述 :
Everything you need to create awesome Node.js command line interfaces
高级语言: JavaScript
项目地址: git://github.com/cb1kenobi/cli-kit.git
创建时间: 2016-10-28T01:09:55Z
项目社区:https://github.com/cb1kenobi/cli-kit

开源协议:MIT License

下载


cli-kit

A command line application toolkit for Node.js.

NPM Version
NPM Downloads
Build
Deps
Dev Deps

Features

  • Command line parsing
  • Support for dynamic command hierarchies
  • Auto-generated help
  • CLI template engine
  • External CLI extensions
  • Client and server for remote CLI session such as xterm.js
  • Automatic Node.js version enforcement

Installation

  1. npm install cli-kit --save

Usage

  1. import CLI from 'cli-kit';
  2. (async () => {
  3. const { argv, _ } = await new CLI({
  4. options: {
  5. '-f, --force': 'use the force',
  6. '--timeout [value]': {
  7. desc: 'the timeout duration',
  8. type: 'int'
  9. }
  10. }
  11. }).exec();
  12. console.log('options:', argv);
  13. console.log('args:', _);
  14. })();

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.

Pseudo-Terminal Support

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.

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
  1. const cli = new CLI({
  2. // An array of argument definitions. They are parsed in the order they are defined.
  3. args: [
  4. // An argument can be as simple as its name. Wrapping the name with `<` and `>` signifies
  5. // that the argument is required.
  6. '<arg1>',
  7. // To define an optional arguemnt, you can use `[` and `]`.
  8. '[arg2]',
  9. // Or simply omit the brackets
  10. 'arg3',
  11. // For more options, you can specify an argument descriptor
  12. {
  13. // The argument name. Follows the same rules as above.
  14. name: 'arg4',
  15. // The argument's description to show in the help output.
  16. desc: undefined,
  17. // When `true`, hides the argument from usage string in the help output.
  18. hidden: false,
  19. // When `true`, captures all subsequent argument values into an array
  20. multiple: false,
  21. // Overrides the brackets and forces the argument to be required or optional.
  22. required: false,
  23. // There are several built-in types. See the "types" section below for more info.
  24. type: 'string'
  25. },
  26. // Adding `...` will capture all subsequent argument values into an array
  27. 'arg4...'
  28. ],
  29. // Global flag to camel case property names derived from multi-word options/arguments.
  30. // Defaults to true, can be overwritten by the option/argument.
  31. camelCase: true,
  32. // An object of command names to command descriptors.
  33. commands: {
  34. 'some-command': {
  35. // The action to perform when the command is parsed.
  36. action({ argv, _ }) {
  37. console.log('options:', argv);
  38. console.log('args:', _);
  39. },
  40. // An array of alternate command names.
  41. aliases: [ 'another-command' ],
  42. // Command specific args. See `args` section above.
  43. args: [],
  44. // When `true`, camel case all option and argument names in the `argv` result.
  45. camelCase: true,
  46. // An object of subcommand names to subcommand descriptors.
  47. commands: {},
  48. // The command description.
  49. desc: undefined,
  50. // When `true`, hides the command from the help output.
  51. hidden: false,
  52. // An object of option formats to option descriptors. See the `options` section below.
  53. options: {},
  54. // The command name to display in the help output. Defaults to the command name.
  55. title: undefined
  56. }
  57. },
  58. // The default command `exec()` should run if no command was found during parsing.
  59. // If `help` is `true` and no default command is specified, it will default to displaying the
  60. // help screen. If you want help, but do not want to default to the help command, then set the
  61. // `defaultCommand` to `null`.
  62. defaultCommand: undefined,
  63. // The CLI description to print on the help screen between the usage and commands/options/args.
  64. desc: undefined,
  65. // Adds the `-h, --help` to the global flags and enables the auto-generated help screen.
  66. // Defaults to `true`.
  67. help: true,
  68. // The exit code to return when the help screen is displayed. This is useful if you want to
  69. // force the program to exit if `--help` is specified and the user is chaining commands together
  70. // or after displaying the help screen and prevent further execution in the CLI's promise chain.
  71. helpExitCode: undefined,
  72. // The name of the program used by the help screen to display the command's usage.
  73. // Defaults to "program".
  74. name: 'program',
  75. // An object of option formats to option descriptors or an array of sorted group names and
  76. // objects of option formats to option descriptors.
  77. options: {
  78. //
  79. },
  80. // The title for the top-level (or "Global") context. This title is displayed on the help screen
  81. // when displaying the list of options.
  82. title: 'Global',
  83. // When set, it will automatically wire up the `-v, --version` option. Upon calling with your
  84. // program with `--version`, it will display the version and exit with a success (zero) exit
  85. // code.
  86. version: null
  87. });

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
  1. cli.exec()
  2. .then(({ argv, _ }) => {
  3. console.log('options:', argv);
  4. console.log('args:', _);
  5. });

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
  1. // define a non-required argument "foo"
  2. cli.argument('foo');
  3. // define a non-required argument "wiz"
  4. cli.argument('[wiz]');
  5. // define a required argument "pow"
  6. cli.argument('<pow>');
  7. cli.argument({
  8. name: 'bar',
  9. type: 'int'
  10. });
  11. 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

Who Uses cli-kit?

License

MIT