@fig/complete-commander

中文文档
3.2.0发布于 2024/06/07MITTypeScriptCommonJS近 30 天下载 362.6万

Export commander command as a Fig spec

原始使用文档
文档目录6 节
  1. Docs
  2. Usage
  3. Custom command
  4. Options for generateCompletionSpec
  5. Specify the fig command name
  6. Contributors

Commander integration

A tool to speed up the workflow of converting Commander commands to Fig completion spec files.


Docs

generateCompletionSpec(command[, options]): string

Generate a completion spec from the provided command and return it a string.

  • command: a commander.Command object
  • options: an object containing the following optional properties:
    • figSpecCommandName: specify the name of the command used to generate the fig spec (see). It defaults to 'generate-fig-spec'
addCompletionSpecCommand(command): void

Add a new Subcommand to the provided command that will print a spec generated when invoked through $CLI generate-fig-spec.

  • command: a commander.Command object

Usage

Using this library is as simple as calling a function. The snippet of code below generates a new spec each time the executable file is run.

import { program } from 'commander'
import { generateCompletionSpec } from '@fig/complete-commander'

program
  .name('babel')
  .description('The compiler for writing next generation JavaScript')
  .version('7.16.0')
  .argument('<file>', 'file to compile')

// Do not generate if production env
if (process.env.NODE_ENV === 'development') {
  const spec = generateCompletionSpec(program) // write the generated spec to a file or print it to the console
}

Custom command

You can also provide a custom command that generates a spec file when called. For example, the program below generates the Fig spec when eslint generate-fig-spec is run from a Terminal.

import { program } from 'commander'
import { addCompletionSpecCommand } from '@fig/complete-commander'

program
  .name('eslint')
  .description('Find and fix problems in your JavaScript code')
  .version('8.0.0')
  .argument('<file>', 'file to lint')

if (process.env.NODE_ENV === 'development') {
  addCompletionSpecCommand(program)
}
program.parse()

NOTE: the command can be called whatever you want, but if you specify a name different from 'generate-fig-spec' you should set the figSpecCommandName option.

Options for generateCompletionSpec

Specify the fig command name

When creating a custom command to generate a Fig spec whose name is different from 'generateCompletionSpec' you want to avoid including it in the generated file. This option helps you do that.

program
  .name('eslint')
  .description('Find and fix problems in your JavaScript code')
  .version('8.0.0')
  .argument('<file>', 'file to lint')

if (process.env.NODE_ENV === 'development') {
  program
    .command('createSpec') // this command will not be included in the generated spec
    .description('Generate a fig spec')
    .action(() => {
      generateCompletionSpec(program, { figSpecCommandName: 'createSpec' })
    })
}
program.parse()

p.s. this tool has been written for commander@8, but should work in the most of the previous versions too.

Contributors

This integration was built by Federico Ciardi. Please see his repo for previous git history: https://github.com/fedeci/commander-to-fig.

版本与发布记录

@fig/complete-commander 共发布 5 个版本,首次发布于 2022/05/10;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.2.0latest2024/06/072 年前
3.1.02024/06/012 年前
3.0.02023/11/222 年前
2.0.12022/09/064 年前
2.0.02022/05/104 年前

关于 @fig/complete-commander 的常见问题

@fig/complete-commander 是什么?

Export commander command as a Fig spec

如何安装 @fig/complete-commander?

在项目目录运行 npm install @fig/complete-commander,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:commander(^11.1.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@fig/complete-commander 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@fig/complete-commander 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@fig/complete-commander 可以商用吗?

@fig/complete-commander 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@fig/complete-commander 的下载量有多少?

根据 npm 官方下载统计,@fig/complete-commander 在 2026/10/15 – 2026/11/13 这 30 天内被下载 3,626,139 次,日均约 120,871 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fig/complete-commander 还在维护吗?

最新版本 3.2.0 发布于 2024/06/07(2 年前);至今共发布 5 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/05/10。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。