Skip to content

Fix NullReferenceException when creating a default immutable instance#495

Merged
moh-hassan merged 2 commits into
commandlineparser:developfrom
0xced:Fix-NullReferenceException
Dec 26, 2019
Merged

Fix NullReferenceException when creating a default immutable instance#495
moh-hassan merged 2 commits into
commandlineparser:developfrom
0xced:Fix-NullReferenceException

Conversation

@0xced

@0xced 0xced commented Aug 7, 2019

Copy link
Copy Markdown
Contributor

No description provided.

@moh-hassan

moh-hassan commented Aug 11, 2019

Copy link
Copy Markdown
Collaborator

@0xced
Thanks for your effort.

Can your PR help in fixing the issue #499?

@0xced

0xced commented Nov 18, 2019

Copy link
Copy Markdown
Contributor Author

Ping.

@moh-hassan

Copy link
Copy Markdown
Collaborator

Thanks for your work.
The Parser already throw InvalidOperationException not NullReferenceException for default constructor.
What about if the Exception message is reworded and contain something like:

... the constructor should have parameters with the same names as Type properties and in the same order(inherited options are expected at the end of the constructor parameter list)..

Have a look to this discussion
This may help in resolving immutable types Ctor Exceptions.

@0xced

0xced commented Nov 26, 2019

Copy link
Copy Markdown
Contributor Author

The Parser already throw InvalidOperationException not NullReferenceException for default constructor.

Sorry, I should have properly explained the purpose of this pull request in the description; this is actually not always the case. When help is requested, the code takes another path and we get a NullReferenceException instead of InvalidOperationException. I have added OptionClass_IsImmutable_HasNoCtor_HelpRequested test to cover that specific issue.

I'll have a look at #188, but it looks orthogonal at first sight.

@moh-hassan

moh-hassan commented Nov 27, 2019

Copy link
Copy Markdown
Collaborator

When help is requested, the code takes another path

That is correct. But Parser isn't calling the InstanceBuilder.Build . It Fire the Help Screen.
InvokeBuild in Test call InstanceBuilder.Build .

The Parser still in help request fire InvalidOperationException.

The attached file is a demo project to demonestrate help request with the Immutable class.
ConsoleApp1.zip
run:

    consoleapp1 --help

Edit:
In case of using InvokeBuild, autohelp parameter should be false.

The next Test case Pass successfully.

 [Fact]
        public void Parse_Immutable_with_default_ctor_and_help__request_throw_InvalidOperationException()
        {

            Action act = () => InvokeBuild<ValueWithNoSetterOptions>(new string[] { "--help" }, false);
            act.Should().Throw<InvalidOperationException>();
        }

@moh-hassan moh-hassan changed the base branch from master to develop December 26, 2019 04:40
@moh-hassan moh-hassan merged commit 2063ffc into commandlineparser:develop Dec 26, 2019
@0xced 0xced deleted the Fix-NullReferenceException branch February 17, 2020 08:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants