Skip to content

AbstractApplication

Lunar Doggo edited this page Jul 24, 2021 · 4 revisions

What is it?

An AbstractApplication provides a more abstracted way to deal with command line parameters by handling the parsing of the parameters. If you use a class derived from AbstractApplication, you just have to provide all possible StartOptionGroups and StartOptions. 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.

If the Run method is executed, you will still have to implement the different execution paths depending on the selected StartOptionGroup and groupless StartOptions yourself.

Implementation

public class AbstractApplication

Constructor

Default constructor that has to be overridden

Methods

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 HelpOptions are selected by the user. Intended to choose an execution path depending on the provided StartOptionGroup and groupless StartOptions
protected abstract ApplicationStartOptions GetApplicationStartOptions() Called by the constructor, intended to return an instance of AbstractStartOptions which contains all StartOptionGroups and groupless StartOptions the application should have. Optionally custom HelpOptions and StartOptionParserSettings can be provided in such instance

Example

class Program
{
    static void Main(string[] args)
    {
        DemoApplication application = new DemoApplication();
        application.Run(args);
        Console.WriteLine("Execution finished");
    }
}

class DemoApplication : AbstractApplication
{
    protected override void PrintHelpPage(StartOptionParserSettings settings, IEnumerable<HelpOption> helpOptions,
                                          IEnumerable<StartOptionGroup> groups, IEnumerable<StartOption> grouplessOptions)
    {
        // If the user starts your application with a help-StartOption, this method will be executed, instead of Run()
        // Here you can call a isntance of the IHelpPagePrinter-Interface to print a help page for the user
        // This library offers the ConsoleHelpPrinter class if you don't want to implement this logic for yourself
    }

    protected override void Run(StartOptionGroup selectedGroup, IEnumerable<StartOption> selectedGrouplessOptions)
    {
        // This method will be executed, if the user starts your application with any StartOptions except the help-StartOptions, here
        // you will have to implement execution paths for all possible StartOptionGroups and handle the selected groupless StartOptions
    }

    protected override ApplicationStartOptions GetApplicationStartOptions()
    {
        // In this function you must return an instance of ApplicationStartOptions, which must consist of at least
        // all possible StartOptionGroups and groupless StartOptions and can optionally include custom HelpOptions
        // and custom StartOptionParserSettings depending on the constructor of ApplicationStartOptions you use
    }
}