-
Notifications
You must be signed in to change notification settings - Fork 0
CommandApplication
Lunar Doggo edited this page Apr 10, 2022
·
2 revisions
An CommandApplication provides a more abstracted way to deal with command line parameters by handling the parsing of the parameters by utilizing attributes. If you use a class derived from CommandApplication, you just have to provide all possible commands your application supports. The input will be processed in the background and will call either the PrintHelpPage
or Run
method depending if the user selected a HelpOption
or not.
The Run
method will automatically instantiate and execute the command associated with the StartOptionGroup provided by the user/cli parameters.
public abstract class CommandApplication
Default constructor that has to be overridden
Sitnature | Description |
---|---|
public virtual void Run(string[]) |
Interface between the calling method and the console parameter parsing |
protected virtual void PrintHelpPage(ParsedStartOptions) |
Called when a HelpOption is selected by the user, processes all selected StartOptions and calls protected abstract void PrintHelpPage
|
protected abstract void PrintHelpPage(StartOptionParserSettings, IEnumerable<HelpOption>, IEnumerable<StartOptionGroup>, IEnumerable<StartOption>) |
Intended for printing a help page for the provided parameters, called by protected virtual void PrintHelpPage
|
protected abstract void Run(StartOptionGroup, IEnumerable<StartOption>) |
Called by public virtual void Run if no HelpOption s are selected by the user. Intended to choose an execution path depending on the provided StartOptionGroup and groupless StartOption s |
protected abstract ApplicationStartOptions GetApplicationStartOptions() |
Called by the constructor, intended to return an instance of AbstractStartOptions which contains all StartOptionGroup s and groupless StartOption s the application should have. Optionally custom HelpOption s and StartOptionParserSettings can be provided in such instance |
protected virtual StartOptionParserSettings GetParserSettings() |
Provides the StartOptionParserSettings used by the StartOptionParser
|
protected virtual IEnumerable<HelpOption> GetHelpOptions() |
Provides the available HelpOption s for you application |
protected abstract Type[] GetCommandTypes() |
Provides all command types your application will be able to utilize |
class Program
{
static void Main(string[] args)
{
DemoApplication application = new DemoApplication();
application.Run(args);
Console.WriteLine("Execution finished");
}
}
class DemoApplication : CommandApplication
{
private readonly IHelpPagePrinter helpPrinter = new ConsoleHelpPrinter('\t');
protected override void PrintHelpPage(StartOptionParserSettings settings, IEnumerable<HelpOption> helpOptions, IEnumerable<StartOptionGroup> groups, IEnumerable<StartOption> grouplessOptions)
{
this.helpPrinter.Print(settings, helpOptions, groups, grouplessOptions);
}
protected override IEnumerable<HelpOption> GetHelpOptions()
{
return new HelpOption[]
{
new HelpOption("help", false),
new HelpOption("h", true)
};
}
protected override StartOptionParserSettings GetParserSettings()
{
// Require at least one StartOptionGroup to be provided in the cli arguments
// Use "/" as prefix for long option names like "/add" or "/help"
// Use "-" as prefix for short option names like "-a" or "-h"
// Separate values from the corresponding start option with a space
// Examples start options:
// /> .\StartOptions.Demo.exe /add -1 3 /value-2 5
// /> .\StartOptions.Demo.exe -h
return new StartOptionParserSettings()
{
RequireStartOptionGroup = true,
ShortOptionNamePrefix = "-",
LongOptionNamePrefix = "/",
OptionValueSeparator = ' ',
};
}
protected override Type[] GetCommandTypes()
{
return new[] { typeof(AddCommand), typeof(ReadFileCommand) };
}
}