Aloe.Utils.CommandLine
1.0.0
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package Aloe.Utils.CommandLine --version 1.0.0
NuGet\Install-Package Aloe.Utils.CommandLine -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Aloe.Utils.CommandLine" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Aloe.Utils.CommandLine" Version="1.0.0" />
<PackageReference Include="Aloe.Utils.CommandLine" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Aloe.Utils.CommandLine --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Aloe.Utils.CommandLine, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=Aloe.Utils.CommandLine&version=1.0.0
#tool nuget:?package=Aloe.Utils.CommandLine&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Aloe.Utils.CommandLine
Aloe.Utils.CommandLine
は、コマンドライン引数を柔軟に前処理する軽量ユーティリティです。
--flag
のようなブールオプションの補完や、-uadmin
のような短縮オプションと値の連結にも対応しています。
.NET の IConfigurationBuilder.AddCommandLine(...)
と組み合わせて使用することで、アプリケーションの設定構成が簡潔かつ堅牢になります。
主な機能
- --flag のような単独オプションを true に補完
- -uadmin → -u admin に分割
- プラグイン不要・ゼロ依存の軽量ユーティリティ
- DI / IConfiguration / appsettings.json などとの統合に最適
対応環境
- .NET 6 以降(.NET Core 3.1 でも動作可)
- Microsoft.Extensions.Configuration と併用可能
Install
NuGet パッケージマネージャーからインストールします:
Install-Package Aloe.Utils.CommandLine
あるいは、.NET CLI で:
dotnet add package Aloe.Utils.CommandLine
Usage
1. 基本的な使用方法
using Aloe.Utils.CommandLine;
var processedArgs = ArgsHelper.PreprocessArgs(
args,
flagArgs: new[] { "--debug", "--standalone" },
shortArgs: new[] { "-u", "-p" }
);
入力例:
myapp.exe -uadmin --debug
出力結果:
new[] { "-u", "admin", "--debug", "true" }
2. 実践的な使用例
以下は、実際のアプリケーションでの使用例です。コマンドライン引数、設定ファイル、シークレット、環境変数を統合的に管理する方法を示しています:
public static class AppConfig
{
// フラグ用のコマンドライン引数
public static readonly List<string> FlagArgs = new()
{
"--standalone",
"--debug",
"--verbose",
};
// 短い名前のコマンドライン引数
public static readonly List<string> ShortArgs = new()
{
"-u",
"-p",
"-c",
};
// コマンドライン引数と設定プロパティのマッピング
public static readonly Dictionary<string, string> Aliases = new()
{
{ "--standalone", "AppSettings:IsStandalone" },
{ "--debug", "AppSettings:IsDebug" },
{ "-u", "AppSettings:Username" },
{ "-p", "AppSettings:Password" },
};
// 設定ファイルの一覧
public static readonly List<string> ConfigFiles = new()
{
"appsettings.json",
"appsettings.Development.json",
};
public static IConfigurationRoot CreateConfigurationRoot(string[] args)
{
// コマンドライン引数の前処理
var processedArgs = ArgsHelper.PreprocessArgs(
args,
FlagArgs,
ShortArgs);
// 設定の構築
var builder = new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory)
// 設定ファイル(優先順位: 低)
.AddJsonFile("appsettings.json", optional: true)
.AddJsonFile("appsettings.Development.json", optional: true)
// 開発環境用のシークレット(優先順位: 中)
.AddUserSecrets<Program>(optional: true)
// 環境変数(優先順位: 高)
.AddEnvironmentVariables()
// コマンドライン引数(優先順位: 最高)
.AddCommandLine(processedArgs, Aliases);
return builder.Build();
}
}
この例では以下の機能を実装しています:
- フラグオプションと短縮オプションの定義
- コマンドライン引数と設定プロパティのマッピング
- 複数の設定ソースの統合(JSON、シークレット、環境変数、コマンドライン)
- 開発環境と本番環境の設定の分離
3. 設定の読み取り
// 設定の読み取り例
var config = AppConfig.CreateConfigurationRoot(args);
// 設定値の取得
var isStandalone = config.GetValue<bool>("AppSettings:IsStandalone");
var username = config.GetValue<string>("AppSettings:Username");
ライセンス
MIT License
貢献
バグ報告や機能要望は、GitHub Issues でお願いします。プルリクエストも歓迎します。
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net9.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.