CommandLineConfigurationExtensions.AddCommandLine 方法

定义

重载

AddCommandLine(IConfigurationBuilder, Action<CommandLineConfigurationSource>)

添加从命令行读取配置值的 IConfigurationProvider

AddCommandLine(IConfigurationBuilder, String[])

添加从命令行读取配置值的 CommandLineConfigurationProvider

AddCommandLine(IConfigurationBuilder, String[], IDictionary<String,String>)

使用指定的交换映射添加从命令行读取配置值的 CommandLineConfigurationProvider

AddCommandLine(IConfigurationBuilder, Action<CommandLineConfigurationSource>)

Source:
CommandLineConfigurationExtensions.cs
Source:
CommandLineConfigurationExtensions.cs
Source:
CommandLineConfigurationExtensions.cs
Source:
CommandLineConfigurationExtensions.cs

添加从命令行读取配置值的 IConfigurationProvider

public:
[System::Runtime::CompilerServices::Extension]
 static Microsoft::Extensions::Configuration::IConfigurationBuilder ^ AddCommandLine(Microsoft::Extensions::Configuration::IConfigurationBuilder ^ builder, Action<Microsoft::Extensions::Configuration::CommandLine::CommandLineConfigurationSource ^> ^ configureSource);
public static Microsoft.Extensions.Configuration.IConfigurationBuilder AddCommandLine (this Microsoft.Extensions.Configuration.IConfigurationBuilder builder, Action<Microsoft.Extensions.Configuration.CommandLine.CommandLineConfigurationSource>? configureSource);
static member AddCommandLine : Microsoft.Extensions.Configuration.IConfigurationBuilder * Action<Microsoft.Extensions.Configuration.CommandLine.CommandLineConfigurationSource> -> Microsoft.Extensions.Configuration.IConfigurationBuilder
<Extension()>
Public Function AddCommandLine (builder As IConfigurationBuilder, configureSource As Action(Of CommandLineConfigurationSource)) As IConfigurationBuilder

参数

builder
IConfigurationBuilder

要添加到的 IConfigurationBuilder

configureSource
Action<CommandLineConfigurationSource>

配置源。

返回

IConfigurationBuilder

适用于

AddCommandLine(IConfigurationBuilder, String[])

Source:
CommandLineConfigurationExtensions.cs
Source:
CommandLineConfigurationExtensions.cs
Source:
CommandLineConfigurationExtensions.cs
Source:
CommandLineConfigurationExtensions.cs

添加从命令行读取配置值的 CommandLineConfigurationProvider

public:
[System::Runtime::CompilerServices::Extension]
 static Microsoft::Extensions::Configuration::IConfigurationBuilder ^ AddCommandLine(Microsoft::Extensions::Configuration::IConfigurationBuilder ^ configurationBuilder, cli::array <System::String ^> ^ args);
public static Microsoft.Extensions.Configuration.IConfigurationBuilder AddCommandLine (this Microsoft.Extensions.Configuration.IConfigurationBuilder configurationBuilder, string[] args);
static member AddCommandLine : Microsoft.Extensions.Configuration.IConfigurationBuilder * string[] -> Microsoft.Extensions.Configuration.IConfigurationBuilder
<Extension()>
Public Function AddCommandLine (configurationBuilder As IConfigurationBuilder, args As String()) As IConfigurationBuilder

参数

configurationBuilder
IConfigurationBuilder

要添加到的 IConfigurationBuilder

args
String[]

命令行参数。

返回

IConfigurationBuilder

示例

具有五个值的简单控制台应用程序。

// dotnet run key1=value1 --key2=value2 /key3=value3 --key4 value4 /key5 value5

using Microsoft.Extensions.Configuration;
using System;

namespace CommandLineSample
{
   public class Program
   {
       public static void Main(string[] args)
       {
           var builder = new ConfigurationBuilder();
           builder.AddCommandLine(args);

           var config = builder.Build();

           Console.WriteLine($"Key1: '{config["Key1"]}'");
           Console.WriteLine($"Key2: '{config["Key2"]}'");
           Console.WriteLine($"Key3: '{config["Key3"]}'");
           Console.WriteLine($"Key4: '{config["Key4"]}'");
           Console.WriteLine($"Key5: '{config["Key5"]}'");
       }
   }
}

注解

在命令行(字符串数组中 args )传递的值应该是一组以两个短划线 (“--”) 为前缀的键,然后是值,用等号 (“=”) 或空格 (“”) 分隔。 正斜杠 (“/”) 可用作替代前缀,具有等于 或空格。 使用等号时,可以完全排除前缀。

参数有五种基本替代格式:

key1=value1
--key2=value2
/key3=value3
--key4 value4
/key5 value5

适用于

AddCommandLine(IConfigurationBuilder, String[], IDictionary<String,String>)

Source:
CommandLineConfigurationExtensions.cs
Source:
CommandLineConfigurationExtensions.cs
Source:
CommandLineConfigurationExtensions.cs
Source:
CommandLineConfigurationExtensions.cs

使用指定的交换映射添加从命令行读取配置值的 CommandLineConfigurationProvider

public:
[System::Runtime::CompilerServices::Extension]
 static Microsoft::Extensions::Configuration::IConfigurationBuilder ^ AddCommandLine(Microsoft::Extensions::Configuration::IConfigurationBuilder ^ configurationBuilder, cli::array <System::String ^> ^ args, System::Collections::Generic::IDictionary<System::String ^, System::String ^> ^ switchMappings);
public static Microsoft.Extensions.Configuration.IConfigurationBuilder AddCommandLine (this Microsoft.Extensions.Configuration.IConfigurationBuilder configurationBuilder, string[] args, System.Collections.Generic.IDictionary<string,string>? switchMappings);
static member AddCommandLine : Microsoft.Extensions.Configuration.IConfigurationBuilder * string[] * System.Collections.Generic.IDictionary<string, string> -> Microsoft.Extensions.Configuration.IConfigurationBuilder
<Extension()>
Public Function AddCommandLine (configurationBuilder As IConfigurationBuilder, args As String(), switchMappings As IDictionary(Of String, String)) As IConfigurationBuilder

参数

configurationBuilder
IConfigurationBuilder

要添加到的 IConfigurationBuilder

args
String[]

命令行参数。

switchMappings
IDictionary<String,String>

交换映射。 映射到配置键(无前缀)的短键(带有前缀“-”)和别名键(带有前缀“--”)字典。

返回

IConfigurationBuilder

示例

一个简单的控制台应用程序,它定义了两个短的和四个别名开关映射。

// dotnet run k1=value1 -k2 value2 --alt3=value2 /alt4=value3 --alt5 value5 /alt6 value6

using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;

namespace CommandLineSample
{
   public class Program
   {
       public static void Main(string[] args)
       {
           var switchMappings = new Dictionary<string, string>()
           {
               { "-k1", "key1" },
               { "-k2", "key2" },
               { "--alt3", "key3" },
               { "--alt4", "key4" },
               { "--alt5", "key5" },
               { "--alt6", "key6" },
           };
           var builder = new ConfigurationBuilder();
           builder.AddCommandLine(args, switchMappings);

           var config = builder.Build();

           Console.WriteLine($"Key1: '{config["Key1"]}'");
           Console.WriteLine($"Key2: '{config["Key2"]}'");
           Console.WriteLine($"Key3: '{config["Key3"]}'");
           Console.WriteLine($"Key4: '{config["Key4"]}'");
           Console.WriteLine($"Key5: '{config["Key5"]}'");
           Console.WriteLine($"Key6: '{config["Key6"]}'");
       }
   }
}

注解

switchMappings允许从命令行使用备用短键和别名键的其他格式。 有关支持的标准格式,请参阅 的基本版本 AddCommandLine

短键以单个短划线 (“-”) 开头,映射到没有前缀) (main键名称,并且可与等于 或空格一起使用。 单破折号映射旨在用于较短的备用开关。 不能直接访问单个短划线开关,但必须具有使用完整键定义和访问的开关映射。 传递未定义的单短划线参数将导致 FormatException

短参数有两种格式: -k1=value1-k2 value2

别名键定义以两个短划线开头 (“--”) ,映射到main键名称 (无前缀) ,可用于代替普通键。 在命令行中使用正斜杠前缀 (但无前缀等于格式) 时,它们也起作用。

别名参数只有四种格式:

--alt3=value3
/alt4=value4
--alt5 value5
/alt6 value6

适用于