
.net core 命令行配置提供程序
核心组件包
- Microsoft.Extensions.Configuration
- Microsoft.Extensions.Configuration.Abstractions
- Microsoft.Extensions.Configuration.CommandLine
支持的命令格式
- 无前缀的 key=value 模式
- 双中横线模式 —key=value 或 -key value
- 正斜杠模式 /key=value 或 /key value
- 无前缀的 key=value 模式
备注:等号分隔符和空格分隔符不能混用
命令替换模式
- 必须以单划线 (-) 或双划线 (—) 开头
- 映射字典不能包含重复key
简单运用
项目结构
配置环境
参数如下CommandLineKey1=value1 --CommandLine=value2 /CommandLineKey3=value3 -k1=k3
当我们按 Ctrl+S 将会自动生成 launchSettings.json 文件
内容如下
{
"profiles": {
"ConfigurationDemo": {
"commandName": "Project",
"commandLineArgs": "CommandLineKey1=value1 --CommandLine=value2 /CommandLineKey3=value3 -k1=k3"
}
}
}
代码示范
var builder = new ConfigurationBuilder();
#region 命令替换
var mapper = new Dictionary<string, string> {
{ "-k1", "CommandLineKey1" }
};
#endregion
//当没有单个命令需要替换的时候可直接这样写
//builder.AddCommandLine(args);
builder.AddCommandLine(args, mapper);
var configurationRoot = builder.Build();
Console.WriteLine($"CommandLineKey1:{ configurationRoot["CommandLineKey1"]}");
Console.WriteLine($"CommandLine:{ configurationRoot["CommandLine"]}");
Console.WriteLine($"CommandLineKey3:{ configurationRoot["CommandLineKey3"]}");
Console.ReadKey();
结果
运用场景
可以用来写一些 dotnet 的命令行工具
欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739
评价
排名
2
文章
634
粉丝
44
评论
93
docker中Sware集群与service
尘叶心繁 : 想学呀!我教你呀
一个bug让程序员走上法庭 索赔金额达400亿日元
叼着奶瓶逛酒吧 : 所以说做程序员也要懂点法律知识
.net core 塑形资源
剑轩 : 收藏收藏
映射AutoMapper
剑轩 :
好是好,这个对效率影响大不大哇,效率高不高
一个bug让程序员走上法庭 索赔金额达400亿日元
剑轩 : 有点可怕
ASP.NET Core 服务注册生命周期
剑轩 :
http://www.tnblog.net/aojiancc2/article/details/167
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:
50010702506256


欢迎加群交流技术