From c495015698e273e9a9ca7695a59f54b7b004b3e3 Mon Sep 17 00:00:00 2001 From: "HOT-ROD\\craig" Date: Sat, 4 Aug 2018 02:37:47 +0100 Subject: [PATCH] Added ValidateFile command-method. I updated the "generate text from file" command-methods to include the new validation functionality. --- WetPancakeCLI/ConsoleCommands.cs | 197 +++++++++++++++++++------------ 1 file changed, 121 insertions(+), 76 deletions(-) diff --git a/WetPancakeCLI/ConsoleCommands.cs b/WetPancakeCLI/ConsoleCommands.cs index a49f060..acd5416 100644 --- a/WetPancakeCLI/ConsoleCommands.cs +++ b/WetPancakeCLI/ConsoleCommands.cs @@ -20,6 +20,7 @@ namespace WetPancakeCLI static FSharpOption _taskCreationOptions = FSharpOption.None; static FSharpOption _cancellationToken = FSharpOption.None; + #region Console Utilities [ListCommand] [Description("Prints a test message to the console.")] [Parameters("None")] @@ -37,17 +38,74 @@ namespace WetPancakeCLI [Parameters("None")] [Usage("> Exit")] public static void Exit() => Environment.Exit(ExitCode); + #endregion + #region Wet Pancake Utilities [ListCommand] - [Description("Generates random text, the number of sentences generated is randomly determined.")] - [Parameters("None")] - [Usage("> GenerateRandomText")] - public static string GenerateRandomText() + [Description( + "Checks to see if the string matches the desired sentence count and removes any over that limit.\n" + + "If the string has less sentences than the number requested, it will not change.\n" + + "Sentences must be greater than 0 and text must contain at least 1 \".\" \"?\" \"!\"")] + [Parameters("sentences: int, text: string")] + [Usage("> CleanText 1 \"This is a test sentence. And, this one needs to be removed.\"")] + public static string CleanText(int sentences, string text) { try { + if (sentences < 1) + throw new ArgumentException("Invalid argument. Must be greater than 0", "sentences"); return FSharpAsync.StartAsTask - (RequestRandomTextAsync(), _taskCreationOptions, _cancellationToken).Result; + (CleanResultAsync(sentences, text), _taskCreationOptions, _cancellationToken).Result; + } + catch (Exception e) + { + Debug.WriteLine(e.Message); + throw; + } + } + + [ListCommand] + [Description("Returns a list of all the available .txt files built-in to Wet Pancake.")] + [Parameters("None")] + [Usage("> RequestAllTemplateFiles")] + public static string RequestAllTemplateFiles() + { + try + { + WriteLine("Attempting to list out the available template files..."); + var files = FSharpAsync.StartAsTask(RequestAllTemplateFilesAsync(), _taskCreationOptions, _cancellationToken).Result; + foreach (var item in files) + { + WriteLine($"File path: {item}"); + } + return "Listing complete."; + } + catch (Exception e) + { + Debug.WriteLine(e.Message); + throw; + } + } + + [ListCommand] + [Description("Checks the text in the specified .txt file to see if it ")] + [Parameters("filePath: string")] + [Usage("> ValidateFile \"C:/your-file.txt\"")] + public static string ValidateFile(string filePath) + { + try + { + WriteLine("Validating file. Please wait..."); + var extension = Path.GetExtension(filePath); + if (string.Equals(extension, ".txt")) + { + var isValid = + FSharpAsync.StartAsTask(TextInFileIsValidAsync(filePath), + _taskCreationOptions, _cancellationToken).Result; + return $"Is Valid: {isValid}"; + } + else + throw new FileLoadException("The file entered is not a plain text (.txt) file.", filePath); } catch (Exception e) { @@ -55,28 +113,30 @@ namespace WetPancakeCLI throw; } } + #endregion + #region Text Generation [ListCommand] - [Description("Generates text using the gibberish-level and number of sentences specified by the user.\n" + - "This command does not run the result through the extra \"cleaning\" process like GenerateCleanText.\n" + - "This means this command is faster but it might produce an extra sentence on the odd occasion.\n" + - "Use this if you prefer speed over accuracy.\n" + - "Gibberish-level must be between 2 and 20." - )] + [Description( + "Generates text using the gibberish-level and number of sentences specified by the user.\n" + + "The result goes through an extra \"cleaning\" process to remove any artefact sentences.\n" + + "Use this if you cannot tolerate the odd extra sentence.\n" + + "With that said, it does mean it is slower than its GenerateText counterpart.\n" + + "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) + [Usage("> GenerateCleanText 5 10")] + public static string GenerateCleanText(int gibberishLevel, int sentences) { try { if (gibberishLevel < 2 || gibberishLevel > 20) throw new ArgumentException - ("Invalid argument. Must be between 2 and 20 (inclusive).", "gibberish-level"); + ("Invalid argument. Must be between 2 and 20 (inclusive).", "gibberish-Level"); if (sentences < 1) throw new ArgumentException ("Invalid argument. Must be greater than 0.", "sentences"); return FSharpAsync.StartAsTask - (RequestTextAsync(gibberishLevel, sentences), _taskCreationOptions, _cancellationToken).Result; + (RequestCleanTextAsync(gibberishLevel, sentences), _taskCreationOptions, _cancellationToken).Result; } catch (Exception e) { @@ -86,28 +146,31 @@ namespace WetPancakeCLI } [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.\n" + - "This command does not run the result through the extra \"cleaning\" process like GenerateCleanTextFromFile.\n" + - "This means this command is faster but it might produce an extra sentence on the odd occasion.\n" + - "Use this if you prefer speed over accuracy.\n" + - "Gibberish-level must be between 2 and 20.")] + [Description( + "Loads the specified .txt file and generates text based on it," + + "using the gibberish-level and number of sentences specified by the user.\n" + + "The result goes through an extra \"cleaning\" process to remove any artefact sentences.\n" + + "Use this if you cannot tolerate the odd extra sentence.\n" + + "With that said, it does mean it is slower than its GenerateTextFromFile counterpart.\n" + + "Gibberish-level must be between 2 and 20.")] [Parameters("gibberish-level: int, sentences: int, file path: string")] - [Usage("> GenerateTextFromFile 3 6 C:/yourfile.txt")] - public static string GenerateTextFromFile(int gibberishLevel, int sentences, string filePath) + [Usage("> GenerateCleanTextFromFile 3 6 C:/yourfile.txt")] + public static string GenerateCleanTextFromFile(int gibberishLevel, int sentences, string filePath) { try { if (gibberishLevel < 2 || gibberishLevel > 20) throw new ArgumentException - ("Invalid argument. Must be between 2 and 20 (inclusive).", "gibberish-level"); + ("Invalid argument. Must be between 2 and 20 (inclusive).", "gibberish-Level"); if (sentences < 1) throw new ArgumentException ("Invalid argument. Must be greater than 0.", "sentences"); - if (Path.GetExtension(filePath) != ".txt") + var extension = Path.GetExtension(filePath); + if (string.Equals(extension, ".txt") != true) throw new FileLoadException ("The file entered is not a plain text (.txt) file.", filePath); return FSharpAsync.StartAsTask - (RequestTextFromFileAsync(gibberishLevel, sentences, filePath), _taskCreationOptions, _cancellationToken).Result; + (RequestCleanTextFromFileAsync(gibberishLevel, sentences, filePath), _taskCreationOptions, _cancellationToken).Result; } catch (Exception e) { @@ -117,20 +180,15 @@ namespace WetPancakeCLI } [ListCommand] - [Description("Returns a list of all the available .txt files built-in to Wet Pancake.")] + [Description("Generates random text, the number of sentences generated is randomly determined.")] [Parameters("None")] - [Usage("> RequestAllTemplateFiles")] - public static string RequestAllTemplateFiles() + [Usage("> GenerateRandomText")] + public static string GenerateRandomText() { try { - WriteLine("Attempting to list out the available template files..."); - var files = FSharpAsync.StartAsTask(RequestAllTemplateFilesAsync(), _taskCreationOptions, _cancellationToken).Result; - foreach (var item in files) - { - WriteLine($"File path: {item}"); - } - return "Listing complete."; + return FSharpAsync.StartAsTask + (RequestRandomTextAsync(), _taskCreationOptions, _cancellationToken).Result; } catch (Exception e) { @@ -140,25 +198,26 @@ namespace WetPancakeCLI } [ListCommand] - [Description("Generates text using the gibberish-level and number of sentences specified by the user.\n" + - "The result goes through an extra \"cleaning\" process to remove any artefact sentences.\n" + - "Use this if you cannot tolerate the odd extra sentence.\n" + - "With that said, it does mean it is slower than its GenerateText counterpart.\n" + - "Gibberish-level must be between 2 and 20.")] + [Description( + "Generates text using the gibberish-level and number of sentences specified by the user.\n" + + "This command does not run the result through the extra \"cleaning\" process like GenerateCleanText.\n" + + "This means this command is faster but it might produce an extra sentence on the odd occasion.\n" + + "Use this if you prefer speed over accuracy.\n" + + "Gibberish-level must be between 2 and 20.")] [Parameters("gibberish-level: int, sentences: int")] - [Usage("> GenerateCleanText 5 10")] - public static string GenerateCleanText(int gibberishLevel, int sentences) + [Usage("> GenerateText 5 10")] + public static string GenerateText(int gibberishLevel, int sentences) { try { if (gibberishLevel < 2 || gibberishLevel > 20) throw new ArgumentException - ("Invalid argument. Must be between 2 and 20 (inclusive).", "gibberish-Level"); + ("Invalid argument. Must be between 2 and 20 (inclusive).", "gibberish-level"); if (sentences < 1) throw new ArgumentException ("Invalid argument. Must be greater than 0.", "sentences"); return FSharpAsync.StartAsTask - (RequestCleanTextAsync(gibberishLevel, sentences), _taskCreationOptions, _cancellationToken).Result; + (RequestTextAsync(gibberishLevel, sentences), _taskCreationOptions, _cancellationToken).Result; } catch (Exception e) { @@ -168,50 +227,35 @@ namespace WetPancakeCLI } [ListCommand] - [Description("Loads the specified .txt file and generates text based on it, using the gibberish-level and number of specified by the user.\n" + - "The result goes through an extra \"cleaning\" process to remove any artefact sentences.\n" + - "Use this if you cannot tolerate the odd extra sentence.\n" + - "With that said, it does mean it is slower than its GenerateTextFromFile counterpart.\n" + - "Gibberish-level must be between 2 and 20.")] + [Description( + "Loads the specified .txt file and generates text based on it using the gibberish-level and number of sentencesspecifiedby the user.\n" + + "This command does not run the result through the extra \"cleaning\" process like GenerateCleanTextFromFile.\n" + + "This means this command is faster but it might produce an extra sentence on the odd occasion.\n" + + "Use this if you prefer speed over accuracy.\n" + + "Gibberish-level must be between 2 and 20.")] [Parameters("gibberish-level: int, sentences: int, file path: string")] - [Usage("> GenerateCleanTextFromFile 3 6 C:/yourfile.txt")] - public static string GenerateCleanTextFromFile(int gibberishLevel, int sentences, string filePath) + [Usage("> GenerateTextFromFile 3 6 C:/yourfile.txt")] + public static string GenerateTextFromFile(int gibberishLevel, int sentences, string filePath) { try { if (gibberishLevel < 2 || gibberishLevel > 20) throw new ArgumentException - ("Invalid argument. Must be between 2 and 20 (inclusive).", "gibberish-Level"); + ("Invalid argument. Must be between 2 and 20 (inclusive).", "gibberish-level"); if (sentences < 1) throw new ArgumentException ("Invalid argument. Must be greater than 0.", "sentences"); - if (Path.GetExtension(filePath) != ".txt") + var extension = Path.GetExtension(filePath); + if (string.Equals(extension, ".txt") != true) throw new FileLoadException ("The file entered is not a plain text (.txt) file.", filePath); + var fileIsValid = + FSharpAsync.StartAsTask(TextInFileIsValidAsync(filePath), + _taskCreationOptions, _cancellationToken).Result; + if (fileIsValid == false) + throw new Exception("The .txt does not contain a vaild end token ('.', '!' or '?')."); return FSharpAsync.StartAsTask - (RequestCleanTextFromFileAsync(gibberishLevel, sentences, filePath), _taskCreationOptions, _cancellationToken).Result; - } - catch (Exception e) - { - Debug.WriteLine(e.Message); - throw; - } - } - - [ListCommand] - [Description("Checks to see if the string matches the desired sentence count and removes any over that limit.\n" + - "If the string has less sentences than the number requested, it will not change.\n" + - "Sentences must be greater than 0 and text must contain at least 1 \".\" \"?\" \"!\"")] - [Parameters("sentences: int, text: string")] - [Usage("> CleanText 1 \"This is a test sentence. And, this one needs to be removed.\"")] - public static string CleanText(int sentences, string text) - { - try - { - if (sentences < 1) - throw new ArgumentException ("Invalid argument. Must be greater than 0", "sentences"); - return FSharpAsync.StartAsTask - (CleanResultAsync(sentences, text), _taskCreationOptions, _cancellationToken).Result; + (RequestTextFromFileAsync(gibberishLevel, sentences, filePath), _taskCreationOptions, _cancellationToken).Result; } catch (Exception e) { @@ -219,5 +263,6 @@ namespace WetPancakeCLI throw; } } + #endregion } }