Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cryptic error message with immutable option class #496

Closed
tpaxatb opened this issue Aug 8, 2019 · 0 comments
Closed

Cryptic error message with immutable option class #496

tpaxatb opened this issue Aug 8, 2019 · 0 comments

Comments

@tpaxatb
Copy link

tpaxatb commented Aug 8, 2019

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants