natemcmaster / CommandLineUtils

Command line parsing and utilities for .NET

Home Page:https://natemcmaster.github.io/CommandLineUtils/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to define behavior that is executed in case of a command-parsing error? [Question]

janca777 opened this issue · comments

On execution, my program takes two arguments it needs to run:

static Task<int> Main(string[] args)
{
   return CommandLineApplication.ExecuteAsync<MyUpdateService>(args);
}

[Argument(0, Description = "Filepath")]
     
private string Filepath { get; }

[Argument(1, Description = "UpdateMode")]

private int Mode { get; }

I want to show a messagebox in case the program is run with too few/ too many arguments, or the arguments don't match the required types (1: string, 2: int).

Since this cannot be achieved by simply wrapping a try/catch-block around it: Can this be done?

Did you try adding [Required] to both arguments?

[Required, Argument(0, Description = "Filepath")]
private string Filepath { get; }

If that still isn't giving you the error you want, try adding an "OnValidationError" method (example) or using new CommandLineApplication instead (example)

Thanks for your help.
Eventhough your suggested example seems like the way to go, I have no idea how to implement this in my code.

However, I was able to solve this myself (solution), hence this issue can be closed.