09 | Command Line Configuration Provider: The easiest and quickest to configure injection method
This section explains how to use the command-line parameters to configure the data source as
Command line configuration (provider) supports three formats of command
1, no prefix mode key = value
2, dual-mode --key = value hyphens or --key value
3, n-bar pattern / key = value or / key value
NOTE: The equal sign separators and separator spaces can not be mixed
Replace Mode command: command parameters aliasing
1, must begin with a single dash (- -) or a double bar ()
2, Key mapping dictionary can not contain duplicate
Source link:
https://github.com/witskeeper/geektime/tree/master/samples/ConfigurationCommandLineDemo
First introduced three packages
- Microsoft.Extensions.Configuration.Abstractions
- Microsoft.Extensions.Configuration
- Microsoft.Extensions.Configuration.CommandLine
The main program
namespace ConfigurationCommandLineDemo
{
class Program
{
static void Main(string[] args)
{
var builder = new ConfigurationBuilder();
// 把入参传递给命令行参提供程序
builder.AddCommandLine(args);
var configurationRoot = builder.Build();
Console.WriteLine($"CommandLineKey1:{configurationRoot["CommandLineKey1"]}");
Console.WriteLine($"CommandLineKey2:{configurationRoot["CommandLineKey2"]}");
Console.ReadKey();
}
}
}
Right-property project, command set debug mode startup parameters
CommandLineKey1=value1 --CommandLineKey2=value2 /CommandLineKey3=value3 --k1=k3
You can also edit files, launchSettings.json
{
"profiles": {
"ConfigurationCommandLineDemo": {
"commandName": "Project",
"commandLineArgs": "CommandLineKey1=value1 --CommandLineKey2=value2 /CommandLineKey3=value3 --k1=k3"
}
}
}
Start the program, the output is as follows:
CommandLineKey1:value1
CommandLineKey2:value2
Followed by command substitution
namespace ConfigurationCommandLineDemo
{
class Program
{
static void Main(string[] args)
{
var builder = new ConfigurationBuilder();
//// 把入参传递给命令行参提供程序
//builder.AddCommandLine(args);
#region 命令替换
var mapper = new Dictionary<string, string> { { "-k1", "CommandLineKey1" } };
builder.AddCommandLine(args, mapper);
#endregion
var configurationRoot = builder.Build();
Console.WriteLine($"CommandLineKey1:{configurationRoot["CommandLineKey1"]}");
Console.WriteLine($"CommandLineKey2:{configurationRoot["CommandLineKey2"]}");
Console.ReadKey();
}
}
}
The double bar --k1 = k3 to single bar -k1 = k3
{
"profiles": {
"ConfigurationCommandLineDemo": {
"commandName": "Project",
"commandLineArgs": "CommandLineKey1=value1 --CommandLineKey2=value2 /CommandLineKey3=value3 -k1=k3"
}
}
}
Start the program, the output is as follows:
CommandLineKey1:k3
CommandLineKey2:value2
As can be seen, -k1 replacement value inside CommandLineKey1
This scene is what to do?
.NET can actually look at their command-line tool
Open the console, enter dotnet --help
sdk-options:
-d|--diagnostics 启用诊断输出。
-h|--help 显示命令行帮助。
--info 显示 .NET Core 信息。
--list-runtimes 显示安装的运行时。
--list-sdks 显示安装的 SDK。
--version 显示使用中的 .NET Core SDK 版本。
Here you can see options to support dual bars long name and short name of the single bars
In fact most typical scenario is to use the command-line parameters provide a way of naming quick short name, for example, you can replace -h --help
This work is Creative Commons Attribution - NonCommercial - ShareAlike 4.0 International License Agreement for licensing.
欢迎转载、使用、重新发布,但务必保留文章署名 郑子铭 (包含链接: http://www.cnblogs.com/MingsonZheng/ ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。
如有任何疑问,请与我联系 ([email protected]) 。