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

Parser.ParseArguments(factory, args) requires parameterless constructor #70

Closed
ericnewton76 opened this issue Nov 4, 2017 · 4 comments

Comments

@ericnewton76
Copy link
Member

Issue by Virtlink
Sunday Apr 24, 2016 at 11:12 GMT
Originally opened as gsscoder/commandline#310


The Parser.ParseArguments() overloads with a factory function has this signature:

public ParserResult<T> ParseArguments<T>(Func<T> factory, IEnumerable<string> args)
    where T : new()

If I provide my own factory function, why does T need to have a public parameterless constructor (new())?

var result = parser.ParseArguments<Options>(() => new Options(true), args);

And on the other hand, why doesn't the non-factory overload of ParseArguments not require a public parameterless constructor? This will cause a runtime-exception when T doesn't have a public parameterless constructor.

var result = parser.ParseArguments<Options>(args);
@ericnewton76
Copy link
Member Author

Comment by nemec
Monday Apr 25, 2016 at 01:38 GMT


I can't speak to the reasoning behind the decision, since I didn't write the code, but they both look like oversights to me. If you want to submit a pull request, I'll merge it.

@ericnewton76
Copy link
Member Author

Comment by Virtlink
Monday Apr 25, 2016 at 10:56 GMT


I can fix the first by removing the where T : new(), but I'm not sure about the semantics of the second as adding where T : new() causes tests to fail.

@ericnewton76
Copy link
Member Author

Comment by JeanSebTr
Wednesday Apr 27, 2016 at 18:43 GMT


I had the same problem and another fix was necessary (https://github.com/Emergensys/commandline/commit/813766d255b6ad75031655482b8e117fdaec0dc7) to use a factory for a type without parameter less constructor.

I'll check to clean that up and make a pull request (I've also added factory support to multi verb https://github.com/Emergensys/commandline/commit/2ebe156199f8bad3c0155c8c415e3363949941ff), but I'm a bit lost in the functional style.

@ericnewton76
Copy link
Member Author

Comment by CreepyGnome
Wednesday Jun 15, 2016 at 16:50 GMT


Doing some digging into the code and unit tests it looks like it is safe to remove the where statement from the ParseArguments that takes a factory Func and it should not be added to the ParseArguments that takes only the args. Basically all the ParseArguments should not have a where statement on them.

Explanation of why I think this:
It will only use the default constructor if the provided type is mutable, and if it is immutable it will use Reflection to figure out the correct constructor to use to create the type. The convention it seems to be expecting is that if the type is mutable then it must have a default constructor, and if it is not expects a constructor that takes the options/args needed to create the type. If you do not follow the expected convention you will get a runtime error, which I think is fine and expected when coding by convention.

If anyone disagrees and thinks it is not safe to remove the one where statement mentioned above let me know, otherwise I will submit a pull request for this issue.

pergardebrink added a commit to pergardebrink/commandline that referenced this issue Mar 8, 2020
pergardebrink added a commit to pergardebrink/commandline that referenced this issue Mar 9, 2020
pergardebrink added a commit to pergardebrink/commandline that referenced this issue Mar 9, 2020
moh-hassan pushed a commit that referenced this issue Mar 9, 2020
* Remove constraint on T for ParseArguments with factory

* Make it possible to use factory for classes without public empty constructor

* Allow types with explicitly defined interface implementations

* Add test for #70

* Make sure explicit interface implementations can be parsed

* Add test to make sure parser can detect explicit interface implementations
moh-hassan pushed a commit that referenced this issue Mar 11, 2020
* Remove constraint on T for ParseArguments with factory

* Make it possible to use factory for classes without public empty constructor

* Allow types with explicitly defined interface implementations

* Add test for #70

* Make sure explicit interface implementations can be parsed

* Add test to make sure parser can detect explicit interface implementations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants