- Ⅰ. はじめに
- Ⅱ. ConsoleAppFrameworkを利用する方法
- Ⅲ. CommandLineParserを利用する方法
- Ⅳ. System.CommandLine.DragonFruit を使う方法
- 関連プロジェクト
- 参考
Ⅱ. ConsoleAppFrameworkを利用する方法
1. NuGetからConsoleAppFrameworkをインストールする
Install-Package ConsoleAppFramework
2. サンプルプログラムを書く
Program.cs
using System; using ConsoleAppFramework; using Microsoft.Extensions.Hosting; using System.Threading.Tasks; namespace Test { class Program : ConsoleAppBase { static async Task Main(string[] args) { await Host.CreateDefaultBuilder().RunConsoleAppFrameworkAsync<Program>(args); } public void Run([Option("n", "User Name")] string name) { Console.WriteLine($"Hello {name}"); } } }
3. 実行結果
引数ありの場合
$ Test.exe -n tanaka Hello tanaka
引数なしの場合
$ Test.exe Usage: Test [options...] Options: -n, -name <String> User Name (Required) Commands: help Display help. version Display version.
Ⅲ. CommandLineParserを利用する方法
1. NuGetから CommandLineParser をインストールする
Install-Package CommandLineParser
2. サンプルプログラムを書く
Option.cs
public class Option { [Option('x', "valuex", Required = true, HelpText = "Xの値", Default = 0)] public int X { get; set; } [Option("valuey", Required = true, HelpText = "Yの値", Default = 0)] public int Y { get; set; } /* [Value(0, Required = true, MetaName = "valuex", HelpText = "Xの値")] public int X { get; set; } [Value(1, Required = true, MetaName = "valuey", HelpText = "Yの値")] public int Y { get; set; } */ }
Program.cs
class Program { static void Main(string[] args) { var result = Parser.Default.ParseArguments<Option>(args); if (result.Tag == ParserResultType.Parsed) { var parsed = (Parsed<Option>)result; Console.WriteLine(parsed.Value.X); Console.WriteLine(parsed.Value.Y); } } }
3. 実行結果
引数ありの場合
$ CommandLineParserTest.exe -x 1 --valuey 2 1 2
引数なしの場合
$ CommandLineParserTest.exe CommandLineParserTest 1.0.0 Copyright (C) 2018 CommandLineParserTest ERROR(S): Required option 'x, valuex' is missing. Required option 'valuey' is missing. -x, --valuex Required. (Default: 0) Xの値 -y, --valuey Required. (Default: 0) Yの値 --help Display this help screen. --version Display version information.
Ⅳ. System.CommandLine.DragonFruit を使う方法
1. パッケージソースを追加する
https://dotnet.myget.org/F/system-commandline/api/v3/index.json
2. System.CommandLine.DragonFruit をインストールする
Install-Package System.CommandLine.DragonFruit
3. サンプルプログラムを書く
Program.cs
static void Main(int intOption = 42, bool boolOption = false, FileInfo fileOption = null) { Console.WriteLine($"The value for --int-option is: {intOption}"); Console.WriteLine($"The value for --bool-option is: {boolOption}"); Console.WriteLine($"The value for --file-option is: {fileOption?.FullName ?? "null"}"); }
4. 実行結果
引数なしの場合
$ Test.exe The value for --int-option is: 42 The value for --bool-option is: False The value for --file-option is: null
引数ありの場合
$ Test.exe --int-option 1 --file-option c:\a.txt The value for --int-option is: 1 The value for --bool-option is: False The value for --file-option is: c:\a.txt
--helpをつけた場合
$ Test.dll --help Usage: Test [options] Options: --int-option intOption --bool-option boolOption --file-option fileOption --version Display version information