0

How to catch that there is an error from CommandLineParser so I can return my own error codes? I need to return my own codes when my console application is called from say SSIS.

class Program
{
    static void Main(string[] args)
    {
        try
        {
            var myParserResult = Parser.Default.ParseArguments<UploadFileCommand, DownloadFileCommand, CompressFileCommand>(args)
                .WithParsed<ICommand>(t => t.Execute());

            var parsed = myParserResult as NotParsed<object>;

         
             if (parsed != null && parsed.Errors.Any())
             {
                Console.WriteLine("Has Errors");
             }

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
        
    }

}
OutOFTouch
  • 1,017
  • 3
  • 13
  • 30
  • Please take the [tour] to learn how Stack Overflow works and read [ask] on how to improve the quality of your question. Then [edit] your question to include your source code as a working [mcve], which can be compiled and tested by others. Please show your attempts you have tried and the problems/error messages you get from your attempts. – Progman Dec 21 '21 at 23:02
  • Do you mean return your own error codes when an argument cannot be parsed? – Eric J. Dec 21 '21 at 23:06
  • @EricJ. Yes, I want to return my own error codes when there is an error parsing an argument. – OutOFTouch Dec 21 '21 at 23:44
  • 1
    @OutOFTouch What errors exactly are you talking about? The `ParseArguments()` method will return a `ParserResult` you can check. – Progman Dec 21 '21 at 23:56
  • @Progman How to get Errors from PaserResult? – OutOFTouch Dec 22 '21 at 02:03
  • @OutOFTouch Please [edit] your question to include a [mcve], which generates the error you get which you want to handle by your code. – Progman Dec 22 '21 at 10:52
  • @OutOFTouch The `ParserResult` class has an [`Errors`](https://github.com/commandlineparser/commandline/blob/d443a51aeb3a418425e970542b3b96e9da5f62e2/src/CommandLine/ParserResult.cs#L104) properties. – Progman Dec 22 '21 at 12:05
  • @Progman I am not trying to catch a specific error yet, I just want to be able to access the Errors property to see if it has errors and to return my own value. I don't see the Errors property of PaserResult as accessible. – OutOFTouch Dec 22 '21 at 16:28
  • 1
    @OutOFTouch In earlier versions the `Errors` property only existed in the `NotParsed` class, but it was moved up to the base class `ParserResult` in the commit https://github.com/commandlineparser/commandline/commit/ad50269e7dd4db96e53f615974f858827b70d6e7#diff-3ece11e4e88d3a71b4de90613da72c119c1b039dc67a0b911f8cc5f21c62ba0d – Progman Dec 22 '21 at 16:59

1 Answers1

0

The method ParseArguments() will return a ParserResult<T> object to indicate if the parsing was successful or not. In case the parsing was not successful a NotParsed<T> instance is returned. This class has an Errors property to contain all the Error instances why the parsing failed. You can use it in a normal foreach() loop to iterate over the found errors:

ParserResult<Options> result = parser.ParseArguments<Options>(args);
Console.WriteLine(result);
NotParsed<Options> notParsed = result as NotParsed<Options>;
if (notParsed != null)
{
    foreach (var error in notParsed.Errors)
    {
        Console.WriteLine(error);
    }
}

This might print the following debug output:

CommandLine.NotParsed`1[Testing.Options]
CommandLine.UnknownOptionError
CommandLine.UnknownOptionError
Progman
  • 16,827
  • 6
  • 33
  • 48
  • I Updated the code in my question based on the answer you gave. Does what I have work or is there a better way to do it? I couldn't use your code as is because I don't have an Options class defined, I use ICommand and define the options in the command classes that implement ICommand – OutOFTouch Dec 22 '21 at 18:22
  • I still have no idea why I can't catch the ArgumentNullException, no matter if I don't pass any verbs or if I pass verbs that don't exist or if I don't pass options, I never see this exception. – OutOFTouch Dec 22 '21 at 18:25
  • @OutOFTouch The `Options` class is just a test class in my code, you have to use your own option classes (most likely `UploadFileCommand`, `DownloadFileCommand`, `CompressFileCommand`). – Progman Dec 22 '21 at 18:56
  • @OutOFTouch There isn't any `ArgumentNullException` to catch. If you are getting an `ArgumentNullException` from somewhere you have to edit your question to include a [mcve], which generates the `ArgumentNullException` you want to catch. – Progman Dec 22 '21 at 18:57