- Hands-On Machine Learning with ML.NET
- Jarred Capellman
- 170字
- 2021-06-24 16:43:30
The Program class
Those of you who have created console applications should be familiar with the Program class and the Main method within. We will follow this structure for other console-based applications throughout the remainder of the book. The following code block contains the program class from which the application will begin execution:
using System;
using chapter02.ML;
namespace chapter02
{
class Program
{
static void Main(string[] args)
{
if (args.Length != 2)
{
Console.WriteLine($"Invalid arguments passed in, exiting.{Environment.NewLine}" + $"{Environment.NewLine}Usage: {Environment.NewLine}" +
$"predict <sentence of text to predict against>{Environment.NewLine}" +
$"or {Environment.NewLine}" +
$"train <path to training data file>{Environment.NewLine}");
return;
}
switch (args[0])
{
case "predict":
new Predictor().Predict(args[1]);
break;
case "train":
new Trainer().Train(args[1]);
break;
default:
Console.WriteLine($"{args[0]} is an invalid option");
break;
}
}
}
}
This constitutes a fairly straightforward method implementation for those familiar with parsing command-line arguments. A simple two-argument approach is used as the help text indicates.
When executing a more complex command-line application that takes in several arguments (optional and required), Microsoft has provided a simple-to-use NuGet package, which is available here: https://github.com/dotnet/command-line-api