From 471ab6cb27a507fd5e1b263d1a9901edb42c8880 Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Wed, 29 Nov 2017 18:14:19 +0700 Subject: [PATCH] Abstracted out command arguments a bit --- .../Common/INadekoCommandOptions.cs | 13 ++++++++++++ NadekoBot.Core/Common/OptionsParser.cs | 20 +++++++++++++++++++ .../Modules/Gambling/AnimalRacingCommands.cs | 7 +++---- .../Common/AnimalRacing/RaceOptions.cs | 3 ++- 4 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 NadekoBot.Core/Common/INadekoCommandOptions.cs create mode 100644 NadekoBot.Core/Common/OptionsParser.cs diff --git a/NadekoBot.Core/Common/INadekoCommandOptions.cs b/NadekoBot.Core/Common/INadekoCommandOptions.cs new file mode 100644 index 00000000..ed5786b7 --- /dev/null +++ b/NadekoBot.Core/Common/INadekoCommandOptions.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NadekoBot.Core.Common +{ + public interface INadekoCommandOptions + { + void NormalizeOptions(); + } +} diff --git a/NadekoBot.Core/Common/OptionsParser.cs b/NadekoBot.Core/Common/OptionsParser.cs new file mode 100644 index 00000000..9a05a606 --- /dev/null +++ b/NadekoBot.Core/Common/OptionsParser.cs @@ -0,0 +1,20 @@ +using CommandLine; + +namespace NadekoBot.Core.Common +{ + public class OptionsParser + { + private static OptionsParser _instance = new OptionsParser(); + public static OptionsParser Default => _instance; + + static OptionsParser() { } + + public T ParseFrom(T options, string[] args) where T : INadekoCommandOptions + { + var res = Parser.Default.ParseArguments(args); + options = (T)res.MapResult(x => x, x => options); + options.NormalizeOptions(); + return options; + } + } +} diff --git a/NadekoBot.Core/Modules/Gambling/AnimalRacingCommands.cs b/NadekoBot.Core/Modules/Gambling/AnimalRacingCommands.cs index 1a712dd2..07e3ac3f 100644 --- a/NadekoBot.Core/Modules/Gambling/AnimalRacingCommands.cs +++ b/NadekoBot.Core/Modules/Gambling/AnimalRacingCommands.cs @@ -12,6 +12,7 @@ using NadekoBot.Modules.Gambling.Common.AnimalRacing; using NadekoBot.Modules.Gambling.Services; using NadekoBot.Core.Modules.Gambling.Common.AnimalRacing; using CommandLine; +using NadekoBot.Core.Common; namespace NadekoBot.Modules.Gambling { @@ -38,10 +39,8 @@ namespace NadekoBot.Modules.Gambling [NadekoOptions(typeof(RaceOptions))] public Task Race(params string[] args) { - var options = new RaceOptions(); - var res = Parser.Default.ParseArguments(args); - options = res.MapResult(x => x, x => options); - options.NormalizeOptions(); + var options = OptionsParser.Default.ParseFrom(new RaceOptions(), args); + var ar = new AnimalRace(options, _cs, _bc.BotConfig.RaceAnimals.Shuffle().ToArray()); if (!_service.AnimalRaces.TryAdd(Context.Guild.Id, ar)) return Context.Channel.SendErrorAsync(GetText("animal_race"), GetText("animal_race_already_started")); diff --git a/NadekoBot.Core/Modules/Gambling/Common/AnimalRacing/RaceOptions.cs b/NadekoBot.Core/Modules/Gambling/Common/AnimalRacing/RaceOptions.cs index cf88546a..86295bbd 100644 --- a/NadekoBot.Core/Modules/Gambling/Common/AnimalRacing/RaceOptions.cs +++ b/NadekoBot.Core/Modules/Gambling/Common/AnimalRacing/RaceOptions.cs @@ -1,8 +1,9 @@ using CommandLine; +using NadekoBot.Core.Common; namespace NadekoBot.Core.Modules.Gambling.Common.AnimalRacing { - public class RaceOptions + public class RaceOptions : INadekoCommandOptions { [Option('s', "start-time", Default = 20, Required = false)] public int StartTime { get; set; } = 20;