ConfigHandler 1.3.2
dotnet add package ConfigHandler --version 1.3.2
NuGet\Install-Package ConfigHandler -Version 1.3.2
<PackageReference Include="ConfigHandler" Version="1.3.2" />
paket add ConfigHandler --version 1.3.2
#r "nuget: ConfigHandler, 1.3.2"
// Install ConfigHandler as a Cake Addin #addin nuget:?package=ConfigHandler&version=1.3.2 // Install ConfigHandler as a Cake Tool #tool nuget:?package=ConfigHandler&version=1.3.2
ConfigHandler
Summary
ConfigHandler allows to load a config file (json format) and possibly override some parameters from command line arguments
The default values may also be set from environment variables.
The config may recursively reference a "parent" config file.
Create a class that derives from ConfigHandler.BaseConfig with all the properties you want to configure, then call :
var config = BaseConfig.LoadAll<ExampleConfig>("exampleConfig.json", args);
And you will get the config loaded from file & arguments
- Usage example:
using System;
using System.Collections.Generic;
using ConfigHandler;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace NUnitConfigHandler
{
// Just a dummy example
public class ExampleConfig : BaseConfig
{
[JsonConverter(typeof(StringEnumConverter))]
public enum Priority
{
Undefined,
Low,
Medium,
High
}
[EnvVar("EXAMPLE_BATCH_PRIORITY")]
[Option("Priority of batch")]
public Priority BatchPriority { get; set; }
[EnvVar("EXAMPLE_SERVER")]
public string Server { get; set; }
[Option("Test date", "yyyy/MM/dd")]
public DateTime TestDate { get; set; }
[Option("List of mail addresses")]
public List<string> MailRecipients { get; set; }
[Option("Level")]
[DefaultValue(true)]
public int Level { get; set; }
public ExampleConfig()
{
}
}
}
....
static void Main(string[] args)
{
var config = BaseConfig.LoadAll<ExampleConfig>("exampleConfig.json", args);
...
}
- Example config files:
exampleConfig.json
{
"ParentConfigFile": "GenericExampleConfig.json",
"MailRecipients": [
"joe@joe.com",
"bill@bill.com"
]
}
GenericExampleConfig.json
{
"BatchPriority": "High",
"MailRecipients": [
"test@test.com"
]
}
Final config will be:
INFO: Loading config file: 'exampleConfig.json'
{
"BatchPriority": "High",
"MailRecipients": [
"joe@joe.com",
"bill@bill.com"
],
"ParentConfigFile": "GenericExampleConfig.json",
"Level": 0
}
--Help option wil display:
Syntax: testhost --option1=... --option2=... (lists are comma separated)
Options for config ExampleConfig:
--Help (Boolean)
Help: Display help
Curent value: True
--BatchPriority (Priority)
Help: Priority of batch
Curent value: Undefined
Possible values: Undefined,Low,Medium,High
Associated Env. Var: EXAMPLE_BATCH_PRIORITY
--Server (String)
Curent value: ""
Associated Env. Var: EXAMPLE_SERVER
--TestDate (DateTime)
Help: Test date
Curent value: 0001/01/01
--MailRecipients (List`1<String>)
Help: List of mail addresses
Curent value: ""
--ConfigFile (String)
Help: The config file to use for startup
Curent value: ""
--ParentConfigFile (String)
Help: Optional parent config file
Curent value: ""
--Version (VersionOption)
Help: Display versions information
Curent value: False
Possible values: False,True,All
--Version will display
Assembly Versions:
Entry : testhost, 15.0.0.0
Executing: ConfigHandler, 1.2.1.0
=====
Loaded assemblies:
testhost, 15.0.0.0 (g:\GlobalNugetsCache\microsoft.testplatform.testhost\16.4.0\build\netcoreapp2.1\x64\testhost.dll)
FrameworkName = .NETCoreApp,Version=v2.1
InformationalVersion = 16.4.0
Copyright = � Microsoft Corporation. All rights reserved.
Company = Microsoft Corporation
netstandard, 2.1.0.0 (C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.4\netstandard.dll)
InformationalVersion = 3.1.4+059a4a19e602494bfbed473dbbb18f2dbfbd0878
Description = netstandard
Copyright = � Microsoft Corporation. All rights reserved.
Company = Microsoft Corporation
Newtonsoft.Json, 12.0.0.0 (G:\my_projects\ConfigHandler\NUnitConfigHandler\bin\Release\netcoreapp3.1\Newtonsoft.Json.dll)
FrameworkName = .NETStandard,Version=v2.0
InformationalVersion = 12.0.3+7c3d7f8da7e35dde8fa74188b0decff70f8f10e3
Configuration = Release
Description = Json.NET is a popular high-performance JSON framework for .NET
Copyright = Copyright � James Newton-King 2008
Company = Newtonsoft
...
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Newtonsoft.Json (>= 13.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.