openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Utility/AppContextSwitchHelper.cs
33lines · modeblame
d5b5c604Liudmila Molkova1 years ago | 1 | using System; |
| 2 | | |
| 3 | namespace OpenAI; | |
| 4 | | |
| 5 | internal static class AppContextSwitchHelper | |
| 6 | { | |
| 7 | /// <summary> | |
| 8 | /// Determines if either an AppContext switch or its corresponding Environment Variable is set | |
| 9 | /// </summary> | |
8a376a68大師兄丶1 years ago | 10 | /// <param name="appContextSwitchName">Name of the AppContext switch.</param> |
d5b5c604Liudmila Molkova1 years ago | 11 | /// <param name="environmentVariableName">Name of the Environment variable.</param> |
| 12 | /// <returns>If the AppContext switch has been set, returns the value of the switch. | |
| 13 | /// If the AppContext switch has not been set, returns the value of the environment variable. | |
| 14 | /// False if neither is set. | |
| 15 | /// </returns> | |
8a376a68大師兄丶1 years ago | 16 | public static bool GetConfigValue(string appContextSwitchName, string environmentVariableName) |
d5b5c604Liudmila Molkova1 years ago | 17 | { |
| 18 | // First check for the AppContext switch, giving it priority over the environment variable. | |
8a376a68大師兄丶1 years ago | 19 | if (AppContext.TryGetSwitch(appContextSwitchName, out bool value)) |
d5b5c604Liudmila Molkova1 years ago | 20 | { |
| 21 | return value; | |
| 22 | } | |
| 23 | // AppContext switch wasn't used. Check the environment variable. | |
| 24 | string envVar = Environment.GetEnvironmentVariable(environmentVariableName); | |
| 25 | if (envVar != null && (envVar.Equals("true", StringComparison.OrdinalIgnoreCase) || envVar.Equals("1"))) | |
| 26 | { | |
| 27 | return true; | |
| 28 | } | |
| 29 | | |
| 30 | // Default to false. | |
| 31 | return false; | |
| 32 | } | |
| 33 | } |