-
Notifications
You must be signed in to change notification settings - Fork 4
feat: Support embedding and installing shell completion scripts #68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d8e1fe3
feat(cli_tools): completion embed command
christerswahn 4f913d1
feat(cli_tools): completion install command
christerswahn dd5fcbc
chore(cli_tools): Export public completion target data types
christerswahn 543de55
feat(cli_tools): Make completion feature non-experimental
christerswahn e0ee38c
docs(cli_tools): Example command using completion scripts feature
christerswahn 410f80d
fix(cli_tools): Improved completion help
christerswahn 853bcf3
fix(cli_tools): Minor improvements to completion generate
christerswahn 61d08b8
test(cli_tools): Cleaned up existing completion tests
christerswahn 999f267
test(cli_tools): Tests for embed and install subcommands
christerswahn 422b409
docs: Embed & install instructions in README
christerswahn 33a284b
fix(review): Fixed for coderabbit suggestions
christerswahn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
packages/cli_tools/example/command_completion_example.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import 'dart:io' show exitCode; | ||
|
|
||
| import 'package:cli_tools/better_command_runner.dart'; | ||
| import 'package:config/config.dart'; | ||
|
|
||
| import 'completion_script_carapace.dart'; | ||
| import 'completion_script_completely.dart'; | ||
|
|
||
christerswahn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// Example of using [BetterCommandRunner] with command line completion. | ||
| /// | ||
| /// Run this example program like so: | ||
| /// ```sh | ||
| /// dart example/command_completion_example.dart completion install -t completely | ||
| /// ``` | ||
| /// or | ||
| /// ```sh | ||
| /// dart example/command_completion_example.dart completion install -t carapace | ||
| /// source <(carapace example) # for bash, for others see https://carapace.sh/setup.html | ||
| /// ``` | ||
| /// | ||
| /// In order to regenerate the completion scripts, run these commands for each | ||
| /// target. | ||
| /// | ||
| /// See also [README_completion.md]. | ||
| /// | ||
| /// Completely: | ||
| /// ```sh | ||
| /// dart example/command_completion_example.dart completion generate -t completely | completely generate - example.bash | ||
| /// dart example/command_completion_example.dart completion embed -t completely -f example.bash -d example/ | ||
| /// ``` | ||
| /// | ||
| /// Carapace: | ||
| /// ```sh | ||
| /// dart example/command_completion_example.dart completion generate -t carapace -f example.yaml | ||
| /// dart example/command_completion_example.dart completion embed -t carapace -f example.yaml -d example/ | ||
| /// ``` | ||
| Future<void> main(final List<String> args) async { | ||
| final commandRunner = BetterCommandRunner( | ||
| 'example', | ||
| 'Example CLI command', | ||
| enableCompletionCommand: true, | ||
| embeddedCompletions: [ | ||
| completionScriptCompletely, | ||
| completionScriptCarapace, | ||
| ], | ||
| ); | ||
|
|
||
| try { | ||
| await commandRunner.run(args); | ||
| } on UsageException catch (e) { | ||
| print(e); | ||
| exitCode = 1; | ||
| } | ||
| } | ||
65 changes: 65 additions & 0 deletions
65
packages/cli_tools/example/completion_script_carapace.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /// This file is auto-generated. | ||
| library; | ||
christerswahn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| import 'package:cli_tools/better_command_runner.dart' show CompletionTarget; | ||
|
|
||
| const String _completionScript = r''' | ||
| # yaml-language-server: $schema=https://carapace.sh/schemas/command.json | ||
| name: example | ||
| persistentFlags: | ||
| -q, --quiet: Suppress all cli output. Is overridden by -v, --verbose. | ||
| -v, --verbose: Prints additional information useful for development. Overrides --q, --quiet. | ||
| commands: | ||
| - name: completion | ||
| commands: | ||
| - name: generate | ||
| flags: | ||
| -t, --target=!: The target tool format | ||
| -e, --exec-name=: Override the name of the executable | ||
| -f, --file=: Write the specification to a file instead of stdout | ||
| completion: | ||
| flag: | ||
| target: ["completely", "carapace"] | ||
| file: ["$files"] | ||
| - name: embed | ||
| flags: | ||
| -t, --target=!: The target tool format | ||
| -f, --script-file=: The script file to embed | ||
| -o, --output-file=: The Dart file name to write | ||
| -d, --output-dir=: Override the directory to write the Dart source file to | ||
| completion: | ||
| flag: | ||
| target: ["completely", "carapace"] | ||
| output-dir: ["$directories"] | ||
| - name: install | ||
| flags: | ||
| -t, --target=!: The target tool format | ||
| -e, --exec-name=: Override the name of the executable | ||
| -d, --write-dir=: Override the directory to write the script to | ||
| completion: | ||
| flag: | ||
| target: ["completely", "carapace"] | ||
| write-dir: ["$directories"] | ||
| - name: install | ||
| flags: | ||
| -t, --target=!: The target tool format | ||
| -e, --exec-name=: Override the name of the executable | ||
| -d, --write-dir=: Override the directory to write the script to | ||
| completion: | ||
| flag: | ||
| target: ["completely", "carapace"] | ||
| write-dir: ["$directories"] | ||
| '''; | ||
|
|
||
| /// Embedded script for command line completion for `carapace`. | ||
| const completionScriptCarapace = ( | ||
| target: CompletionTarget.carapace, | ||
| script: _completionScript, | ||
| ); | ||
139 changes: 139 additions & 0 deletions
139
packages/cli_tools/example/completion_script_completely.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| /// This file is auto-generated. | ||
| library; | ||
|
|
||
| import 'package:cli_tools/better_command_runner.dart' show CompletionTarget; | ||
|
|
||
| const String _completionScript = r''' | ||
| # example completion -*- shell-script -*- | ||
|
|
||
| # This bash completions script was generated by | ||
| # completely (https://github.com/bashly-framework/completely) | ||
| # Modifying it manually is not recommended | ||
|
|
||
| _example_completions_filter() { | ||
| local words="$1" | ||
| local cur=${COMP_WORDS[COMP_CWORD]} | ||
| local result=() | ||
|
|
||
| # words the user already typed (excluding the command itself) | ||
| local used=() | ||
| if ((COMP_CWORD > 1)); then | ||
| used=("${COMP_WORDS[@]:1:$((COMP_CWORD - 1))}") | ||
| fi | ||
|
|
||
| if [[ "${cur:0:1}" == "-" ]]; then | ||
| # Completing an option: offer everything (including options) | ||
| echo "$words" | ||
|
|
||
| else | ||
| # Completing a non-option: offer only non-options, | ||
| # and don't re-offer ones already used earlier in the line. | ||
| for word in $words; do | ||
| [[ "${word:0:1}" == "-" ]] && continue | ||
|
|
||
| local seen=0 | ||
| for u in "${used[@]}"; do | ||
| if [[ "$u" == "$word" ]]; then | ||
| seen=1 | ||
| break | ||
| fi | ||
| done | ||
| ((!seen)) && result+=("$word") | ||
| done | ||
|
|
||
| echo "${result[*]}" | ||
| fi | ||
| } | ||
|
|
||
| _example_completions() { | ||
| local cur=${COMP_WORDS[COMP_CWORD]} | ||
| local compwords=() | ||
| if ((COMP_CWORD > 0)); then | ||
| compwords=("${COMP_WORDS[@]:1:$((COMP_CWORD - 1))}") | ||
| fi | ||
| local compline="${compwords[*]}" | ||
|
|
||
| COMPREPLY=() | ||
|
|
||
| case "$compline" in | ||
| 'completion install'*'--write-dir') | ||
| while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -A directory -- "$cur") | ||
| ;; | ||
|
|
||
| 'completion embed'*'--output-dir') | ||
| while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -A directory -- "$cur") | ||
| ;; | ||
|
|
||
| 'completion generate'*'--target') | ||
| while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_example_completions_filter "completely carapace")" -- "$cur") | ||
| ;; | ||
|
|
||
| 'completion install'*'--target') | ||
| while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_example_completions_filter "completely carapace")" -- "$cur") | ||
| ;; | ||
|
|
||
| 'completion generate'*'--file') | ||
| while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -A file -- "$cur") | ||
| ;; | ||
|
|
||
| 'completion embed'*'--target') | ||
| while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_example_completions_filter "completely carapace")" -- "$cur") | ||
| ;; | ||
|
|
||
| 'completion generate'*'-t') | ||
| while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_example_completions_filter "completely carapace")" -- "$cur") | ||
| ;; | ||
|
|
||
| 'completion generate'*'-f') | ||
| while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -A file -- "$cur") | ||
| ;; | ||
|
|
||
| 'completion install'*'-d') | ||
| while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -A directory -- "$cur") | ||
| ;; | ||
|
|
||
| 'completion install'*'-t') | ||
| while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_example_completions_filter "completely carapace")" -- "$cur") | ||
| ;; | ||
|
|
||
| 'completion embed'*'-t') | ||
| while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_example_completions_filter "completely carapace")" -- "$cur") | ||
| ;; | ||
|
|
||
| 'completion embed'*'-d') | ||
| while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -A directory -- "$cur") | ||
| ;; | ||
|
|
||
| 'completion generate'*) | ||
| while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_example_completions_filter "--quiet -q --verbose -v --target -t --exec-name -e --file -f")" -- "$cur") | ||
| ;; | ||
|
|
||
| 'completion install'*) | ||
| while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_example_completions_filter "--quiet -q --verbose -v --target -t --exec-name -e --write-dir -d")" -- "$cur") | ||
| ;; | ||
|
|
||
| 'completion embed'*) | ||
| while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_example_completions_filter "--quiet -q --verbose -v --target -t --script-file -f --output-file -o --output-dir -d")" -- "$cur") | ||
| ;; | ||
|
|
||
| 'completion'*) | ||
| while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_example_completions_filter "generate embed install --quiet -q --verbose -v")" -- "$cur") | ||
| ;; | ||
|
|
||
| *) | ||
| while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_example_completions_filter "completion --quiet -q --verbose -v")" -- "$cur") | ||
| ;; | ||
|
|
||
| esac | ||
| } && | ||
| complete -F _example_completions example | ||
|
|
||
| # ex: filetype=sh | ||
|
|
||
| '''; | ||
|
|
||
| /// Embedded script for command line completion for `completely`. | ||
| const completionScriptCompletely = ( | ||
| target: CompletionTarget.completely, | ||
| script: _completionScript, | ||
| ); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.