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

Reduce boilerplate needed for IValueParser implementations #169

Merged
merged 11 commits into from
Oct 26, 2018

Conversation

atifaziz
Copy link
Contributor

This PR adds ValueParser.Create that is designed to reduce the amount of ceremony needed for IValueParser and IValueParser<> implementations. ValueParser.Create and its overloads return a standard implementation of IValueParser and IValueParser<> that delegate to a single parsing function.

As a testimony of the code reduction and increased re-use, all of the following:

  • BooleanValueParser
  • ByteValueParser
  • DoubleValueParser
  • FloatValueParser
  • Int16ValueParser
  • Int32ValueParser
  • Int64ValueParser
  • StringValueParser
  • UInt16ValueParser
  • UInt32ValueParser
  • UInt64ValueParser
  • UriValueParser

have been replaced with singletons in fields of StockValueParsers, as in:

--- a/src/CommandLineUtils/Abstractions/ValueParserProvider.cs
+++ b/src/CommandLineUtils/Abstractions/ValueParserProvider.cs
@@ -21,18 +21,18 @@ internal ValueParserProvider()
             AddRange(
                 new IValueParser[]
                 {
-                    StringValueParser.Singleton,
-                    BooleanValueParser.Singleton,
-                    ByteValueParser.Singleton,
-                    Int16ValueParser.Singleton,
-                    Int32ValueParser.Singleton,
-                    Int64ValueParser.Singleton,
-                    UInt16ValueParser.Singleton,
-                    UInt32ValueParser.Singleton,
-                    UInt64ValueParser.Singleton,
-                    FloatValueParser.Singleton,
-                    DoubleValueParser.Singleton,
-                    UriValueParser.Singleton,
+                    StockValueParsers.String,
+                    StockValueParsers.Boolean,
+                    StockValueParsers.Byte,
+                    StockValueParsers.Int16,
+                    StockValueParsers.Int32,
+                    StockValueParsers.Int64,
+                    StockValueParsers.UInt16,
+                    StockValueParsers.UInt32,
+                    StockValueParsers.UInt64,
+                    StockValueParsers.Float,
+                    StockValueParsers.Double,
+                    StockValueParsers.Uri,
                 });
         }

Within StockValueParsers, there's even greater consolidation of all duplication amongst categories (for integers, non-negative integers & floating-point) of value parsers.

All unit tests are passing.

Forgive me for submitting a PR before opening an issue but I thought it'd be okay per the following point in the contribution guidelines:

  1. Before opening a PR with a significant change, open an issue so we can discuss design issues. Significant = behavior or API change

This PR does not change behavior. It doesn't change any existing API. It does however add one new method (that is ValueParser.Create plus overloads) to help users of this library to implement new value parsers in their CLI applications without the ceremony of implementing the IValueParser/IValueParser<T> interface.

Copy link
Owner

@natemcmaster natemcmaster left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making this PR. I'd be happy to get this merged. I have a few comments I'd like to discuss first, but nothing too major. We should be able to get this in soon.

Also, it looks like some of the unit tests are failing. Can you take a look?

@atifaziz
Copy link
Contributor Author

Also, it looks like some of the unit tests are failing. Can you take a look?

Strange. Passed on mine before I opened the PR but now I see it's failing. Bizarre. Will have a look for and submit the fix.

@atifaziz
Copy link
Contributor Author

atifaziz commented Oct 26, 2018

Also, it looks like some of the unit tests are failing. Can you take a look?

I've addressed this in aec7e57 but looks like build is now failing due to the unrelated commit 4128af0 in master.

On my box, the tests are now all passing:

$ dotnet test ./test/CommandLineUtils.Tests
Build started, please wait...
Build completed.

Test run for A:\CommandLineUtils\.build\bin\McMaster.Extensions.CommandLineUtils.Tests\Debug\netcoreapp2.1\McMaster.Extensions.CommandLineUtils.Tests.dll(.NETCoreApp,Version=v2.1)
Microsoft (R) Test Execution Command Line Tool Version 15.8.0
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...
Specify --help for a list of available options and commands.

Total tests: 675. Passed: 675. Failed: 0. Skipped: 0.
Test Run Successful.
Test execution time: 2.4332 Seconds
Test run for A:\CommandLineUtils\.build\bin\McMaster.Extensions.CommandLineUtils.Tests\Debug\net461\McMaster.Extensions.CommandLineUtils.Tests.dll(.NETFramework,Version=v4.6.1)
Microsoft (R) Test Execution Command Line Tool Version 15.8.0
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...
Specify --help for a list of available options and commands.

Total tests: 674. Passed: 674. Failed: 0. Skipped: 0.
Test Run Successful.
Test execution time: 2.6901 Seconds

@natemcmaster natemcmaster merged commit 5e246ca into natemcmaster:master Oct 26, 2018
@atifaziz atifaziz deleted the stock-value-parsers branch October 26, 2018 14:45
@natemcmaster natemcmaster added this to the 2.3.0 milestone Oct 26, 2018
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

Successfully merging this pull request may close these issues.

2 participants