Browse Source

Added ValidateFile command-method.

I updated the "generate text from file" command-methods to include the new validation functionality.
master
Craig Oates 6 years ago
parent
commit
c495015698
  1. 197
      WetPancakeCLI/ConsoleCommands.cs

197
WetPancakeCLI/ConsoleCommands.cs

@ -20,6 +20,7 @@ namespace WetPancakeCLI
static FSharpOption<TaskCreationOptions> _taskCreationOptions = FSharpOption<TaskCreationOptions>.None; static FSharpOption<TaskCreationOptions> _taskCreationOptions = FSharpOption<TaskCreationOptions>.None;
static FSharpOption<CancellationToken> _cancellationToken = FSharpOption<CancellationToken>.None; static FSharpOption<CancellationToken> _cancellationToken = FSharpOption<CancellationToken>.None;
#region Console Utilities
[ListCommand] [ListCommand]
[Description("Prints a test message to the console.")] [Description("Prints a test message to the console.")]
[Parameters("None")] [Parameters("None")]
@ -37,17 +38,74 @@ namespace WetPancakeCLI
[Parameters("None")] [Parameters("None")]
[Usage("> Exit")] [Usage("> Exit")]
public static void Exit() => Environment.Exit(ExitCode); public static void Exit() => Environment.Exit(ExitCode);
#endregion
#region Wet Pancake Utilities
[ListCommand] [ListCommand]
[Description("Generates random text, the number of sentences generated is randomly determined.")] [Description(
[Parameters("None")] "Checks to see if the string matches the desired sentence count and removes any over that limit.\n" +
[Usage("> GenerateRandomText")] "If the string has less sentences than the number requested, it will not change.\n" +
public static string GenerateRandomText() "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 try
{ {
if (sentences < 1)
throw new ArgumentException("Invalid argument. Must be greater than 0", "sentences");
return FSharpAsync.StartAsTask 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) catch (Exception e)
{ {
@ -55,28 +113,30 @@ namespace WetPancakeCLI
throw; throw;
} }
} }
#endregion
#region Text Generation
[ListCommand] [ListCommand]
[Description("Generates text using the gibberish-level and number of sentences specified by the user.\n" + [Description(
"This command does not run the result through the extra \"cleaning\" process like GenerateCleanText.\n" + "Generates text using the gibberish-level and number of sentences specified by the user.\n" +
"This means this command is faster but it might produce an extra sentence on the odd occasion.\n" + "The result goes through an extra \"cleaning\" process to remove any artefact sentences.\n" +
"Use this if you prefer speed over accuracy.\n" + "Use this if you cannot tolerate the odd extra sentence.\n" +
"Gibberish-level must be between 2 and 20." "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")] [Parameters("gibberish-level: int, sentences: int")]
[Usage("> GenerateText 5 10")] [Usage("> GenerateCleanText 5 10")]
public static string GenerateText(int gibberishLevel, int sentences) public static string GenerateCleanText(int gibberishLevel, int sentences)
{ {
try try
{ {
if (gibberishLevel < 2 || gibberishLevel > 20) if (gibberishLevel < 2 || gibberishLevel > 20)
throw new ArgumentException 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) if (sentences < 1)
throw new ArgumentException throw new ArgumentException
("Invalid argument. Must be greater than 0.", "sentences"); ("Invalid argument. Must be greater than 0.", "sentences");
return FSharpAsync.StartAsTask return FSharpAsync.StartAsTask
(RequestTextAsync(gibberishLevel, sentences), _taskCreationOptions, _cancellationToken).Result; (RequestCleanTextAsync(gibberishLevel, sentences), _taskCreationOptions, _cancellationToken).Result;
} }
catch (Exception e) catch (Exception e)
{ {
@ -86,28 +146,31 @@ namespace WetPancakeCLI
} }
[ListCommand] [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" + [Description(
"This command does not run the result through the extra \"cleaning\" process like GenerateCleanTextFromFile.\n" + "Loads the specified .txt file and generates text based on it," +
"This means this command is faster but it might produce an extra sentence on the odd occasion.\n" + "using the gibberish-level and number of sentences specified by the user.\n" +
"Use this if you prefer speed over accuracy.\n" + "The result goes through an extra \"cleaning\" process to remove any artefact sentences.\n" +
"Gibberish-level must be between 2 and 20.")] "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")] [Parameters("gibberish-level: int, sentences: int, file path: string")]
[Usage("> GenerateTextFromFile 3 6 C:/yourfile.txt")] [Usage("> GenerateCleanTextFromFile 3 6 C:/yourfile.txt")]
public static string GenerateTextFromFile(int gibberishLevel, int sentences, string filePath) public static string GenerateCleanTextFromFile(int gibberishLevel, int sentences, string filePath)
{ {
try try
{ {
if (gibberishLevel < 2 || gibberishLevel > 20) if (gibberishLevel < 2 || gibberishLevel > 20)
throw new ArgumentException 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) if (sentences < 1)
throw new ArgumentException throw new ArgumentException
("Invalid argument. Must be greater than 0.", "sentences"); ("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 throw new FileLoadException
("The file entered is not a plain text (.txt) file.", filePath); ("The file entered is not a plain text (.txt) file.", filePath);
return FSharpAsync.StartAsTask return FSharpAsync.StartAsTask
(RequestTextFromFileAsync(gibberishLevel, sentences, filePath), _taskCreationOptions, _cancellationToken).Result; (RequestCleanTextFromFileAsync(gibberishLevel, sentences, filePath), _taskCreationOptions, _cancellationToken).Result;
} }
catch (Exception e) catch (Exception e)
{ {
@ -117,20 +180,15 @@ namespace WetPancakeCLI
} }
[ListCommand] [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")] [Parameters("None")]
[Usage("> RequestAllTemplateFiles")] [Usage("> GenerateRandomText")]
public static string RequestAllTemplateFiles() public static string GenerateRandomText()
{ {
try try
{ {
WriteLine("Attempting to list out the available template files..."); return FSharpAsync.StartAsTask
var files = FSharpAsync.StartAsTask(RequestAllTemplateFilesAsync(), _taskCreationOptions, _cancellationToken).Result; (RequestRandomTextAsync(), _taskCreationOptions, _cancellationToken).Result;
foreach (var item in files)
{
WriteLine($"File path: {item}");
}
return "Listing complete.";
} }
catch (Exception e) catch (Exception e)
{ {
@ -140,25 +198,26 @@ namespace WetPancakeCLI
} }
[ListCommand] [ListCommand]
[Description("Generates text using the gibberish-level and number of sentences specified by the user.\n" + [Description(
"The result goes through an extra \"cleaning\" process to remove any artefact sentences.\n" + "Generates text using the gibberish-level and number of sentences specified by the user.\n" +
"Use this if you cannot tolerate the odd extra sentence.\n" + "This command does not run the result through the extra \"cleaning\" process like GenerateCleanText.\n" +
"With that said, it does mean it is slower than its GenerateText counterpart.\n" + "This means this command is faster but it might produce an extra sentence on the odd occasion.\n" +
"Gibberish-level must be between 2 and 20.")] "Use this if you prefer speed over accuracy.\n" +
"Gibberish-level must be between 2 and 20.")]
[Parameters("gibberish-level: int, sentences: int")] [Parameters("gibberish-level: int, sentences: int")]
[Usage("> GenerateCleanText 5 10")] [Usage("> GenerateText 5 10")]
public static string GenerateCleanText(int gibberishLevel, int sentences) public static string GenerateText(int gibberishLevel, int sentences)
{ {
try try
{ {
if (gibberishLevel < 2 || gibberishLevel > 20) if (gibberishLevel < 2 || gibberishLevel > 20)
throw new ArgumentException 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) if (sentences < 1)
throw new ArgumentException throw new ArgumentException
("Invalid argument. Must be greater than 0.", "sentences"); ("Invalid argument. Must be greater than 0.", "sentences");
return FSharpAsync.StartAsTask return FSharpAsync.StartAsTask
(RequestCleanTextAsync(gibberishLevel, sentences), _taskCreationOptions, _cancellationToken).Result; (RequestTextAsync(gibberishLevel, sentences), _taskCreationOptions, _cancellationToken).Result;
} }
catch (Exception e) catch (Exception e)
{ {
@ -168,50 +227,35 @@ namespace WetPancakeCLI
} }
[ListCommand] [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" + [Description(
"The result goes through an extra \"cleaning\" process to remove any artefact sentences.\n" + "Loads the specified .txt file and generates text based on it using the gibberish-level and number of sentencesspecifiedby the user.\n" +
"Use this if you cannot tolerate the odd extra sentence.\n" + "This command does not run the result through the extra \"cleaning\" process like GenerateCleanTextFromFile.\n" +
"With that said, it does mean it is slower than its GenerateTextFromFile counterpart.\n" + "This means this command is faster but it might produce an extra sentence on the odd occasion.\n" +
"Gibberish-level must be between 2 and 20.")] "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")] [Parameters("gibberish-level: int, sentences: int, file path: string")]
[Usage("> GenerateCleanTextFromFile 3 6 C:/yourfile.txt")] [Usage("> GenerateTextFromFile 3 6 C:/yourfile.txt")]
public static string GenerateCleanTextFromFile(int gibberishLevel, int sentences, string filePath) public static string GenerateTextFromFile(int gibberishLevel, int sentences, string filePath)
{ {
try try
{ {
if (gibberishLevel < 2 || gibberishLevel > 20) if (gibberishLevel < 2 || gibberishLevel > 20)
throw new ArgumentException 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) if (sentences < 1)
throw new ArgumentException throw new ArgumentException
("Invalid argument. Must be greater than 0.", "sentences"); ("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 throw new FileLoadException
("The file entered is not a plain text (.txt) file.", filePath); ("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 return FSharpAsync.StartAsTask
(RequestCleanTextFromFileAsync(gibberishLevel, sentences, filePath), _taskCreationOptions, _cancellationToken).Result; (RequestTextFromFileAsync(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;
} }
catch (Exception e) catch (Exception e)
{ {
@ -219,5 +263,6 @@ namespace WetPancakeCLI
throw; throw;
} }
} }
#endregion
} }
} }

Loading…
Cancel
Save