Dart completion パッケージの使い方

March 26, 2023

dart

coding

Dart の completion パッケージで CLI のコマンドオプションのオートコンプリートを実現する

まずはじめにオートコンプリート用に必要となるシェルスクリプトを生成するコマンドをインストールする。

$ dart pub global activate completion

上記を実行することで以下のコマンドが使用可能となる。使用できない場合はターミナルを再起動する。

$ shell_completion_generator {EXECUTE_FILE}

CLI 用のソースコードには以下のようにインポートする。ポイントは completion.tryArgsCompletion の部分のみ。

import 'package:args/args.dart';
import 'package:completion/completion.dart' as completion;

void main(List<String> arguments) {
  final parser = ArgParser();
  parser
    ..addOption('name', abbr: 'n')
    ..addOption('verbose', abbr: 'v')
    ..addFlag('option', help: 'flag help');

  ArgResults argResults = completion.tryArgsCompletion(arguments, parser);
  print(argResults.rest);
  print(argResults.options);

  print(parser.usage);
}

ソースコードをコンパイルし、コンパイルした実行ファイルを対象にオートコンプリート用のシェルスクリプトを作成する。

$ dart compile exe bin/dartcli.dart -o dartcli
$ shell_completion_generator dartcli > dartcli-completion.sh

あとは dartcli-completion.sh~/.bashrc or ~/.zshrc~/.bash_profile に組み込んで完了。以下のように組み込むことができる。 環境変数 PATH には実行ファイルがあるパスを指定する。すでにパスが通っている場所に配置している場合は不要。export PATH 以外の部分は shell_completion_generator で生成されたファイルをそのまま貼り付けているだけ。

...
export PATH="$PATH:/Users/XXX/Development/dartcli"
#
# Installation:
#
# Via shell config file  ~/.bashrc  (or ~/.zshrc)
#
#   Append the contents to config file
#   'source' the file in the config file
#
# You may also have a directory on your system that is configured
#    for completion files, such as:
#
#    /usr/local/etc/bash_completion.d/

###-begin-dartcli-completion-###

if type complete &>/dev/null; then
  __dartcli_completion() {
    local si="$IFS"
    IFS=$'\n' COMPREPLY=($(COMP_CWORD="$COMP_CWORD" \
                           COMP_LINE="$COMP_LINE" \
                           COMP_POINT="$COMP_POINT" \
                           dartcli completion -- "${COMP_WORDS[@]}" \
                           2>/dev/null)) || return $?
    IFS="$si"
  }
  complete -F __dartcli_completion dartcli
elif type compdef &>/dev/null; then
  __dartcli_completion() {
    si=$IFS
    compadd -- $(COMP_CWORD=$((CURRENT-1)) \
                 COMP_LINE=$BUFFER \
                 COMP_POINT=0 \
                 dartcli completion -- "${words[@]}" \
                 2>/dev/null)
    IFS=$si
  }
  compdef __dartcli_completion dartcli
elif type compctl &>/dev/null; then
  __dartcli_completion() {
    local cword line point words si
    read -Ac words
    read -cn cword
    let cword-=1
    read -l line
    read -ln point
    si="$IFS"
    IFS=$'\n' reply=($(COMP_CWORD="$cword" \
                       COMP_LINE="$line" \
                       COMP_POINT="$point" \
                       dartcli completion -- "${words[@]}" \
                       2>/dev/null)) || return $?
    IFS="$si"
  }
  compctl -K __dartcli_completion dartcli
fi

###-end-dartcli-completion-###

## Generated 2023-03-26 06:26:16.597679Z
## By /Users/XXX/.pub-cache/global_packages/completion/bin/shell_completion_generator.dart-2.19.2.snapshot

dartcli -- と入力し、タブを押すと以下のようにオートコンプリートが実行される。

$ dartcli --
--name       --no-option  --option     --verbose

以上。