lundi 13 juin 2022

choosing correct handler based on CLI options

I am currently building a simple command-line application in C#. To help me with parsing and other under-the-hood stuff I use .NET System.CommandLine library. The approach for implementing options and setting handlers is identical to what Microsoft uses in their docs for the library. The tool is meant to be used as follows:

program -a aArgument -b -c cArgument
program -b -c cArgument

-a, -b and -c are all CLI options. Then, I implemented a handler method, which executes the correct set of methods depending on which options are used.

internal static void HandleOptions(string aOpt, string bOpt, string cOpt){
    if (aOpt != null && bOpt != null && cOpt != null)
    {
        method1();
        method2();
    }
    else if (aOpt == null && bOpt != null && cOpt != null) 
    {
        method3();
    }
    else if (aOpt != null && bOpt == null && cOpt != null)
    {
        method4();
    }
...
}

However, this approach is not extensible in the long run, since as I continue adding more options, the if-else statements get complicated. What would be a good design pattern to solve this issue?

Aucun commentaire:

Enregistrer un commentaire