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

Fix NullReferenceException when creating a default immutable instance #495

Merged

Conversation

0xced
Copy link
Contributor

@0xced 0xced commented Aug 7, 2019

No description provided.

@moh-hassan
Copy link
Collaborator

moh-hassan commented Aug 11, 2019

@0xced
Thanks for your effort.

Can your PR help in fixing the issue #499?

@0xced
Copy link
Contributor Author

0xced commented Nov 18, 2019

Ping.

@moh-hassan
Copy link
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
Copy link
Contributor Author

0xced commented Nov 26, 2019

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
Copy link
Collaborator

moh-hassan commented Nov 27, 2019

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