Von Bedeutung
System.CommandLine
は現在プレビュー段階であり、このドキュメントはバージョン 2.0 ベータ 5 用です。
一部の情報は、リリース前に大幅に変更される可能性があるプレリリース製品に関連しています。 Microsoft は、ここで提供される情報に関して明示的または黙示的な保証を行いません。
System.CommandLine.CommandLineConfiguration
は、パーサーを構成するためのプロパティを提供するクラスです。
System.CommandLine.Command.Parse
やSystem.CommandLine.Parsing.CommandLineParser.Parse
など、すべてのParse
メソッドの省略可能な引数です。 指定されていない場合は、既定の構成が使用されます。
すべての System.CommandLine.ParseResult
インスタンスには、解析に使用される構成を返す System.CommandLine.ParseResult.Configuration
プロパティがあります。
標準出力とエラー
System.CommandLine.CommandLineConfiguration
は、 System.Console
を使用するよりも簡単に、テストだけでなく、多くの拡張性のシナリオも実現します。
Output
とError
の 2 つのTextWriter
プロパティが公開されます。 これらは、テスト用の出力をキャプチャするために使用できるTextWriter
など、任意のStringWriter
インスタンスに設定できます。
標準出力に書き込む単純なコマンドを定義しましょう。
Option<FileInfo?> fileOption = new("--file")
{
Description = "An option whose argument is parsed as a FileInfo"
};
RootCommand rootCommand = new("Configuration sample")
{
fileOption
};
rootCommand.SetAction((parseResult) =>
{
FileInfo? fileOptionValue = parseResult.GetValue(fileOption);
parseResult.Configuration.Output.WriteLine($"File option value: {fileOptionValue?.FullName}");
});
次に、 CommandLineConfiguration
を使用して出力をキャプチャしてみましょう。
StringWriter output = new();
CommandLineConfiguration configuration = new(rootCommand)
{
Output = output,
Error = TextWriter.Null
};
configuration.Parse("-h").Invoke();
Debug.Assert(output.ToString().Contains("Configuration sample"));
EnablePosixBundling
1 文字のオプションのバンドルは既定で有効になっていますが、System.CommandLine.CommandLineConfiguration.EnablePosixBundling
プロパティを false
に設定することで無効にできます。
ProcessTerminationTimeout
プロセス終了タイムアウト は、 System.CommandLine.CommandLineConfiguration.ProcessTerminationTimeout
プロパティを使用して構成できます。 既定値は 2 秒です。
ResponseFileTokenReplacer
応答ファイル は既定で有効になっていますが、 System.CommandLine.CommandLineConfiguration.ResponseFileTokenReplacer
プロパティを null
に設定することで無効にできます。 応答ファイルの処理方法をカスタマイズするカスタム実装を提供することもできます。
EnableDefaultExceptionHandler
既定では、コマンドの呼び出し中にスローされたすべてのハンドルされない例外がキャッチされ、ユーザーに報告されます。 この動作は、 System.CommandLine.CommandLineConfiguration.EnableDefaultExceptionHandler
プロパティを false
に設定することで無効にすることができます。 これは、例外のログ記録や別のユーザー エクスペリエンスの提供など、カスタムの方法で例外を処理する場合に便利です。
派生クラス
System.CommandLine.CommandLineConfiguration
はシールされていないため、そこから派生してカスタム プロパティまたはメソッドを追加できます。 これは、アプリケーションに固有の追加の構成オプションを指定する場合に便利です。
こちらも参照ください
.NET