Abstracted out command arguments a bit

This commit is contained in:
Master Kwoth 2017-11-29 18:14:19 +07:00
parent c723f82d95
commit 471ab6cb27
4 changed files with 38 additions and 5 deletions

View File

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

View File

@ -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>(T options, string[] args) where T : INadekoCommandOptions
{
var res = Parser.Default.ParseArguments<T>(args);
options = (T)res.MapResult(x => x, x => options);
options.NormalizeOptions();
return options;
}
}
}

View File

@ -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<RaceOptions>(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"));

View File

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