{"id":19696281,"url":"https://github.com/axiosleo/node-cli","last_synced_at":"2025-04-29T11:31:59.733Z","repository":{"id":37683515,"uuid":"312452834","full_name":"AxiosLeo/node-cli","owner":"AxiosLeo","description":"Design for quickly developing CLI applications using Node.js","archived":false,"fork":false,"pushed_at":"2025-03-20T08:24:15.000Z","size":396,"stargazers_count":10,"open_issues_count":0,"forks_count":4,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-05T15:11:17.294Z","etag":null,"topics":["cli","cli-tool","node","node-cli","nodejs"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AxiosLeo.png","metadata":{"files":{"readme":"README-CN.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-11-13T02:33:20.000Z","updated_at":"2025-03-20T08:24:19.000Z","dependencies_parsed_at":"2023-12-27T15:33:57.437Z","dependency_job_id":"c38a3406-16ce-4f2c-9495-e650d713a805","html_url":"https://github.com/AxiosLeo/node-cli","commit_stats":{"total_commits":457,"total_committers":5,"mean_commits":91.4,"dds":"0.013129102844638973","last_synced_commit":"94a37a1e80df291b21a3c8e6cfee64b56e9cbc82"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AxiosLeo%2Fnode-cli","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AxiosLeo%2Fnode-cli/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AxiosLeo%2Fnode-cli/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AxiosLeo%2Fnode-cli/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AxiosLeo","download_url":"https://codeload.github.com/AxiosLeo/node-cli/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251493974,"owners_count":21598207,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cli","cli-tool","node","node-cli","nodejs"],"created_at":"2024-11-11T19:34:29.410Z","updated_at":"2025-04-29T11:31:59.037Z","avatar_url":"https://github.com/AxiosLeo.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @axiosleo/cli-tool\n\n[English](/README.md) | 简体中文\n\n[![NPM version](https://img.shields.io/npm/v/@axiosleo/cli-tool.svg?style=flat-square)](https://npmjs.org/package/@axiosleo/cli-tool)\n[![npm download](https://img.shields.io/npm/dm/@axiosleo/cli-tool.svg?style=flat-square)](https://npmjs.org/package/@axiosleo/cli-tool)\n[![node version](https://img.shields.io/badge/node.js-%3E=_14.0-green.svg?style=flat-square)](http://nodejs.org/download/)\n[![CI Build Status](https://github.com/AxiosLeo/node-cli/actions/workflows/ci.yml/badge.svg)](https://github.com/AxiosLeo/node-cli/actions/workflows/ci.yml)\n[![](https://codecov.io/gh/AxiosLeo/node-cli/branch/master/graph/badge.svg)](https://codecov.io/gh/AxiosLeo/node-cli)\n[![License](https://img.shields.io/github/license/AxiosLeo/node-cli?color=%234bc524)](LICENSE)\n[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2FAxiosLeo%2Fnode-cli.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2FAxiosLeo%2Fnode-cli/refs/branch/master)\n\n\u003e 为了使用 Node.js 快速开发命令行应用而设计\n\u003e\n\u003e 详见文档 [wiki](https://github.com/AxiosLeo/node-cli/wiki)\n\n## 安装\n\n```bash\nnpm install @axiosleo/cli-tool\n```\n\n## 快速初始化应用\n\n```bash\nnpm install @axiosleo/cli-tool -g\n\ncli-tool init \u003capp-name\u003e\n\n# 生成命令行脚本文件\ncli-tool make \u003ccommand-name\u003e \u003ccommands-dir-path\u003e\n# 示例\ncli-tool make test ./commands/ # 将会生成 ./commands/test.js 文件\n\n# 直接执行生成的命令行脚本文件\ncli-tool exec ./command/test.js\n```\n\n## 使用\n\n### 应用入口程序\n\n```js\nconst { App } = require('@axiosleo/cli-tool');\nconst app = new App({\n  name: 'cli-tool',      // 命令行应用的名称, 必须\n  desc: 'cli app description',\n  version: '1.0.0',      // 命令行应用的版本, 必须\n  commands_dir: '/path/to/commands/dir/', // 将会自动加载目录内的命令行脚本文件\n  commands_sort: ['help', ... ],\n  commands_group: {\n    'group description': ['command_name', ...],\n  }\n});\napp.start();\n```\n\n### 执行单个命令\n\n- 通过 Command 类注册命令\n\n```js\nconst CommandExample = require('/path/to/your/command/file');\napp.register(CommandExample);\n\napp.exec(\"\u003ccommand-name\u003e\");\n```\n\n- 通过 Command 对象注册命令\n\n```js\nconst CommandExample = require('/path/to/your/command/file');\nconst command = new CommandExample();\napp.register(command);\n\napp.exec(\"\u003ccommand-name\u003e\");\n```\n\n- 通过 Command 文件地址注册命令\n\n```js\napp.register('/path/to/your/command/file');\n\napp.exec(\"\u003ccommand-name\u003e\");\n```\n\n## 多语言\n\n\u003e 开启多语言模式后，应用和命令的“描述”将会根据配置的 locales json 文件，自动翻译成对应语言\n\u003e\n\u003e locales 示例 json 文件 : [locales](./locales)\n\u003e\n\u003e 详见 [locales wiki](https://github.com/AxiosLeo/node-cli/wiki/locales)\n\n```js\nconst path = require('path');\napp.locale({\n  dir: path.join(__dirname, '../locales'), // /path/to/app/locales/dir\n  sets: ['en-US', 'zh-CN'],                // 不能为空, 且第一个元素为默认值\n});\napp.start(); // 需要在调用 app.start() 方法前配置 locale\n```\n\n### 命令类示例\n\n```js\n'use strict';\n\nconst { Command } = require('@axiosleo/cli-tool');\n\nclass CommandExample extends Command {\n  constructor() {\n    super({\n      name: 'command-name',\n      desc: 'command desc',\n      alias: ['command-alia1', 'command-alia2'],\n    });\n\n    /**\n     * add argument of current command\n     * @param name argument name\n     * @param desc argument description\n     * @param mode argument mode : required | optional\n     * @param default_value only supported on optional mode\n     */\n    this.addArgument('arg-name', 'desc', 'required', null);\n\n    /**\n     * add option of current command\n     * @param name option name\n     * @param short option short name\n     * @param desc option description\n     * @param mode option mode : required | optional\n     * @param default_value only supported on optional mode\n     */\n    this.addOption('test', 't', 'desc', 'required', null);\n  }\n\n  async exec(args, options, argList, app) {\n      // do something in here\n\n      // get arg\u0026option by name\n      const arg1 = args.argName;\n      const option1 = options.optionName;\n\n      // get arg by index\n      const index = 0;\n      const arg2 = argList[index];\n\n      // ask for answer\n      const answer = await this.ask('Please input your answer');\n\n      // ask for confirm, default value is 'false'\n      const confirm = await this.confirm('Confirm do this now?', false);\n\n      // select action\n      const action = await this.select('Select an action', ['info', 'update']);\n\n      // print table\n      const rows = [\n        ['Bob', 2]\n      ];\n      const head = ['Name', 'Score'];\n      this.table(rows, head);\n  }\n}\n\nmodule.exports = CommandExample;\n```\n\n## 许可证\n\n本项目基于 [MIT](LICENSE) 开源协议.\n\n[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2FAxiosLeo%2Fnode-cli.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2FAxiosLeo%2Fnode-cli/refs/branch/master/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faxiosleo%2Fnode-cli","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faxiosleo%2Fnode-cli","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faxiosleo%2Fnode-cli/lists"}