重新整理 .net core 实践篇————配置系统——军令(命令行)[六]
前言
前文已经基本写了一下配置文件系统的一些基本原理。本文介绍一下命令行导入配置系统。
正文
要使用的话,引入Microsoft.extensions.Configuration.commandLine 包。
代码:
IConfigurationBuilder builder = new ConfigurationBuilder();
builder.AddCommandLine(args);
var configurationRoot = builder.Build();
Console.WriteLine($"CommandLineKey1:{configurationRoot["CommandLineKey1"]}");
Console.ReadLine();
写入测试参数:

结果:

另一个问题,就是命令行支持几种命令格式。
无前缀模式 key=value 模式
双中横线模式 --key = value 或 --key vlaue
正斜杠模式 /key=value 或/key value
注:如果--key = value 这种等号模式,那么就不能使用--key vlaue 这种中间空格二点模式。
配置:

IConfigurationBuilder builder = new ConfigurationBuilder();
builder.AddCommandLine(args);
var configurationRoot = builder.Build();
Console.WriteLine($"CommandLineKey1:{configurationRoot["CommandLineKey1"]}");
Console.WriteLine($"CommandLineKey2:{configurationRoot["CommandLineKey2"]}");
Console.WriteLine($"CommandLineKey3:{configurationRoot["CommandLineKey3"]}");
Console.ReadLine();
结果:

这里其实项目属性在lauchSettings.json 中,后面我就不截图,直接放这个启动配置。

命令替换模式:
{
"profiles": {
"ConfigureDemo": {
"commandName": "Project",
"commandLineArgs": "CommandLineKey1=value1 /CommandLineKey2=value2 --CommandLineKey3=value3 -k1=value4"
}
}
}
代码:
IConfigurationBuilder builder = new ConfigurationBuilder();
var mappers = new Dictionary<string, string>
{
{"-k1","CommandLineKey1" }
};
builder.AddCommandLine(args, mappers);
var configurationRoot = builder.Build();
Console.WriteLine($"CommandLineKey1:{configurationRoot["CommandLineKey1"]}");
Console.WriteLine($"CommandLineKey2:{configurationRoot["CommandLineKey2"]}");
Console.WriteLine($"CommandLineKey3:{configurationRoot["CommandLineKey3"]}");
Console.ReadLine();
这里面就是以-k1的值替换了CommandLineKey1的值。
上述的-k1也不是随便命名的,要用-开头才可以替换。
那么这种有什么用呢?
这种可以缩短命名。
{
"profiles": {
"ConfigureDemo": {
"commandName": "Project",
"commandLineArgs": "-k1=value4"
}
}
}
代码:
IConfigurationBuilder builder = new ConfigurationBuilder();
var mappers = new Dictionary<string, string>
{
{"-k1","CommandLineKey1" }
};
builder.AddCommandLine(args, mappers);
var configurationRoot = builder.Build();
Console.WriteLine($"CommandLineKey1:{configurationRoot["CommandLineKey1"]}");
Console.ReadLine();
结果:

结
下一节配置系统之变色龙(环境配置)
上述为个人整理,如有错误望请指出,谢谢。
重新整理 .net core 实践篇————配置系统——军令(命令行)[六]的更多相关文章
- 重新整理 .net core 实践篇————配置系统之盟约[五]
前言 在asp .net core 中我们会看到一个appsettings.json 文件,它就是我们在服务中的各种配置,是至关重要的一部门. 不管是官方自带的服务,还是我们自己编写的服务都是用它来实 ...
- 重新整理 .net core 实践篇—————配置系统之军令状[七](配置文件)
前言 介绍一下配置系统中的配置文件,很多服务的配置都写在配置文件中,也是配置系统的大头. 正文 在asp .net core 提供了下面几种配置文件格式的读取方式. Microsoft.extensi ...
- 重新整理 .net core 实践篇—————配置系统之间谍[八](文件监控)
前言 前文提及到了当我们的配置文件修改了,那么从 configurationRoot 在此读取会读取到新的数据,本文进行扩展,并从源码方面简单介绍一下,下面内容和前面几节息息相关. 正文 先看一下,如 ...
- 重新整理 .net core 实践篇—————配置系统之强类型配置[十]
前言 前文中我们去获取value值的时候,都是通过configurationRoot 来获取的,如configurationRoot["key"],这种形式. 这种形式有一个不好的 ...
- 重新整理 .net core 实践篇—————配置系统之简单配置中心[十一]
前言 市面上已经有很多配置中心集成工具了,故此不会去实践某个框架. 下面链接是apollo 官网的教程,实在太详细了,本文介绍一下扩展数据源,和简单翻翻阅一下apollo 关键部分. apollo 服 ...
- 重新整理 .net core 实践篇————配置应用[一]
前言 本来想整理到<<重新整理.net core 计1400篇>>里面去,但是后来一想,整理 .net core 实践篇 是偏于实践,故而分开. 因为是重新整理,那么就从配置开 ...
- 重新整理 .net core 实践篇—————日志系统之战地记者[十五]
前言 本节开始整理日志相关的东西.先整理一下日志的基本原理. 正文 首先介绍一下包: Microsoft.Extengsion.Logging.Abstrations 这个是接口包. Microsof ...
- 重新整理 .net core 实践篇—————日志系统之作用域[十七]
前言 前面介绍了服务与日志之间的配置,那么我们服务会遇到下面的场景会被遇到一些打log的问题. 前面我提及到我们的log,其实是在一个队列里面,而我们的请求是在并发的,多个用户同时发送请求这个时候我们 ...
- 重新整理 .net core 实践篇—————日志系统之结构化[十八]
前言 什么是结构化呢? 结构化,就是将原本没有规律的东西进行有规律话. 就比如我们学习数据结构,需要学习排序然后又要学习查询,说白了这就是一套,没有排序,谈如何查询是没有意义的,因为查询算法就是根据某 ...
随机推荐
- Day05_24_继承
继承 什么是继承? 继承本质上是对某一批类的抽象,从而实现对现实世界更好的建模.继承是类和类之间的一种关系,除此之外类和类之间的关系还有依赖.组合.聚合等. 继承就是子类(派生类)继承父类(基类)的特 ...
- mybatis的本质和原理
背景 项目需要,我们需要自己做一套mybatis,或者使用大部分mybatis的原始内容.对其改造,以适应需要.这就要求我再次学习一下mybatis,对它有更深入的了解. 是什么 MyBatis ...
- k8s helm 安装etcd
待续 helm install etcd bitnami/etcd \ --set statefulset.replicaCount=3 \ --set persistence.enabled=tru ...
- B - Rikka with Graph HDU - 5631 (并查集+思维)
As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some mat ...
- 用最容易的方式学会单链表(Python实现)
单链表与数组 在本博客中,我们介绍单链表这种数据结构,链表结构为基于数组的序列提供了另一种选择(例如Python列表). 基于数组的序列也会有如下缺点: 一个动态数组的长度可能超过实际存储数组元素所需 ...
- 【vue-09】axios
[vue-09]axios 文档:Axios中文文档 官网 为什么要使用axios 功能特点: 支持发送ajax异步 支持在NodeJs中发送ajax请求. 支持Promise 支持拦截器请求和响应 ...
- 解决GET http://localhost:8080/js/layui/layui.js net::ERR_ABORTED 404
用ssm+layui在写页面的时候,发现无法找到资源路径 <script src="js/layui/layui.js" charset="utf-8"& ...
- 【opencv】获取摄像头rstp视频流地址方法
1.rstp通用地址格式为 : 通用格式 // user : 登录摄像头的用户名 // password:登录摄像头的密码 // ip:摄像头的ip地址 // port:端口号,常用的为554 &qu ...
- SecureCRT 基本设置
1:字体与大小 Lucida Console 四号 2:声音关闭 Terminal-->Audio bell不勾选 默认网络工程师常用: Terminal-->Emulation--& ...
- 三、多线程之Thread与Runnable的区别
Thread与Runnable的区别(用三个窗口同时出售10张车票为例子) 运行代码 运行结果 分析 System.out.println("开始测试多线程");class MyT ...