Skip to content

Commit

Permalink
feat: add completions as cli method (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
carloscortonc committed Mar 10, 2024
1 parent f6b5d38 commit 4654dba
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ npm install cli-er
- [**run(args?)**](./docs/api.md#runargs): parse the given arguments and execute the corresponding script found in the calculated location. Integrates help and version generation.
- [**help(location?)**](./docs/api.md#helplocation): generate help based on the definition. Can be scoped to a namespace/command.
- [**version()**](./docs/api.md#version): generate a formatted version of the application's version.
- [**completions()**](./docs/features.md#bash-completion): output bash-completion script contents.

#### Glossary
- **Namespace**: is used to group commands, but cannot be invoked. Can contain other namespaces, commands or options.
Expand Down
7 changes: 6 additions & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,9 @@ _**TIP**: any `DefinitionElement` can be hidden from the generated help by using
Prints the formatted version of the current cli application: finds the package.json for the current application, and prints its name and version.

> **Note**
> version-generation option is auto-included by default. This can be configured via [`CliOptions.version`](/docs/cli-options.md#versionautoinclude)
> version-generation option is auto-included by default. This can be configured via [`CliOptions.version`](/docs/cli-options.md#versionautoinclude)

## completions()

Generates and outputs bash-completion script contents. This can instead be included as a command and be managed by `Cli.run`, check: [`bash completion`](/docs/features.md#bash-completion)
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import CliLogger from "./cli-logger";
import { ERROR_MESSAGES } from "./cli-errors";
import { CLI_MESSAGES, formatMessage } from "./cli-messages";
import { defineCommand, CommandOptions } from "./extract-options-type";
import { generateCompletions } from "./bash-completion";

export default class Cli {
static logger: ICliLogger = new CliLogger();
Expand Down Expand Up @@ -175,6 +176,12 @@ export default class Cli {
version() {
formatVersion(this.options);
}
/**
* Output bash-completion script contents
*/
completions() {
generateCompletions({ definition: this.definition, cliOptions: this.options });
}
}

// Export of types not used anywhere in the codebase
Expand Down

0 comments on commit 4654dba

Please sign in to comment.