Browse Source

Added input validation to the command-methods.

Throw exceptions when argument is invalid.
master
Craig Oates 6 years ago
parent
commit
66307167d5
  1. 23
      WetPancakeCLI/ConsoleCommands.cs

23
WetPancakeCLI/ConsoleCommands.cs

@ -8,6 +8,7 @@ using System.Threading.Tasks;
using Microsoft.FSharp.Control;
using Microsoft.FSharp.Core;
using System.Threading;
using System.IO;
namespace WetPancakeCLI
{
@ -42,47 +43,47 @@ namespace WetPancakeCLI
[Usage("> GenerateRandomText")]
public static string GenerateRandomText()
{
string result = String.Empty;
try { return FSharpAsync.StartAsTask(RequestRandomTextAsync(), _taskCreationOptions, _cancellationToken).Result; }
catch (Exception e)
{
Debug.WriteLine(e.Message);
System.Console.WriteLine("[ERROR] Unable to complete request. Re-executing command...");
System.Console.WriteLine("[WARNING] Unable to complete last request. Re-executing command...");
return FSharpAsync.StartAsTask(RequestRandomTextAsync(), _taskCreationOptions, _cancellationToken).Result;
}
}
[ListCommand]
[Description("Generates text using the gibberish level and number of sentences specified by the user. Gibberish level must be greater the 1.")]
[Description("Generates text using the gibberish level and number of sentences specified by the user. Gibberish level must be between 2 and 20.")]
[Parameters("gibberish level: int, sentences: int")]
[Usage("> GenerateText 5 10")]
public static string GenerateText(int gibberishLevel, int sentences)
{
if (gibberishLevel < 2) return "Please enter a gibberish level greater than 1.";
if (gibberishLevel < 2 || gibberishLevel > 20) throw new ArgumentException("Invalid argument. Must be between 2 and 20 (inclusive).", "gibberishLevel");
if (sentences < 1) throw new ArgumentException("Invalid argument. Must be greater than 0.", "sentences");
try { return FSharpAsync.StartAsTask(RequestTextAsync(gibberishLevel, sentences), _taskCreationOptions, _cancellationToken).Result; }
catch (Exception e)
{
Debug.WriteLine(e.Message);
System.Console.WriteLine("[ERROR] Unable to complete request. Re-executing command...");
System.Console.WriteLine("[WARNING] Unable to complete last request. Re-executing command...");
return FSharpAsync.StartAsTask(RequestTextAsync(gibberishLevel, sentences), _taskCreationOptions, _cancellationToken).Result;
}
}
// YOU ARE WORKING IN HERE... (0.5-P1)
[ListCommand]
[Description("Loads the specified .txt file and generates text based on it using the gibberish level and number of sentences specified by the user. Gibberish level must be greater the 1.")]
[Description("Loads the specified .txt file and generates text based on it using the gibberish level and number of sentences specified by the user. Gibberish level must be between 2 and 20.")]
[Parameters("gibberish level: int, sentences: int, file path: string")]
[Usage("> RequestTextFromFile 3 6 C:/yourfile.txt")]
[Usage("> GenerateTextFromFile 3 6 C:/yourfile.txt")]
public static string GenerateTextFromFile(int gibberishLevel, int sentences, string filePath)
{
// Could do with checking if the file type is .txt...
if (gibberishLevel < 2) return "Please enter a gibberish level greater than 1.";
if (gibberishLevel < 2 || gibberishLevel > 20) throw new ArgumentException("Invalid argument. Must be between 2 and 20 (inclusive).", "gibberishLevel");
if (sentences < 1) throw new ArgumentException("Invalid argument. Must be greater than 0.", "sentences");
if (Path.GetExtension(filePath) != ".txt") throw new FileLoadException("The file entered is not a plain text (.txt) file.", filePath);
try
{ return FSharpAsync.StartAsTask(RequestTextFromFileAsync(gibberishLevel, sentences, filePath), _taskCreationOptions, _cancellationToken).Result; }
catch (Exception e)
{
Debug.WriteLine(e.Message);
System.Console.WriteLine("[ERROR] Unable to complete request. Re-executing command...");
System.Console.WriteLine("[WARNING] Unable to complete last request. Re-executing command...");
return FSharpAsync.StartAsTask(RequestTextFromFileAsync(gibberishLevel, sentences, filePath), _taskCreationOptions, _cancellationToken).Result;
}
}

Loading…
Cancel
Save