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

Related to #1119
pull/1125/head
Tyrrrz 1 year ago
parent 5f6e51f6fb
commit d430f77ae1

@ -246,7 +246,9 @@ public abstract class ExportCommandBase : DiscordCommandBase
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))
await console.Error.WriteLineAsync(error);

@ -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);
}
}

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

@ -6,6 +6,8 @@ using CliFx.Attributes;
using CliFx.Exceptions;
using CliFx.Infrastructure;
using DiscordChatExporter.Cli.Commands.Base;
using DiscordChatExporter.Cli.Commands.Converters;
using DiscordChatExporter.Cli.Commands.Shared;
using DiscordChatExporter.Core.Discord;
using DiscordChatExporter.Core.Discord.Data;
using DiscordChatExporter.Core.Exceptions;
@ -25,11 +27,16 @@ public class ExportAllCommand : ExportCommandBase
[CommandOption("include-vc", Description = "Include voice channels.")]
public bool IncludeVoiceChannels { get; init; } = true;
[CommandOption("include-threads", Description = "Include threads.")]
public bool IncludeThreads { get; init; } = false;
[CommandOption(
"include-threads",
Description = "Specifies which types of threads should be included.",
Converter = typeof(ThreadInclusionBindingConverter)
)]
public ThreadInclusion ThreadInclusion { get; init; } = ThreadInclusion.None;
private bool IncludeThreads => ThreadInclusion != ThreadInclusion.None;
[CommandOption("include-archived-threads", Description = "Include archived threads.")]
public bool IncludeArchivedThreads { get; init; } = false;
private bool IncludeArchivedThreads => ThreadInclusion.HasFlag(ThreadInclusion.Archived);
[CommandOption(
"data-package",
@ -42,14 +49,6 @@ public class ExportAllCommand : ExportCommandBase
{
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 channels = new List<Channel>();

@ -1,9 +1,10 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using CliFx.Attributes;
using CliFx.Exceptions;
using CliFx.Infrastructure;
using DiscordChatExporter.Cli.Commands.Base;
using DiscordChatExporter.Cli.Commands.Converters;
using DiscordChatExporter.Cli.Commands.Shared;
using DiscordChatExporter.Core.Discord;
using DiscordChatExporter.Core.Discord.Data;
@ -18,24 +19,21 @@ public class ExportGuildCommand : ExportCommandBase
[CommandOption("include-vc", Description = "Include voice channels.")]
public bool IncludeVoiceChannels { get; init; } = true;
[CommandOption("include-threads", Description = "Include threads.")]
public bool IncludeThreads { get; init; } = false;
[CommandOption(
"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.")]
public bool IncludeArchivedThreads { get; init; } = false;
private bool IncludeThreads => ThreadInclusion != ThreadInclusion.None;
private bool IncludeArchivedThreads => ThreadInclusion.HasFlag(ThreadInclusion.Archived);
public override async ValueTask ExecuteAsync(IConsole 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 channels = new List<Channel>();

@ -2,9 +2,10 @@
using System.Linq;
using System.Threading.Tasks;
using CliFx.Attributes;
using CliFx.Exceptions;
using CliFx.Infrastructure;
using DiscordChatExporter.Cli.Commands.Base;
using DiscordChatExporter.Cli.Commands.Converters;
using DiscordChatExporter.Cli.Commands.Shared;
using DiscordChatExporter.Core.Discord;
using DiscordChatExporter.Core.Discord.Data;
using DiscordChatExporter.Core.Utils.Extensions;
@ -20,24 +21,21 @@ public class GetChannelsCommand : DiscordCommandBase
[CommandOption("include-vc", Description = "Include voice channels.")]
public bool IncludeVoiceChannels { get; init; } = true;
[CommandOption("include-threads", Description = "Include threads.")]
public bool IncludeThreads { get; init; } = false;
[CommandOption(
"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.")]
public bool IncludeArchivedThreads { get; init; } = false;
private bool IncludeThreads => ThreadInclusion != ThreadInclusion.None;
private bool IncludeArchivedThreads => ThreadInclusion.HasFlag(ThreadInclusion.Archived);
public override async ValueTask ExecuteAsync(IConsole 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 channels = (await Discord.GetGuildChannelsAsync(GuildId, cancellationToken))
@ -77,7 +75,9 @@ public class GetChannelsCommand : DiscordCommandBase
// Channel category / name
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 channelThreadIdMaxLength = channelThreads

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

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