2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

C#のSystemCommandLineの静的補完機能について

2
Posted at

.NET CLIなどの作成に使用されているC#用のコマンドラインライブラリのSystem.CommandLineについて最近下記のようなPRがマージされました。

個人的に面白いなと感じたため内容について紹介します。

概要

System.CommandLineを使用して作成したCLIツールについて別ツールなしでTab補完を可能にさせる機能を実装中

詳細

もともとSystem.CommandLineで作成したツールに対してTab補完をユーザーに提供するためには別途dotnet-suggestというdotnet Toolを入れる必要がありました。個人的にCLIを作成使用する場合は問題ありませんでしたがほかの人に配布する場合には各環境にインストールが必要です。

そこでこの機能ではCLIツール自体に入力補完用のスクリプト生成機能を追加することが可能です。

using System.CommandLine;
using System.CommandLine.StaticCompletions;

RootCommand rootCommand = new("My application");

// ... コマンドの設定を追加

// `completions` サブコマンドをCLIに追加
CompletionsCommandDefinition completionsCommand = new();
CompletionsCommandParser.ConfigureCommand(completionsCommand);
rootCommand.Subcommands.Add(completionsCommand);

return rootCommand.Parse(args).Invoke();

コマンドの実行方法

# 入力補完用のスクリプト生成
> myapp completions script bash > ~/.local/share/bash-completion/completions/myapp

# 現在のプロファイルにそのままパイプして追加することも可能
> myapp completions script >> $PROFILE

DBなどの外部のソースから補完を行うことも可能です。

まとめ

System.CommandLineで実装が進んでいる入力コマンドの静的補完についてまとめました。まだNugetには公開されていない(2026/08/02)ためこれから登場すること期待しています。

.NET CLIを筆頭にSystem.CommandLineを使用しているプロジェクトはC#を使用していると以外と使っていることが多いためリリースが楽しみです。

この記事が皆様のコーディングライフの助けになれば幸いです。

参考

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?