From bcf054d5cba533a19a90dc5217ad077243c3c952 Mon Sep 17 00:00:00 2001 From: Byron Watts Date: Mon, 26 Nov 2018 10:03:20 -0600 Subject: [PATCH 1/2] Added extra Boolean processing to handle :yes :y :true :t :no :n :false :f processing. --- Utility.CommandLine.Arguments/Arguments.cs | 26 +++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/Utility.CommandLine.Arguments/Arguments.cs b/Utility.CommandLine.Arguments/Arguments.cs index c4970b6..cfa037c 100644 --- a/Utility.CommandLine.Arguments/Arguments.cs +++ b/Utility.CommandLine.Arguments/Arguments.cs @@ -50,6 +50,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; +using System.Globalization; using System.Linq; using System.Reflection; using System.Text.RegularExpressions; @@ -452,7 +453,30 @@ private static object ChangeType(object value, string argument, Type toType) { try { - return Convert.ChangeType(value, toType); + if (toType == typeof(System.Boolean)) + { + string myValue = value.ToString(); + if ((myValue.Length == 0) + || (string.Equals(myValue, "yes", StringComparison.OrdinalIgnoreCase)) + || (string.Equals(myValue, "y", StringComparison.OrdinalIgnoreCase)) + || (string.Equals(myValue, "true", StringComparison.OrdinalIgnoreCase)) + || (string.Equals(myValue, "t", StringComparison.OrdinalIgnoreCase))) + { + return true; + } + else + if ((string.Equals(myValue, "no", StringComparison.OrdinalIgnoreCase)) + || (string.Equals(myValue, "n", StringComparison.OrdinalIgnoreCase)) + || (string.Equals(myValue, "false", StringComparison.OrdinalIgnoreCase)) + || (string.Equals(myValue, "f", StringComparison.OrdinalIgnoreCase))) + { + return false; + } + + Boolean.TryParse(myValue, out bool myVal); + return Convert.ToBoolean(value); + } + return Convert.ChangeType(value, toType, CultureInfo.InvariantCulture); } catch (Exception ex) { From 1582b55c96f17ae16b6ea38bdcd73ab671732e0e Mon Sep 17 00:00:00 2001 From: Byron Watts Date: Mon, 26 Nov 2018 10:49:11 -0600 Subject: [PATCH 2/2] Removed the test code that slipped in. --- Utility.CommandLine.Arguments/Arguments.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Utility.CommandLine.Arguments/Arguments.cs b/Utility.CommandLine.Arguments/Arguments.cs index cfa037c..39e02ae 100644 --- a/Utility.CommandLine.Arguments/Arguments.cs +++ b/Utility.CommandLine.Arguments/Arguments.cs @@ -473,7 +473,6 @@ private static object ChangeType(object value, string argument, Type toType) return false; } - Boolean.TryParse(myValue, out bool myVal); return Convert.ToBoolean(value); } return Convert.ChangeType(value, toType, CultureInfo.InvariantCulture);