| 1 | # Commander integration |
| 2 | 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. |
| 3 | |
| 4 | --- |
| 5 | |
| 6 | ## Docs |
| 7 | |
| 8 | ```ts |
| 9 | generateCompletionSpec(command[, options]): string |
| 10 | ``` |
| 11 | Generate a completion spec from the provided command and return it a string. |
| 12 | - `command`: a `commander.Command` object |
| 13 | - [`options`](#options): an object containing the following optional properties: |
| 14 | - [`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'` |
| 15 | |
| 16 | ```ts |
| 17 | addCompletionSpecCommand(command): void |
| 18 | ``` |
| 19 | Add a new Subcommand to the provided command that will print a spec generated when invoked through `$CLI generate-fig-spec`. |
| 20 | - `command`: a `commander.Command` object |
| 21 | |
| 22 | |
| 23 | ## Usage |
| 24 | Using this library is as simple as calling a function. |
| 25 | The snippet of code below generates a new spec each time the executable file is run. |
| 26 | ```js |
| 27 | import { program } from 'commander' |
| 28 | import { generateCompletionSpec } from '@fig/complete-commander' |
| 29 | |
| 30 | program |
| 31 | .name('babel') |
| 32 | .description('The compiler for writing next generation JavaScript') |
| 33 | .version('7.16.0') |
| 34 | .argument('<file>', 'file to compile') |
| 35 | |
| 36 | // Do not generate if production env |
| 37 | if (process.env.NODE_ENV === 'development') { |
| 38 | const spec = generateCompletionSpec(program) // write the generated spec to a file or print it to the console |
| 39 | } |
| 40 | ``` |
| 41 | |
| 42 | ### Custom command |
| 43 | 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. |
| 44 | |
| 45 | ```js |
| 46 | import { program } from 'commander' |
| 47 | import { addCompletionSpecCommand } from '@fig/complete-commander' |
| 48 | |
| 49 | program |
| 50 | .name('eslint') |
| 51 | .description('Find and fix problems in your JavaScript code') |
| 52 | .version('8.0.0') |
| 53 | .argument('<file>', 'file to lint') |
| 54 | |
| 55 | if (process.env.NODE_ENV === 'development') { |
| 56 | addCompletionSpecCommand(program) |
| 57 | } |
| 58 | program.parse() |
| 59 | ``` |
| 60 | |
| 61 | > _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._ |
| 62 | |
| 63 | ## Options for `generateCompletionSpec` |
| 64 | ### Specify the fig command name |
| 65 | 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. |
| 66 | |
| 67 | ```js |
| 68 | program |
| 69 | .name('eslint') |
| 70 | .description('Find and fix problems in your JavaScript code') |
| 71 | .version('8.0.0') |
| 72 | .argument('<file>', 'file to lint') |
| 73 | |
| 74 | if (process.env.NODE_ENV === 'development') { |
| 75 | program |
| 76 | .command('createSpec') // this command will not be included in the generated spec |
| 77 | .description('Generate a fig spec') |
| 78 | .action(() => { |
| 79 | generateCompletionSpec(program, { figSpecCommandName: 'createSpec' }) |
| 80 | }) |
| 81 | } |
| 82 | program.parse() |
| 83 | ``` |
| 84 | |
| 85 | --- |
| 86 | |
| 87 | _p.s. this tool has been written for `commander@8`, but should work in the most of the previous versions too._ |
| 88 | |
| 89 | ## Contributors |
| 90 | 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). |