From 2e1636e6c9decdc3b5415ac89e45e9712b7be106 Mon Sep 17 00:00:00 2001 From: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com> Date: Thu, 23 Mar 2023 13:23:32 +0200 Subject: [PATCH] Re-implement changes in the previous commit --- .../Commands/Base/ExportCommandBase.cs | 5 +++- .../TruthyBooleanBindingConverter.cs | 26 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 DiscordChatExporter.Cli/Commands/Converters/TruthyBooleanBindingConverter.cs diff --git a/DiscordChatExporter.Cli/Commands/Base/ExportCommandBase.cs b/DiscordChatExporter.Cli/Commands/Base/ExportCommandBase.cs index 40255b3..ec40184 100644 --- a/DiscordChatExporter.Cli/Commands/Base/ExportCommandBase.cs +++ b/DiscordChatExporter.Cli/Commands/Base/ExportCommandBase.cs @@ -7,6 +7,7 @@ using System.Threading.Tasks; using CliFx.Attributes; using CliFx.Exceptions; using CliFx.Infrastructure; +using DiscordChatExporter.Cli.Commands.Converters; using DiscordChatExporter.Cli.Utils.Extensions; using DiscordChatExporter.Core.Discord; using DiscordChatExporter.Core.Discord.Data; @@ -120,7 +121,9 @@ public abstract class ExportCommandBase : DiscordCommandBase [CommandOption( "fuck-russia", - Description = "Don't print the Support Ukraine message to the console." + EnvironmentVariable = "FUCK_RUSSIA", + Description = "Don't print the Support Ukraine message to the console.", + Converter = typeof(TruthyBooleanBindingConverter) )] public bool IsUkraineSupportMessageDisabled { get; init; } diff --git a/DiscordChatExporter.Cli/Commands/Converters/TruthyBooleanBindingConverter.cs b/DiscordChatExporter.Cli/Commands/Converters/TruthyBooleanBindingConverter.cs new file mode 100644 index 0000000..c813876 --- /dev/null +++ b/DiscordChatExporter.Cli/Commands/Converters/TruthyBooleanBindingConverter.cs @@ -0,0 +1,26 @@ +using System; +using System.Globalization; +using CliFx.Extensibility; + +namespace DiscordChatExporter.Cli.Commands.Converters; + +internal class TruthyBooleanBindingConverter : BindingConverter +{ + public override bool Convert(string? rawValue) + { + // Null is still considered true, to match the base behavior + if (rawValue is null) + return true; + + if (string.IsNullOrWhiteSpace(rawValue)) + return false; + + if (bool.TryParse(rawValue, out var boolValue)) + return boolValue; + + if (int.TryParse(rawValue, CultureInfo.InvariantCulture, out var intValue) && intValue == 0) + return false; + + return true; + } +} \ No newline at end of file