From 83ea7ae317e1d082c9031bc64a128ddd9f961708 Mon Sep 17 00:00:00 2001 From: Alexey Golub Date: Sat, 5 Aug 2017 21:49:29 +0300 Subject: [PATCH] Change command line argument style --- .../DiscordChatExporter.csproj | 1 - DiscordChatExporter/Models/Options.cs | 15 +++++++ DiscordChatExporter/Options.cs | 32 --------------- DiscordChatExporter/Program.cs | 41 ++++++++++++++++--- Readme.md | 3 +- 5 files changed, 51 insertions(+), 41 deletions(-) create mode 100644 DiscordChatExporter/Models/Options.cs delete mode 100644 DiscordChatExporter/Options.cs diff --git a/DiscordChatExporter/DiscordChatExporter.csproj b/DiscordChatExporter/DiscordChatExporter.csproj index 2119c69..1b14a07 100644 --- a/DiscordChatExporter/DiscordChatExporter.csproj +++ b/DiscordChatExporter/DiscordChatExporter.csproj @@ -14,7 +14,6 @@ - diff --git a/DiscordChatExporter/Models/Options.cs b/DiscordChatExporter/Models/Options.cs new file mode 100644 index 0000000..41585b4 --- /dev/null +++ b/DiscordChatExporter/Models/Options.cs @@ -0,0 +1,15 @@ +namespace DiscordChatExporter.Models +{ + public class Options + { + public string Token { get; } + + public string ChannelId { get; } + + public Options(string token, string channelId) + { + Token = token; + ChannelId = channelId; + } + } +} \ No newline at end of file diff --git a/DiscordChatExporter/Options.cs b/DiscordChatExporter/Options.cs deleted file mode 100644 index 17c4070..0000000 --- a/DiscordChatExporter/Options.cs +++ /dev/null @@ -1,32 +0,0 @@ -using CommandLine; -using CommandLine.Text; - -namespace DiscordChatExporter -{ - public class Options - { - [Option('t', "token", Required = true, HelpText = "Discord access token")] - public string Token { get; set; } - - [Option('c', "channel", Required = true, HelpText = "ID of the text channel to export")] - public string ChannelId { get; set; } - - [HelpOption] - public string GetUsage() - { - var help = new HelpText - { - Heading = new HeadingInfo("DiscordChatExporter"), - Copyright = new CopyrightInfo("Alexey 'Tyrrrz' Golub", 2017), - AdditionalNewLineAfterOption = true, - AddDashesToOption = true - }; - help.AddPreOptionsLine("Usage: DiscordChatExporter.exe " + - "-t REkOTVqm9RWOTNOLCdiuMpWd.QiglBz.Lub0E0TZ1xX4ZxCtnwtpBhWt3v1 " + - "-c 459360869055190534"); - help.AddOptions(this); - - return help; - } - } -} \ No newline at end of file diff --git a/DiscordChatExporter/Program.cs b/DiscordChatExporter/Program.cs index b39349d..b281938 100644 --- a/DiscordChatExporter/Program.cs +++ b/DiscordChatExporter/Program.cs @@ -1,30 +1,59 @@ using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; using System.Threading.Tasks; using DiscordChatExporter.Models; using DiscordChatExporter.Services; +using Tyrrrz.Extensions; namespace DiscordChatExporter { public static class Program { - private static readonly Options Options = new Options(); - private static readonly DiscordApiService DiscordApiService = new DiscordApiService(); private static readonly ExportService ExportService = new ExportService(); + private static Options GetOptions(string[] args) + { + // Parse the arguments + var argsDic = new Dictionary(StringComparer.OrdinalIgnoreCase); + foreach (string arg in args) + { + var match = Regex.Match(arg, "/(.*?):\"?(.*?)\"?$"); + string key = match.Groups[1].Value; + string value = match.Groups[2].Value; + + if (key.IsBlank()) + continue; + + argsDic[key] = value; + } + + // Extract required arguments + string token = argsDic.GetOrDefault("token"); + string channelId = argsDic.GetOrDefault("channelId"); + + // Verify arguments + if (token.IsBlank() || channelId.IsBlank()) + throw new ArgumentException("Some or all required command line arguments are missing"); + + // Create option set + return new Options(token, channelId); + } + private static async Task MainAsync(string[] args) { // Parse cmd args - CommandLine.Parser.Default.ParseArgumentsStrict(args, Options); + var options = GetOptions(args); // Get messages Console.WriteLine("Getting messages..."); - var messages = await DiscordApiService.GetMessagesAsync(Options.Token, Options.ChannelId); - var chatLog = new ChatLog(Options.ChannelId, messages); + var messages = await DiscordApiService.GetMessagesAsync(options.Token, options.ChannelId); + var chatLog = new ChatLog(options.ChannelId, messages); // Export Console.WriteLine("Exporting messages..."); - ExportService.Export($"{Options.ChannelId}.html", chatLog); + ExportService.Export($"{options.ChannelId}.html", chatLog); } public static void Main(string[] args) diff --git a/Readme.md b/Readme.md index 9dc4edd..cfacaac 100644 --- a/Readme.md +++ b/Readme.md @@ -25,11 +25,10 @@ You can get your token by opening the Discord app, pressing `Ctrl+Shift+I`, navi You can get the channel ID by enabling `Developer Mode` in `Settings > Appearance` and then right clicking on the channel and clicking on `Copy ID`. -- `DiscordChatExporter.exe -t REkOTVqm9RWOTNOLCdiuMpWd.QiglBz.Lub0E0TZ1xX4ZxCtnwtpBhWt3v1 -c 459360869055190534` +- `DiscordChatExporter.exe /token:REkOTVqm9RWOTNOLCdiuMpWd.QiglBz.Lub0E0TZ1xX4ZxCtnwtpBhWt3v1 /channelId:459360869055190534` ## Libraries used -- [CommandLineParser](https://github.com/gsscoder/commandline) - [HtmlAgilityPack](https://github.com/zzzprojects/html-agility-pack) - [Newtonsoft.Json](https://github.com/JamesNK/Newtonsoft.Json) - [Tyrrrz.Extensions](https://github.com/Tyrrrz/Extensions)