CommandLineConfigurationExtensions.AddCommandLine 方法

定义

重载

AddCommandLine(IConfigurationBuilder, Action<CommandLineConfigurationSource>)

添加从命令行读取配置值的 IConfigurationProviderAdds an IConfigurationProvider that reads configuration values from the command line.

AddCommandLine(IConfigurationBuilder, String[])

添加从命令行读取配置值的 CommandLineConfigurationProviderIConfigurationProviderAdds a CommandLineConfigurationProviderIConfigurationProvider that reads configuration values from the command line.

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

使用指定的交换映射添加从命令行读取配置值的 CommandLineConfigurationProviderIConfigurationProviderAdds a CommandLineConfigurationProviderIConfigurationProvider that reads configuration values from the command line using the specified switch mappings.

AddCommandLine(IConfigurationBuilder, Action<CommandLineConfigurationSource>)

添加从命令行读取配置值的 IConfigurationProviderAdds an IConfigurationProvider that reads configuration values from the command line.

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

要添加到的 IConfigurationBuilderThe IConfigurationBuilder to add to.

configureSource
Action<CommandLineConfigurationSource>

配置源。Configures the source.

返回

IConfigurationBuilder

IConfigurationBuilderThe IConfigurationBuilder.

适用于

AddCommandLine(IConfigurationBuilder, String[])

添加从命令行读取配置值的 CommandLineConfigurationProviderIConfigurationProviderAdds a CommandLineConfigurationProviderIConfigurationProvider that reads configuration values from the command line.

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

要添加到的 IConfigurationBuilderThe IConfigurationBuilder to add to.

args
String[]

命令行参数。The command line args.

返回

IConfigurationBuilder

IConfigurationBuilderThe IConfigurationBuilder.

示例

具有五个值的简单控制台应用程序。A simple console application that has five values.

// 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"]}'");
       }
   }
}

注解

在命令行上的 参数 字符串数组中传递的值应该是以两个短划线作为前缀的一组键, ( "--" ) ,然后是值,用等号 ( "=" ) 或空格 ( "" ) 分隔。The values passed on the command line, in the args string array, should be a set of keys prefixed with two dashes ("--") and then values, separate by either the equals sign ("=") or a space (" ").

正斜杠 ( "/" ) 可用作备用前缀,大小为等于或空格,使用等号时,可以完全省略前缀。A forward slash ("/") can be used as an alternative prefix, with either equals or space, and when using an equals sign the prefix can be left out altogether.

自变量有五种基本的替代格式: key1 = value1--key2 = value2/key3 = value3--key4 value4/key5 value5There are five basic alternative formats for arguments: key1=value1 --key2=value2 /key3=value3 --key4 value4 /key5 value5.

适用于

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

使用指定的交换映射添加从命令行读取配置值的 CommandLineConfigurationProviderIConfigurationProviderAdds a CommandLineConfigurationProviderIConfigurationProvider that reads configuration values from the command line using the specified switch mappings.

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

要添加到的 IConfigurationBuilderThe IConfigurationBuilder to add to.

args
String[]

命令行参数。The command line args.

switchMappings
IDictionary<String,String>

交换映射。The switch mappings. 映射到配置键(无前缀)的短键(带有前缀“-”)和别名键(带有前缀“--”)字典。A dictionary of short (with prefix "-") and alias keys (with prefix "--"), mapped to the configuration key (no prefix).

返回

IConfigurationBuilder

IConfigurationBuilderThe IConfigurationBuilder.

示例

一个简单的控制台应用程序,它定义了两个短和四个别名开关映射。A simple console application that has two short and four alias switch mappings defined.

// 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允许从命令行使用其他格式的可选短密钥和别名。The switchMappings allows additional formats for alternative short and alias keys to be used from the command line. 有关支持的标准格式,另请参阅 AddCommandLine 的基本版本。Also see the basic version of AddCommandLine for the standard formats supported.

短密钥以单个短划线开头 ( "-" ) ,并映射到主键名称 (不带前缀) ,并且可以与 equals 或 space 一起使用。Short keys start with a single dash ("-") and are mapped to the main key name (without prefix), and can be used with either equals or space. 应将单短划线映射用于更短的替代开关。The single dash mappings are intended to be used for shorter alternative switches.

请注意,不能直接访问单个短划线开关,但必须使用完整密钥来定义和访问该映射。Note that a single dash switch cannot be accessed directly, but must have a switch mapping defined and accessed using the full key. 传递未定义的单划线参数将导致 FormatExceptionPassing an undefined single dash argument will cause as FormatException.

Short 参数有两种格式: -版 k1 = value1-k2 value2There are two formats for short arguments: -k1=value1 -k2 value2.

别名键定义以两个短划线开头 ( "--" ) ,并映射到主键名称 (不带前缀) ,可用于替代普通键。Alias key definitions start with two dashes ("--") and are mapped to the main key name (without prefix), and can be used in place of the normal key. 当在命令行中使用正斜杠前缀时,它们也会起作用,但不 (前缀等于 format) 。They also work when a forward slash prefix is used in the command line (but not with the no prefix equals format).

别名参数只有四种格式: --alt3 = value3/alt4 = value4--alt5 value5/alt6 value6There are only four formats for aliased arguments: --alt3=value3 /alt4=value4 --alt5 value5 /alt6 value6.

适用于