# @fig/complete-commander

> Export commander command as a Fig spec

Latest version **3.2.0** (published 2024-06-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install @fig/complete-commander
pnpm add @fig/complete-commander
yarn add @fig/complete-commander
bun add @fig/complete-commander
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.2.0 |
| Published | 2024-06-06 |
| First published | 2022-05-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 11.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 58 |
| Author | Federico Ciardi |
| Maintainers | superbryntendo, chaynabors, grant0417, brendanfalk |
| Keywords | commander, fig, spec, generator, plugin, integration |

## Links

- npm: https://www.npmjs.com/package/@fig/complete-commander
- Repository: https://github.com/withfig/autocomplete-tools
- Homepage: https://github.com/withfig/autocomplete-tools#readme
- Issues: https://github.com/withfig/autocomplete-tools/issues
- npm.io page: https://npm.io/package/@fig/complete-commander

## Dependencies (1)

- [prettier](https://npm.io/package/prettier.md) ^3.2.5

## Alternatives

- [jsforce](https://npm.io/package/jsforce.md) — 851.2K weekly downloads
- [react-native-qrcode-svg](https://npm.io/package/react-native-qrcode-svg.md) — 693.5K weekly downloads
- [@salesforce/plugin-data](https://npm.io/package/@salesforce/plugin-data.md) — 394.9K weekly downloads
- [@backstage/plugin-search-common](https://npm.io/package/@backstage/plugin-search-common.md) — 308.5K weekly downloads
- [@chain-registry/types](https://npm.io/package/@chain-registry/types.md) — 38.4K weekly downloads

## Recent versions

- 3.2.0 (latest) — 2024-06-06
- 3.1.0 — 2024-05-31
- 3.0.0 — 2023-11-21
- 2.0.1 — 2022-09-05
- 2.0.0 — 2022-05-10

## README

# Commander integration
A tool to speed up the workflow of converting [Commander](https://github.com/tj/commander.js) commands to [Fig](https://github.com/withfig/autocomplete) completion spec files.

---

## Docs

```ts
generateCompletionSpec(command[, options]): string
```
Generate a completion spec from the provided command and return it a string.
- `command`: a `commander.Command` object
- [`options`](#options): an object containing the following optional properties:
  - [`figSpecCommandName`](#specify-the-fig-command-name): specify the name of the command used to generate the fig spec ([see](#custom-command)). It defaults to `'generate-fig-spec'`

```ts
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.
```js
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.

```js
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`](#specify-the-fig-command-name) option._

## Options for `generateCompletionSpec`
### Specify the fig command name
When creating a [custom command](#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.

```js
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](https://github.com/fedeci/). Please see his repo for previous git history: [https://github.com/fedeci/commander-to-fig](https://github.com/fedeci/commander-to-fig).

---
_Source: https://npm.io/package/@fig/complete-commander · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
