← 全部工具

magicli

热度 65 更新于 开发与构建

Automagically generates command-line interfaces (CLI) for any module. Expected options and help sections are created automatically based on parameters names, with support to async. It can be installed globally, in order to *execute* any module, or .js fil

npmauto-collected

安装

npm
npm install -g magicli

通过 npm 安装。

MagiCLI

Automagically generates command-line interfaces (CLI), for any module. Just require('magicli')(); and your module is ready to be executed via CLI.

The main goal is to have any module prepared to be executed via CLI (installed globally with -g, or by using npx):

To see why I believe you should plug it on your module, even if you don't need a CLI (it probably will serve someone on the community), read here: Introducing MagiCLI: Automagically generates a command-line interface (CLI) for any module

It can be installed globally, in order to execute any module or .js file via CLI.

Goals

  • Minimal setup (one line)
  • Automatic options names based on functions parameters
  • Out of the box support to async functions (Promises, or any thenable lib)
  • A specific help section for each nested property ("subcommands")
  • Name, Description and Version extracted from package.json
  • Simple API to hook into the execution flow (stdin, before, after)
  • Cover all possible cases of module.exports (Function, Object with nested properties, Destructuring parameters)
  • Provide a CLI to be used to execute any given module or .js file via CLI

Usage (the most simple and minimal way)

  • npm install magicli
  • Add the property bin to your package.json containing the value ./bin/magicli.js
  • Create the file ./bin/magicli.js with the following content:
#!/usr/bin/env node

require('magicli')();

Done! Install your module with -g, or use it via npx, and run it with --help to see the result. The --version option will show the same value found at package.json. In the same way you can just run node ./bin/magicli.js --help to test it quickly, without installing it.

Let's suppose that your-module exports the function:

module.exports = function(param1, param2) {
    return param1 + param2;
}

When calling it via CLI, with --help, you will get:

Description:

  Same description found at package.json

Usage:

  $ your-module [options]

Options:

  --param1
  --param2

The program will be expecting options with the same name as the parameters declared at the exported function, and it doesn't need to follow the same order. Example:

$ your-module --param2="K" --param1="Z" would result in: ZK.