Replace --include-threads and --include-archived-threads with a single multi-value option

Related to #1119
This commit is contained in:
Tyrrrz 2023-08-28 21:52:56 +03:00
parent 5f6e51f6fb
commit d430f77ae1
8 changed files with 83 additions and 52 deletions

View file

@ -246,7 +246,9 @@ public abstract class ExportCommandBase : DiscordCommandBase
foreach (var (channel, error) in errorsByChannel) foreach (var (channel, error) in errorsByChannel)
{ {
await console.Error.WriteAsync($"{channel.ParentNameWithFallback} / {channel.Name}: "); await console.Error.WriteAsync(
$"{channel.ParentNameWithFallback} / {channel.Name}: "
);
using (console.WithForegroundColor(ConsoleColor.Red)) using (console.WithForegroundColor(ConsoleColor.Red))
await console.Error.WriteLineAsync(error); await console.Error.WriteLineAsync(error);

View file

@ -0,0 +1,22 @@
using System;
using CliFx.Extensibility;
using DiscordChatExporter.Cli.Commands.Shared;
namespace DiscordChatExporter.Cli.Commands.Converters;
internal class ThreadInclusionBindingConverter : BindingConverter<ThreadInclusion>
{
public override ThreadInclusion Convert(string? rawValue)
{
// Empty or unset value is treated as 'active' to match the previous behavior
if (string.IsNullOrWhiteSpace(rawValue))
return ThreadInclusion.Active;
// Boolean 'true' is treated as 'active', boolean 'false' is treated as 'none'
if (bool.TryParse(rawValue, out var boolValue))
return boolValue ? ThreadInclusion.Active : ThreadInclusion.None;
// Otherwise, fall back to regular enum parsing
return Enum.Parse<ThreadInclusion>(rawValue, true);
}
}

View file

@ -7,19 +7,15 @@ internal class TruthyBooleanBindingConverter : BindingConverter<bool>
{ {
public override bool Convert(string? rawValue) public override bool Convert(string? rawValue)
{ {
// Null is still considered true, to match the base behavior // Empty or unset value is treated as 'true', to match the regular boolean behavior
if (rawValue is null)
return true;
if (string.IsNullOrWhiteSpace(rawValue)) 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; return true;
// Number '1' is treated as 'true', other numbers are treated as 'false'
if (int.TryParse(rawValue, CultureInfo.InvariantCulture, out var intValue))
return intValue == 1;
// Otherwise, fall back to regular boolean parsing
return bool.Parse(rawValue);
} }
} }

View file

@ -6,6 +6,8 @@ using CliFx.Attributes;
using CliFx.Exceptions; using CliFx.Exceptions;
using CliFx.Infrastructure; using CliFx.Infrastructure;
using DiscordChatExporter.Cli.Commands.Base; using DiscordChatExporter.Cli.Commands.Base;
using DiscordChatExporter.Cli.Commands.Converters;
using DiscordChatExporter.Cli.Commands.Shared;
using DiscordChatExporter.Core.Discord; using DiscordChatExporter.Core.Discord;
using DiscordChatExporter.Core.Discord.Data; using DiscordChatExporter.Core.Discord.Data;
using DiscordChatExporter.Core.Exceptions; using DiscordChatExporter.Core.Exceptions;
@ -25,11 +27,16 @@ public class ExportAllCommand : ExportCommandBase
[CommandOption("include-vc", Description = "Include voice channels.")] [CommandOption("include-vc", Description = "Include voice channels.")]
public bool IncludeVoiceChannels { get; init; } = true; public bool IncludeVoiceChannels { get; init; } = true;
[CommandOption("include-threads", Description = "Include threads.")] [CommandOption(
public bool IncludeThreads { get; init; } = false; "include-threads",
Description = "Specifies which types of threads should be included.",
Converter = typeof(ThreadInclusionBindingConverter)
)]
public ThreadInclusion ThreadInclusion { get; init; } = ThreadInclusion.None;
[CommandOption("include-archived-threads", Description = "Include archived threads.")] private bool IncludeThreads => ThreadInclusion != ThreadInclusion.None;
public bool IncludeArchivedThreads { get; init; } = false;
private bool IncludeArchivedThreads => ThreadInclusion.HasFlag(ThreadInclusion.Archived);
[CommandOption( [CommandOption(
"data-package", "data-package",
@ -42,14 +49,6 @@ public class ExportAllCommand : ExportCommandBase
{ {
await base.ExecuteAsync(console); await base.ExecuteAsync(console);
// Cannot include archived threads without including active threads as well
if (IncludeArchivedThreads && !IncludeThreads)
{
throw new CommandException(
"Option --include-archived-threads can only be used when --include-threads is also specified."
);
}
var cancellationToken = console.RegisterCancellationHandler(); var cancellationToken = console.RegisterCancellationHandler();
var channels = new List<Channel>(); var channels = new List<Channel>();

View file

@ -1,9 +1,10 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using CliFx.Attributes; using CliFx.Attributes;
using CliFx.Exceptions;
using CliFx.Infrastructure; using CliFx.Infrastructure;
using DiscordChatExporter.Cli.Commands.Base; using DiscordChatExporter.Cli.Commands.Base;
using DiscordChatExporter.Cli.Commands.Converters;
using DiscordChatExporter.Cli.Commands.Shared;
using DiscordChatExporter.Core.Discord; using DiscordChatExporter.Core.Discord;
using DiscordChatExporter.Core.Discord.Data; using DiscordChatExporter.Core.Discord.Data;
@ -18,24 +19,21 @@ public class ExportGuildCommand : ExportCommandBase
[CommandOption("include-vc", Description = "Include voice channels.")] [CommandOption("include-vc", Description = "Include voice channels.")]
public bool IncludeVoiceChannels { get; init; } = true; public bool IncludeVoiceChannels { get; init; } = true;
[CommandOption("include-threads", Description = "Include threads.")] [CommandOption(
public bool IncludeThreads { get; init; } = false; "include-threads",
Description = "Specifies which types of threads should be included.",
Converter = typeof(ThreadInclusionBindingConverter)
)]
public ThreadInclusion ThreadInclusion { get; init; } = ThreadInclusion.None;
[CommandOption("include-archived-threads", Description = "Include archived threads.")] private bool IncludeThreads => ThreadInclusion != ThreadInclusion.None;
public bool IncludeArchivedThreads { get; init; } = false;
private bool IncludeArchivedThreads => ThreadInclusion.HasFlag(ThreadInclusion.Archived);
public override async ValueTask ExecuteAsync(IConsole console) public override async ValueTask ExecuteAsync(IConsole console)
{ {
await base.ExecuteAsync(console); await base.ExecuteAsync(console);
// Cannot include archived threads without including active threads as well
if (IncludeArchivedThreads && !IncludeThreads)
{
throw new CommandException(
"Option --include-archived-threads can only be used when --include-threads is also specified."
);
}
var cancellationToken = console.RegisterCancellationHandler(); var cancellationToken = console.RegisterCancellationHandler();
var channels = new List<Channel>(); var channels = new List<Channel>();

View file

@ -2,9 +2,10 @@
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using CliFx.Attributes; using CliFx.Attributes;
using CliFx.Exceptions;
using CliFx.Infrastructure; using CliFx.Infrastructure;
using DiscordChatExporter.Cli.Commands.Base; using DiscordChatExporter.Cli.Commands.Base;
using DiscordChatExporter.Cli.Commands.Converters;
using DiscordChatExporter.Cli.Commands.Shared;
using DiscordChatExporter.Core.Discord; using DiscordChatExporter.Core.Discord;
using DiscordChatExporter.Core.Discord.Data; using DiscordChatExporter.Core.Discord.Data;
using DiscordChatExporter.Core.Utils.Extensions; using DiscordChatExporter.Core.Utils.Extensions;
@ -20,24 +21,21 @@ public class GetChannelsCommand : DiscordCommandBase
[CommandOption("include-vc", Description = "Include voice channels.")] [CommandOption("include-vc", Description = "Include voice channels.")]
public bool IncludeVoiceChannels { get; init; } = true; public bool IncludeVoiceChannels { get; init; } = true;
[CommandOption("include-threads", Description = "Include threads.")] [CommandOption(
public bool IncludeThreads { get; init; } = false; "include-threads",
Description = "Specifies which types of threads should be included.",
Converter = typeof(ThreadInclusionBindingConverter)
)]
public ThreadInclusion ThreadInclusion { get; init; } = ThreadInclusion.None;
[CommandOption("include-archived-threads", Description = "Include archived threads.")] private bool IncludeThreads => ThreadInclusion != ThreadInclusion.None;
public bool IncludeArchivedThreads { get; init; } = false;
private bool IncludeArchivedThreads => ThreadInclusion.HasFlag(ThreadInclusion.Archived);
public override async ValueTask ExecuteAsync(IConsole console) public override async ValueTask ExecuteAsync(IConsole console)
{ {
await base.ExecuteAsync(console); await base.ExecuteAsync(console);
// Cannot include archived threads without including active threads as well
if (IncludeArchivedThreads && !IncludeThreads)
{
throw new CommandException(
"Option --include-archived-threads can only be used when --include-threads is also specified."
);
}
var cancellationToken = console.RegisterCancellationHandler(); var cancellationToken = console.RegisterCancellationHandler();
var channels = (await Discord.GetGuildChannelsAsync(GuildId, cancellationToken)) var channels = (await Discord.GetGuildChannelsAsync(GuildId, cancellationToken))
@ -77,7 +75,9 @@ public class GetChannelsCommand : DiscordCommandBase
// Channel category / name // Channel category / name
using (console.WithForegroundColor(ConsoleColor.White)) using (console.WithForegroundColor(ConsoleColor.White))
await console.Output.WriteLineAsync($"{channel.ParentNameWithFallback} / {channel.Name}"); await console.Output.WriteLineAsync(
$"{channel.ParentNameWithFallback} / {channel.Name}"
);
var channelThreads = threads.Where(t => t.Parent?.Id == channel.Id).ToArray(); var channelThreads = threads.Where(t => t.Parent?.Id == channel.Id).ToArray();
var channelThreadIdMaxLength = channelThreads var channelThreadIdMaxLength = channelThreads

View file

@ -44,7 +44,9 @@ public class GetDirectChannelsCommand : DiscordCommandBase
// Channel category / name // Channel category / name
using (console.WithForegroundColor(ConsoleColor.White)) using (console.WithForegroundColor(ConsoleColor.White))
await console.Output.WriteLineAsync($"{channel.ParentNameWithFallback} / {channel.Name}"); await console.Output.WriteLineAsync(
$"{channel.ParentNameWithFallback} / {channel.Name}"
);
} }
} }
} }

View file

@ -0,0 +1,12 @@
using System;
namespace DiscordChatExporter.Cli.Commands.Shared;
[Flags]
public enum ThreadInclusion
{
None = 0,
Active = 1,
Archived = 2,
All = Active | Archived
}