zoukankan      html  css  js  c++  java
  • 重新整理 .net core 实践篇————配置系统——军令(命令行)[六]

    前言

    前文已经基本写了一下配置文件系统的一些基本原理。本文介绍一下命令行导入配置系统。

    正文

    要使用的话,引入Microsoft.extensions.Configuration.commandLine 包。

    代码:

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

    写入测试参数:

    结果:

    另一个问题,就是命令行支持几种命令格式。

    1. 无前缀模式 key=value 模式

    2. 双中横线模式 --key = value 或 --key vlaue

    3. 正斜杠模式 /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();
    

    结果:

    下一节配置系统之变色龙(环境配置)

    上述为个人整理,如有错误望请指出,谢谢。

  • 相关阅读:
    Openssl命令详解
    Openssl命令详解
    Mac根目录下无法创建文件或目录
    解决 mysql from_base64 函数返回乱码的问题
    elementUI日期选择器 el-date-picker根据所选日期选择禁用
    el-dialog设置为点击弹窗以外的区域不自动关闭弹窗
    在vue项目中MD5加密的使用方法
    bower install 报错fatal: unable to access 'https://github.com/angular/bower-angular-touch.git/'类错误解决方法
    angular项目grunt serve报错Cannot find where you keep your Bower packages
    移动端开发--》适配各种机型样式大小
  • 原文地址:https://www.cnblogs.com/aoximin/p/14826887.html
Copyright © 2011-2022 走看看