2

I am reading the CommandLineParser documentation and I am very interested in the way functionality is designed here:

CommandLine for verbs

I basically have a command line application where I need to set different parameters. Something like below

MyApp.exe -a val1 -b val2 -c -d

What I am trying to achieve is that if -c is present in the command line application, I want to take the result of what -a val1 -b val2 produced and then call my next function with that result and the operation that -c does.

In this case:

int Main(string[] args) {
  return CommandLine.Parser.Default.ParseArguments<AddOptions, CommitOptions, CloneOptions>(args)
    .MapResult(
      (AddOptions opts) => RunAddAndReturnExitCode(opts),
      (CommitOptions opts) => RunCommitAndReturnExitCode(opts), 
      errs => 1);
}

How can I get the result of RunAddAndReturnExitCode(opts) and add it into RunCommitAndReturnExitCode?

I really like how the verbs are designed since it keeps the code nice and clean.

user3587624
  • 1,427
  • 5
  • 29
  • 60

1 Answers1

0

You don't need the anonymous delegate if you are using named static methods.

static int Main(string[] args)
{
    return 
        Parser.Default.ParseArguments<SubmitProcessOptions, CancelProcessOptions, GetProcessOptions>(args)
            .MapResult<SubmitProcessOptions, CancelProcessOptions, GetProcessOptions, int>(
                RunSubmitProcess,
                RunCancelProcess,
                RunGetProcess,
                HandleParseError);
}

static int RunSubmitProcess(SubmitProcessOptions submitProcessOptions)
{
    Console.WriteLine($"ProcessKey: {submitProcessOptions.ProcessKey}");
    foreach (var parameter in submitProcessOptions.Parameters)
    {
        Console.WriteLine($"Parameter: {parameter}");
    }
    return 0;
}

static int RunCancelProcess(CancelProcessOptions cancelProcessOptions)
{
    Console.WriteLine($"ProcessKey: {cancelProcessOptions.ProcessKey}");
    return 0;
}

static int RunGetProcess(GetProcessOptions getProcessOptions)
{
    Console.WriteLine($"ProcessKey: {getProcessOptions.ProcessKey}");
    return 0;
}

static int HandleParseError(IEnumerable<Error> errs)
{
    //handle errors
    return 1;
}        
CarenRose
  • 1,266
  • 1
  • 12
  • 24
stlawrence
  • 169
  • 1
  • 2
  • 11