Browse Source

Added GenerateTextFromTemplate command-method.

master
Craig Oates 6 years ago
parent
commit
d7bf9db792
  1. 12
      WetPancake/ProductServices.fs
  2. 42
      WetPancakeCLI/ConsoleCommands.cs

12
WetPancake/ProductServices.fs

@ -56,14 +56,4 @@ module Pancake =
// Add Checks to make sure the client is passing in a .txt file?
// Leave for the client to check?
// Give the client a function to check (and throw) if not handed a .txt file?
(* Should the library provide list of the available default txt files?
By doing so, the user can specify a particular file from the list available.
Is this overkill?
Command-method Example
======================
public static string RetrieveAllTxtFiles()...
public static string GenerateTextFromDefault(gibberishLevel, sentences, defaultFileName)... *)
// Give the client a function to check (and throw) if not handed a .txt file?

42
WetPancakeCLI/ConsoleCommands.cs

@ -59,9 +59,12 @@ namespace WetPancakeCLI
[Usage("> GenerateText 5 10")]
public static string GenerateText(int gibberishLevel, int sentences)
{
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; }
try
{
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");
return FSharpAsync.StartAsTask(RequestTextAsync(gibberishLevel, sentences), _taskCreationOptions, _cancellationToken).Result;
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
@ -76,12 +79,14 @@ namespace WetPancakeCLI
[Usage("> GenerateTextFromFile 3 6 C:/yourfile.txt")]
public static string GenerateTextFromFile(int gibberishLevel, int sentences, string filePath)
{
// Should these checks be moved into the main library? How are people going to now to add these themselves when they are new to the project?
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; }
{
// Should these checks be moved into the main library? How are people going to now to add these themselves when they are new to the project?
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);
return FSharpAsync.StartAsTask(RequestTextFromFileAsync(gibberishLevel, sentences, filePath), _taskCreationOptions, _cancellationToken).Result;
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
@ -112,5 +117,26 @@ namespace WetPancakeCLI
throw;
}
}
[ListCommand]
[Description("Generates text using the gibberish level (between 2 and 20) and number of sentences specified by the user." +
"You can, also, specify which template file you want to use. " +
"If you are unsure what options are available, use the RequestAllTemplateFiles command-method to generate a list of them.")]
[Parameters("gibberish level: int, sentences: int, file path: string")]
[Usage("> GenerateTextFromTemplate 5 10 D:\\WetPancake\\WetPancake\\TextFiles\\console-waterworks-announcement.txt")]
public static string GenerateTextFromTemplate(int gibberishLevel, int sentences, string filePath)
{
try
{
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");
return FSharpAsync.StartAsTask(RequestTextFromTemplateAsync(gibberishLevel, sentences, filePath), _taskCreationOptions, _cancellationToken).Result;
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
throw;
}
}
}
}

Loading…
Cancel
Save