Skip to content

Commit 4649b92

Browse files
renancaraujoScarlett Elizafelangel
authored
docs: update readme and docs (#32)
Co-authored-by: Scarlett Eliza <[email protected]> Co-authored-by: Felix Angelov <[email protected]>
1 parent d30f59e commit 4649b92

File tree

5 files changed

+162
-33
lines changed

5 files changed

+162
-33
lines changed

.github/cspell.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"version": "0.2",
3+
"$schema": "https://raw.githubusercontent.com/streetsidesoftware/cspell/main/cspell.schema.json",
4+
"dictionaries": ["vgv_allowed", "vgv_forbidden"],
5+
"dictionaryDefinitions": [
6+
{
7+
"name": "vgv_allowed",
8+
"path": "https://raw.githubusercontent.com/verygoodopensource/very_good_dictionaries/main/allowed.txt",
9+
"description": "Allowed VGV Spellings"
10+
},
11+
{
12+
"name": "vgv_forbidden",
13+
"path": "https://raw.githubusercontent.com/verygoodopensource/very_good_dictionaries/main/forbidden.txt",
14+
"description": "Forbidden VGV Spellings"
15+
}
16+
],
17+
"useGitignore": true,
18+
19+
"words": [
20+
"subcommand",
21+
"inverseflag",
22+
"trueflag"
23+
]
24+
}

.github/workflows/main.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ jobs:
1616
semantic_pull_request:
1717
uses: VeryGoodOpenSource/very_good_workflows/.github/workflows/semantic_pull_request.yml@v1
1818

19+
spell-check:
20+
uses: VeryGoodOpenSource/very_good_workflows/.github/workflows/spell_check.yml@v1
21+
with:
22+
includes: "**/*.md"
23+
modified_files_only: false
24+
1925
build:
2026
uses: VeryGoodOpenSource/very_good_workflows/.github/workflows/dart_package.yml@v1
2127

README.md

Lines changed: 56 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,92 @@
1-
# Cli Completion
21

2+
# CLI Completion
3+
4+
[![Very Good Ventures][logo_white]][very_good_ventures_link_dark]
5+
[![Very Good Ventures][logo_black]][very_good_ventures_link_light]
6+
7+
[![pub package][pub_badge]][pub_link]
38
[![style: very good analysis][very_good_analysis_badge]][very_good_analysis_link]
4-
[![Powered by Mason](https://img.shields.io/endpoint?url=https%3A%2F%2Ftinyurl.com%2Fmason-badge)](https://github.com/felangel/mason)
59
[![License: MIT][license_badge]][license_link]
610

7-
Completion toolkit for Command runner based Dart CLIs
11+
---
812

9-
## Installation 💻
13+
Completion functionality for Dart Command-Line Interfaces built using CommandRunner.
1014

11-
**❗ In order to start using Cli Completion you must have the [Dart SDK][dart_install_link] installed on your machine.**
15+
Developed with 💙 by [Very Good Ventures][very_good_ventures_link] 🦄
1216

13-
Add `cli_completion` to your `pubspec.yaml`:
1417

15-
```yaml
16-
dependencies:
17-
cli_completion:
18-
```
18+
## Installation 💻
1919

20-
Install it:
20+
**❗ In order to start using CLI Completion you must have the [Dart SDK][dart_install_link] installed
21+
on your machine.**
2122

22-
```sh
23-
dart pub get
23+
```
24+
flutter pub add cli_completion
2425
```
2526

26-
---
2727

28-
## Continuous Integration 🤖
2928

30-
Cli Completion comes with a built-in [GitHub Actions workflow][github_actions_link] powered by [Very Good Workflows][very_good_workflows_link] but you can also add your preferred CI/CD solution.
29+
## Usage ✨
3130

32-
Out of the box, on each pull request and push, the CI `formats`, `lints`, and `tests` the code. This ensures the code remains consistent and behaves correctly as you add functionality or make changes. The project uses [Very Good Analysis][very_good_analysis_link] for a strict set of analysis options used by our team. Code coverage is enforced using the [Very Good Workflows][very_good_coverage_link].
31+
On your `CommandRunner` class, extend `CompletionCommandRunner` :
3332

34-
---
33+
```dart
34+
import 'package:cli_completion/cli_completion.dart';
3535
36-
## Running Tests 🧪
36+
class ExampleCommandRunner extends CompletionCommandRunner<int> {
37+
...
38+
```
39+
This will make the first command run to install the completion files automatically. To disable that behavior, set `enableAutoInstall` to false:
40+
41+
```dart
42+
class ExampleCommandRunner extends **CompletionCommandRunner**<int> {
43+
44+
@override
45+
bool get enableAutoInstall => false;
46+
...
47+
```
3748

38-
To run all unit tests:
49+
When `enableAutoInstall` is set to false, users will have to call `install-completion-files` to install these files manually.
3950

40-
```sh
41-
dart pub global activate coverage 1.2.0
42-
dart test --coverage=coverage
43-
dart pub global run coverage:format_coverage --lcov --in=coverage --out=coverage/lcov.info
51+
```bash
52+
$ example_cli install-completion-files
4453
```
4554

46-
To view the generated coverage report you can use [lcov](https://github.com/linux-test-project/lcov).
55+
## Documentation 📝
56+
57+
For an overview of how this package works, check out the [documentation][docs_link].
58+
59+
---
60+
61+
### ⚠️ Using analytics
62+
63+
Handling completion requests should be straightforward.
64+
65+
If there are any checks (like analytics, telemetry, or anything that you may have on `run` or `runCommand` overrides) before running subcommands, make sure you fast track the `completion` command to skip all of the unnecessary computations.
4766

48-
```sh
49-
# Generate Coverage Report
50-
genhtml coverage/lcov.info -o coverage/
67+
Example:
5168

52-
# Open Coverage Report
53-
open coverage/index.html
69+
```dart
70+
@override
71+
Future<int?> runCommand(ArgResults topLevelResults) async {
72+
if (topLevelResults.command?.name == 'completion') {
73+
super.runCommand(topLevelResults);
74+
return;
75+
}
76+
// ... analytics and other unrelated stuff
5477
```
5578

5679
[dart_install_link]: https://dart.dev/get-dart
57-
[github_actions_link]: https://docs.github.com/en/actions/learn-github-actions
5880
[license_badge]: https://img.shields.io/badge/license-MIT-blue.svg
5981
[license_link]: https://opensource.org/licenses/MIT
6082
[logo_black]: https://raw.githubusercontent.com/VGVentures/very_good_brand/main/styles/README/vgv_logo_black.png#gh-light-mode-only
6183
[logo_white]: https://raw.githubusercontent.com/VGVentures/very_good_brand/main/styles/README/vgv_logo_white.png#gh-dark-mode-only
62-
[mason_link]: https://github.com/felangel/mason
6384
[very_good_analysis_badge]: https://img.shields.io/badge/style-very_good_analysis-B22C89.svg
6485
[very_good_analysis_link]: https://pub.dev/packages/very_good_analysis
65-
[very_good_coverage_link]: https://github.com/marketplace/actions/very-good-coverage
6686
[very_good_ventures_link]: https://verygood.ventures
6787
[very_good_ventures_link_light]: https://verygood.ventures#gh-light-mode-only
6888
[very_good_ventures_link_dark]: https://verygood.ventures#gh-dark-mode-only
6989
[very_good_workflows_link]: https://github.com/VeryGoodOpenSource/very_good_workflows
90+
[docs_link]: https://github.com/VeryGoodOpenSource/cli_completion/tree/main/doc
91+
[pub_link]: https://cli_completion.pckg.pub
92+
[pub_badge]: https://img.shields.io/pub/v/cli_completion.svg

doc/README.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# CLI Completion
2+
3+
`cli_completion` is a Dart package that aims to enable Dart CLI applications to receive shell completions with minimal setup. You can also customize completion suggestions programmatically.
4+
5+
It is inspired by Kevin Moore's package [completion](https://pub.dev/packages/completion) and the excellent [tab tab package](https://github.com/mklabs/tabtab) for JS CLIs.
6+
7+
It works on bash and zsh on Linux, macOS, and Windows.
8+
9+
## How It Works
10+
11+
There are several ways to achieve shell completions for CLI commands. Most of them depend on which shell you are using and, in some cases, the terminal.
12+
13+
The approach used by `cli_completion` is to create shell script files in a directory under the user's home ( `~/.dart-cli-completion` for Linux/macOS users) and then source these scripts in the shell initialization files (`~/.zshrc`, for example).
14+
15+
We call this process [installation](#the-installation-process).
16+
17+
Then, scripts are installed and sourced (via `source ~/.zshrc`).
18+
19+
These scripts instruct the shell to call the “completion” subcommand in the CLI when the user presses <TAB>.
20+
21+
Then, the messages generated by that command will be sent back to the shell, which will take care of formatting it and displaying it to the user as completion suggestions.
22+
23+
We call this process [parsing](#how-parsing-completion-works) .
24+
25+
### The Installation Process
26+
27+
The class `CompletionCommandRunner` tries to create these files upon any command run. It does nothing if the completion files exist and displays a short error message if there is an error in the process.
28+
29+
To disable this behavior, set `enableAutoInstall` to false on your `CompletionCommandRunner` subclass.
30+
31+
### How Parsing Completion Works
32+
33+
`CompletionCommandRunner` is responsible for handling the `completion` sub-command and reading some particular environment variables that contain the state of the user input upon tab press.
34+
35+
Once it parses the user input, it reads the `ArgParser` grammar to create valid suggestions.
36+
37+
### Use Cases
38+
39+
You can find below some completion use cases that is handled by this package:
40+
41+
Completes with options and subcommands:
42+
```bash
43+
$ example_cli |
44+
--rootFlag -- A flag: in the root command
45+
some_command -- This is help for some_command
46+
some_other_command -- This is help for some_other_command
47+
```
48+
49+
Completes partially written commands and options:
50+
```bash
51+
$ example_cli some|
52+
some_command -- This is help for some_command
53+
```
54+
55+
Hides aliases and hidden options and commands:
56+
```bash
57+
$ example_cli some_command|
58+
--continuous -- A continuous option: any value is allowed
59+
--discrete -- A discrete option with "allowed" values (mandatory)
60+
--help -- Print this usage information.
61+
--inverseflag -- A flag that the default value is true
62+
--multi-c -- An continuous option that can be passed multiple times
63+
--multi-d -- An discrete option that can be passed multiple times
64+
--trueflag -- A flag that cannot be negated
65+
```
66+
67+
Completes with values of option with "allowed" values:
68+
```bash
69+
example_cli some_command --discrete |
70+
bar -- bar help
71+
faa -- faa help
72+
foo -- foo help
73+
```
74+
75+
For more use cases, check out the [integration tests](https://github.com/VeryGoodOpenSource/cli_completion/blob/main/example/test/integration/completion_integration_test.dart).

lib/src/command_runner/completion_command_runner.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ abstract class CompletionCommandRunner<T> extends CommandRunner<T> {
5252
bool get enableAutoInstall => true;
5353

5454
/// The [CompletionInstallation] used to install completion files.
55+
@visibleForTesting
5556
CompletionInstallation get completionInstallation {
5657
var completionInstallation = _completionInstallation;
5758

0 commit comments

Comments
 (0)