共用方式為


CommandLineConfigurationExtensions.AddCommandLine 方法

定義

多載

AddCommandLine(IConfigurationBuilder, Action<CommandLineConfigurationSource>)

新增從命令列讀取組態值的 IConfigurationProvider

AddCommandLine(IConfigurationBuilder, String[])

新增從命令列讀取組態值的 CommandLineConfigurationProvider

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

使用指定的參數對應,新增從命令列讀取設定值的 CommandLineConfigurationProvider

AddCommandLine(IConfigurationBuilder, Action<CommandLineConfigurationSource>)

來源:
CommandLineConfigurationExtensions.cs
來源:
CommandLineConfigurationExtensions.cs
來源:
CommandLineConfigurationExtensions.cs
來源:
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[])

來源:
CommandLineConfigurationExtensions.cs
來源:
CommandLineConfigurationExtensions.cs
來源:
CommandLineConfigurationExtensions.cs
來源:
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>)

來源:
CommandLineConfigurationExtensions.cs
來源:
CommandLineConfigurationExtensions.cs
來源:
CommandLineConfigurationExtensions.cs
來源:
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>

參數對應。 簡短 (具有前置詞 "-") 和 alias 索引鍵 (首碼為 "--") 的字典,對應至組態機碼 (沒有前置詞)。

傳回

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

簡短索引鍵開頭為單一虛線 (“-”) ,並對應至主鍵名稱, (不含前置詞) ,而且可以搭配等於或空格使用。 單一虛線對應是用於較短的替代參數。 無法直接存取單一虛線交換器,但必須使用完整索引鍵來定義及存取交換器對應。 傳遞未定義的單一虛線自變數會導致 FormatException

簡短自變數有兩種格式: -k1=value1-k2 value2

別名索引鍵定義會以兩個虛線開頭 (“--”) ,並對應至沒有前置詞) 的主要索引鍵 (名稱,而且可用來取代一般索引鍵。 在命令行中使用正斜線前置詞時,它們也會運作 (,但沒有前置詞等於格式) 。

別名自變數只有四種格式:

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

適用於