Skip to content

Cryptic error message with immutable option class #496

@tpaxatb

Description

@tpaxatb

If an immutable class is being used for the command line options, and the constructor does not contain a match for the property name, a cryptic exception is thrown rather than a useful error message stating this fact. It would be very nice if there was an indication that the property has no matching ctor parameter. It took forever to realize that I had changed the property name from InputFile to IndexFile without changing the ctor parameter (and it looks close enough that I missed it.

Sample options class:

   internal class CommandLineOptions
   {
      private const string DefaultInputFile = "";
      private const string DefaultOutputFile = "-";

      public CommandLineOptions(string inputFile = DefaultInputFile, string outputFile = DefaultOutputFile)
      {
         this.IndexFile = inputFile;
         this.OutputFile = outputFile;
      }

      [Option('i', "index-xml", Required = false, Default = DefaultInputFile, HelpText = "The index file to use. If not specified, intput is read from stdin.")]
      public string IndexFile { get; }

      [Value(0, Required = false, Default = DefaultOutputFile, HelpText = "The output file to use. If not specified the output is written to stdout.")]
      public string OutputFile { get; }
   }

Cryptic exception:

Unhandled Exception: System.InvalidOperationException: Sequence contains no matching element
   at System.Linq.Enumerable.First[TSource](IEnumerable`1 source, Func`2 predicate)
   at CommandLine.Core.InstanceBuilder.<>c__DisplayClass2_0`1.<BuildImmutable>b__5(<>f__AnonymousType0`2 <>h__TransparentIdentifier0, SpecificationProperty sp)
   at System.Linq.Enumerable.<SelectManyIterator>d__23`3.MoveNext()
   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at CommandLine.Core.InstanceBuilder.BuildImmutable[T](Type typeInfo, Maybe`1 factory, IEnumerable`1 specProps, IEnumerable`1 specPropsWithValue, List`1 setPropertyErrors)
   at CommandLine.Core.InstanceBuilder.<>c__DisplayClass0_0`1.<Build>b__5()
   at CommandLine.Core.InstanceBuilder.Build[T](Maybe`1 factory, Func`3 tokenizer, IEnumerable`1 arguments, StringComparer nameComparer, Boolean ignoreValueCase, CultureInfo parsingCulture, Boolean autoHelp, Boolean autoVersion, IEnumerable`1 nonFatalErrors)
   at CommandLine.Parser.ParseArguments[T](IEnumerable`1 args)
   at Converter.Program.TryGetCommandLineParameters(IEnumerable`1 args, CommandLineOptions& commandLineOptions) in Z:\Converter\Program.cs:line 66
   at Converter.Program.Main(String[] args) in Z:\Converter\Program.cs:line 17

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions