commandlineparser / commandlineparser/commandline
"InvalidOperationException: appears to be Immutable with invalid constructor" when suppling invalid (throwable) args
- Dominant language
- C#
- Stars
- 4.8k
- Forks
- 478
- PR merge metrics
- No merged PRs in 30d
Description
# Description
When calling constructor with single string, if the constructor throws - the result of parsing is also throwing (misleading) exception instead of returning error lambda.
# Minimal reproducable code:
````csharp
using System;
using System.Globalization;
using CommandLine;
class Program
{
[Verb("test")]
public class Options
{
private CultureInfo _localization;
[Option('l', "lang", Required = true, Default = "en")]
public CultureInfo Localization => _localization;
public Options(CultureInfo localization)
{
_localization = localization;
}
}
static void Main(string[] args)
{
var types = new Type[] { typeof(Options) };
var result = Parser.Default.ParseArguments(args, types);
result
.WithParsed(obj => {})
.WithNotParsed(errs => {});
}
}
````
Call with: `test -l "FooBar"`
# Expected behaviour
The `WithNotParsed` lambda should be called with error message pointing to invalid argument
# Actual behaviour
The line with `var result = Parser.Default.ParseArguments(args, types);` throws:
`System.InvalidOperationException: 'Type Program+Options appears to be Immutable with invalid constructor. Check that constructor arguments have the same name and order of their underlying Type. Constructor Parameters can be ordered as: '(localization)''`
# Additional info:
When called with `test -l "en-US"` the code will correctly enter `WithParsed` lambda.
# Version data:
- Tested on Windows / .NET Framework 4.7.2
- Tested on version 2.8.0 from NuGet of CommandLineParser
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with the minimal reproducible C# program and the Parser.Default.ParseArguments call using test -l "FooBar"; compare it with the working "en-US" case. The fix is done when an invalid constructor argument produces an error passed to WithNotParsed instead of throwing InvalidOperationException.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- cli
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100