Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Removing main side effects from public interface (Parser type) #59
As you seen with latest release a lot was done to simplify the library.
Don't get me wrong, I'll not change basic concepts of the API; but I'd like to embrace good design from functional world for make our library strongest.
When you declare the options type:
var options = new Options();
var result = new Parser().ParseArguments(args, options);
So we can say that
Incredibly the solution is quite easy:
T Parser.ParseArguments<T>(string args, Action onFail)
The method with this new signature constructs the instance and returns it.
But if parsing fails an appropriate delegate (
Other advantages: convergence of standard methods with strict methods (@nemec suggested feature).
Functional paradigms proves their effectiveness from '70 (and before), please dig into the argument if not convinced. :))
As always, opinions (or flames) are welcome.
I'm doing some experiment on anther branch, for now it seems promising.
Here the branch (uncomplete), https://github.com/gsscoder/commandline/tree/issue59-sideeff
Anyway the latest stable is tagged and well defined, https://github.com/gsscoder/commandline/tree/v126.96.36.199
Thanks for the comments!
PS: I've explained in detail all these things for future reference / other users, I know you're more than familiar with what I quoted.
added a commit
Feb 28, 2013
Work is moved to new official beta https://github.com/gsscoder/commandline/tree/develop-1.9.8-beta branch.
Some was done, other things must be done, but I think that this could be a nice starting point.
The issue could be considered close (also if other msg maybe appended here).
When some internal refactoring goes to end -> it will be merged to master.
A nice day to everyone! :))
I want to add. The public API of
T ParseArguments<T>(string args, Action onFail) where T : new() T ParseArguments<T>(string args, Action<string, object> onVerbCommand, Action onFail) where T : new()
A lot cleaner now... or not?
We can do as most IoC does, providing an overload like
T ParseArguments<T>(Func<T> builder, string args, Action onFail)
var parser = new Parser(); var options = parser.ParseArguments<AbstractOptions>(() => new ConcreteOptions(), args, () => Environment.Exit(-1)); // we are here, options can't be null in any case! :)
What do you think about?
It avoid side effects on options instance and let devs create it...
That would work. Of course it's easy to introduce side effects, but that's the implementer's problem rather than yours ;)
@nemec, great! you've read my mind... This is exactly what I've thought about it.
Talking of side side effects (or intended effects as printing help) I think they are not an absolute evil.
The same F#
I'm not try to change this library to a purely functional piece a software (inside/out), but a stated in the post I'd like to remove all not benign side effects from public surface of the API.
Always toward usability and a not-verbose-syntax compromise.
PS: is also true, if you look to the new branch, that at a lot is done internally / and other can and will be done.