diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-07-02 02:45:02 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-07-02 02:45:02 -0400 |
commit | 2ca49fba62f59135c2ed3ec7958cb78073ff486b (patch) | |
tree | 5b4e5251606622f615b105b683a2722f4e6b046c /src/TrainerMod/Framework/Commands/World | |
parent | f9482906ae7ce4dfd41bb4236e094be5d4fa7689 (diff) | |
download | SMAPI-2ca49fba62f59135c2ed3ec7958cb78073ff486b.tar.gz SMAPI-2ca49fba62f59135c2ed3ec7958cb78073ff486b.tar.bz2 SMAPI-2ca49fba62f59135c2ed3ec7958cb78073ff486b.zip |
encapsulate TrainerMod's argument parsing (#302)
Diffstat (limited to 'src/TrainerMod/Framework/Commands/World')
7 files changed, 34 insertions, 67 deletions
diff --git a/src/TrainerMod/Framework/Commands/World/DownMineLevelCommand.cs b/src/TrainerMod/Framework/Commands/World/DownMineLevelCommand.cs index 2700a0dc..4e62cf77 100644 --- a/src/TrainerMod/Framework/Commands/World/DownMineLevelCommand.cs +++ b/src/TrainerMod/Framework/Commands/World/DownMineLevelCommand.cs @@ -18,7 +18,7 @@ namespace TrainerMod.Framework.Commands.World /// <param name="monitor">Writes messages to the console and log file.</param> /// <param name="command">The command name.</param> /// <param name="args">The command arguments.</param> - public override void Handle(IMonitor monitor, string command, string[] args) + public override void Handle(IMonitor monitor, string command, ArgumentParser args) { int level = (Game1.currentLocation as MineShaft)?.mineLevel ?? 0; monitor.Log($"OK, warping you to mine level {level + 1}.", LogLevel.Info); diff --git a/src/TrainerMod/Framework/Commands/World/FreezeTimeCommand.cs b/src/TrainerMod/Framework/Commands/World/FreezeTimeCommand.cs index 89cd68cb..13d08398 100644 --- a/src/TrainerMod/Framework/Commands/World/FreezeTimeCommand.cs +++ b/src/TrainerMod/Framework/Commands/World/FreezeTimeCommand.cs @@ -35,23 +35,18 @@ namespace TrainerMod.Framework.Commands.World /// <param name="monitor">Writes messages to the console and log file.</param> /// <param name="command">The command name.</param> /// <param name="args">The command arguments.</param> - public override void Handle(IMonitor monitor, string command, string[] args) + public override void Handle(IMonitor monitor, string command, ArgumentParser args) { if (args.Any()) { - if (int.TryParse(args[0], out int value)) - { - if (value == 0 || value == 1) - { - this.FreezeTime = value == 1; - FreezeTimeCommand.FrozenTime = Game1.timeOfDay; - monitor.Log($"OK, time is now {(this.FreezeTime ? "frozen" : "resumed")}.", LogLevel.Info); - } - else - this.LogUsageError(monitor, "The value should be 0 (not frozen), 1 (frozen), or empty (toggle).", command); - } - else - this.LogArgumentNotInt(monitor, command); + // parse arguments + if (!args.TryGetInt(0, "value", out int value, min: 0, max: 1)) + return; + + // handle + this.FreezeTime = value == 1; + FreezeTimeCommand.FrozenTime = Game1.timeOfDay; + monitor.Log($"OK, time is now {(this.FreezeTime ? "frozen" : "resumed")}.", LogLevel.Info); } else { diff --git a/src/TrainerMod/Framework/Commands/World/SetDayCommand.cs b/src/TrainerMod/Framework/Commands/World/SetDayCommand.cs index e47b76a7..54267384 100644 --- a/src/TrainerMod/Framework/Commands/World/SetDayCommand.cs +++ b/src/TrainerMod/Framework/Commands/World/SetDayCommand.cs @@ -18,24 +18,18 @@ namespace TrainerMod.Framework.Commands.World /// <param name="monitor">Writes messages to the console and log file.</param> /// <param name="command">The command name.</param> /// <param name="args">The command arguments.</param> - public override void Handle(IMonitor monitor, string command, string[] args) + public override void Handle(IMonitor monitor, string command, ArgumentParser args) { - // validate + // no-argument mode if (!args.Any()) { monitor.Log($"The current date is {Game1.currentSeason} {Game1.dayOfMonth}. Specify a value to change the day.", LogLevel.Info); return; } - if (!int.TryParse(args[0], out int day)) - { - this.LogArgumentNotInt(monitor, command); - return; - } - if (day > 28 || day <= 0) - { - this.LogUsageError(monitor, "That isn't a valid day.", command); + + // parse arguments + if (!args.TryGetInt(0, "day", out int day, min: 1, max: 28)) return; - } // handle Game1.dayOfMonth = day; diff --git a/src/TrainerMod/Framework/Commands/World/SetMineLevelCommand.cs b/src/TrainerMod/Framework/Commands/World/SetMineLevelCommand.cs index bfcc566f..225ec091 100644 --- a/src/TrainerMod/Framework/Commands/World/SetMineLevelCommand.cs +++ b/src/TrainerMod/Framework/Commands/World/SetMineLevelCommand.cs @@ -1,5 +1,4 @@ using System; -using System.Linq; using StardewModdingAPI; using StardewValley; @@ -19,19 +18,11 @@ namespace TrainerMod.Framework.Commands.World /// <param name="monitor">Writes messages to the console and log file.</param> /// <param name="command">The command name.</param> /// <param name="args">The command arguments.</param> - public override void Handle(IMonitor monitor, string command, string[] args) + public override void Handle(IMonitor monitor, string command, ArgumentParser args) { - // validate - if (!args.Any()) - { - this.LogArgumentsInvalid(monitor, command); + // parse arguments + if (!args.TryGetInt(0, "mine level", out int level, min: 1)) return; - } - if (!int.TryParse(args[0], out int level)) - { - this.LogArgumentNotInt(monitor, command); - return; - } // handle level = Math.Max(1, level); diff --git a/src/TrainerMod/Framework/Commands/World/SetSeasonCommand.cs b/src/TrainerMod/Framework/Commands/World/SetSeasonCommand.cs index d60f8601..96c3d920 100644 --- a/src/TrainerMod/Framework/Commands/World/SetSeasonCommand.cs +++ b/src/TrainerMod/Framework/Commands/World/SetSeasonCommand.cs @@ -25,22 +25,21 @@ namespace TrainerMod.Framework.Commands.World /// <param name="monitor">Writes messages to the console and log file.</param> /// <param name="command">The command name.</param> /// <param name="args">The command arguments.</param> - public override void Handle(IMonitor monitor, string command, string[] args) + public override void Handle(IMonitor monitor, string command, ArgumentParser args) { - // validate + // no-argument mode if (!args.Any()) { monitor.Log($"The current season is {Game1.currentSeason}. Specify a value to change it.", LogLevel.Info); return; } - if (!this.ValidSeasons.Contains(args[0])) - { - this.LogUsageError(monitor, "That isn't a valid season name.", command); + + // parse arguments + if (!args.TryGet(0, "season", out string season, oneOf: this.ValidSeasons)) return; - } // handle - Game1.currentSeason = args[0]; + Game1.currentSeason = season; monitor.Log($"OK, the date is now {Game1.currentSeason} {Game1.dayOfMonth}.", LogLevel.Info); } } diff --git a/src/TrainerMod/Framework/Commands/World/SetTimeCommand.cs b/src/TrainerMod/Framework/Commands/World/SetTimeCommand.cs index 4ecff485..c827ea5e 100644 --- a/src/TrainerMod/Framework/Commands/World/SetTimeCommand.cs +++ b/src/TrainerMod/Framework/Commands/World/SetTimeCommand.cs @@ -18,24 +18,18 @@ namespace TrainerMod.Framework.Commands.World /// <param name="monitor">Writes messages to the console and log file.</param> /// <param name="command">The command name.</param> /// <param name="args">The command arguments.</param> - public override void Handle(IMonitor monitor, string command, string[] args) + public override void Handle(IMonitor monitor, string command, ArgumentParser args) { - // validate + // no-argument mode if (!args.Any()) { monitor.Log($"The current time is {Game1.timeOfDay}. Specify a value to change it.", LogLevel.Info); return; } - if (!int.TryParse(args[0], out int time)) - { - this.LogArgumentNotInt(monitor, command); - return; - } - if (time > 2600 || time < 600) - { - this.LogUsageError(monitor, "That isn't a valid time.", command); + + // parse arguments + if (!args.TryGetInt(0, "time", out int time, min: 600, max: 2600)) return; - } // handle Game1.timeOfDay = time; diff --git a/src/TrainerMod/Framework/Commands/World/SetYearCommand.cs b/src/TrainerMod/Framework/Commands/World/SetYearCommand.cs index 6b2b0d93..760fc170 100644 --- a/src/TrainerMod/Framework/Commands/World/SetYearCommand.cs +++ b/src/TrainerMod/Framework/Commands/World/SetYearCommand.cs @@ -18,24 +18,18 @@ namespace TrainerMod.Framework.Commands.World /// <param name="monitor">Writes messages to the console and log file.</param> /// <param name="command">The command name.</param> /// <param name="args">The command arguments.</param> - public override void Handle(IMonitor monitor, string command, string[] args) + public override void Handle(IMonitor monitor, string command, ArgumentParser args) { - // validate + // no-argument mode if (!args.Any()) { monitor.Log($"The current year is {Game1.year}. Specify a value to change the year.", LogLevel.Info); return; } - if (!int.TryParse(args[0], out int year)) - { - this.LogArgumentNotInt(monitor, command); - return; - } - if (year < 1) - { - this.LogUsageError(monitor, "That isn't a valid year.", command); + + // parse arguments + if (!args.TryGetInt(0, "year", out int year, min: 1)) return; - } // handle Game1.year = year; |