diff --git a/Core/Other/Settings/Dialogs/DialogSettingsCefArgs.cs b/Core/Other/Settings/Dialogs/DialogSettingsCefArgs.cs index fb812b6c..dbf143ae 100644 --- a/Core/Other/Settings/Dialogs/DialogSettingsCefArgs.cs +++ b/Core/Other/Settings/Dialogs/DialogSettingsCefArgs.cs @@ -2,6 +2,7 @@ using System.Windows.Forms; using TweetDuck.Core.Controls; using TweetDuck.Core.Utils; +using TweetDuck.Data; namespace TweetDuck.Core.Other.Settings.Dialogs{ sealed partial class DialogSettingsCefArgs : Form{ @@ -30,7 +31,7 @@ private void btnApply_Click(object sender, EventArgs e){ return; } - int count = CommandLineArgsParser.ReadCefArguments(CefArgs).Count; + int count = CommandLineArgs.ReadCefArguments(CefArgs).Count; string prompt = count == 0 && !string.IsNullOrWhiteSpace(prevArgs) ? "All current arguments will be removed. Continue?" : count+(count == 1 ? " argument was" : " arguments were")+" detected. Continue?"; if (FormMessage.Question("Confirm CEF Arguments", prompt, FormMessage.OK, FormMessage.Cancel)){ diff --git a/Core/Utils/CommandLineArgsParser.cs b/Core/Utils/CommandLineArgsParser.cs deleted file mode 100644 index 538b8a73..00000000 --- a/Core/Utils/CommandLineArgsParser.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System; -using System.Text.RegularExpressions; -using TweetDuck.Data; - -namespace TweetDuck.Core.Utils{ - static class CommandLineArgsParser{ - private static readonly Lazy<Regex> SplitRegex = new Lazy<Regex>(() => new Regex(@"([^=\s]+(?:=(?:\S*""[^""]*?""\S*|\S*))?)", RegexOptions.Compiled), false); - - public static CommandLineArgs ReadCefArguments(string argumentString){ - CommandLineArgs args = new CommandLineArgs(); - - if (string.IsNullOrWhiteSpace(argumentString)){ - return args; - } - - foreach(Match match in SplitRegex.Value.Matches(argumentString)){ - string matchValue = match.Value; - - int indexEquals = matchValue.IndexOf('='); - string key, value; - - if (indexEquals == -1){ - key = matchValue.TrimStart('-'); - value = "1"; - } - else{ - key = matchValue.Substring(0, indexEquals).TrimStart('-'); - value = matchValue.Substring(indexEquals+1).Trim('"'); - } - - if (key.Length != 0){ - args.SetValue(key, value); - } - } - - return args; - } - } -} diff --git a/Data/CommandLineArgs.cs b/Data/CommandLineArgs.cs index 0caa437f..39b90f30 100644 --- a/Data/CommandLineArgs.cs +++ b/Data/CommandLineArgs.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Text; +using System.Text.RegularExpressions; namespace TweetDuck.Data{ sealed class CommandLineArgs{ @@ -32,6 +33,36 @@ public static void ReadStringArray(char entryChar, string[] array, CommandLineAr } } + public static CommandLineArgs ReadCefArguments(string argumentString){ + CommandLineArgs args = new CommandLineArgs(); + + if (string.IsNullOrWhiteSpace(argumentString)){ + return args; + } + + foreach(Match match in Regex.Matches(argumentString, @"([^=\s]+(?:=(?:\S*""[^""]*?""\S*|\S*))?)")){ + string matchValue = match.Value; + + int indexEquals = matchValue.IndexOf('='); + string key, value; + + if (indexEquals == -1){ + key = matchValue.TrimStart('-'); + value = "1"; + } + else{ + key = matchValue.Substring(0, indexEquals).TrimStart('-'); + value = matchValue.Substring(indexEquals+1).Trim('"'); + } + + if (key.Length != 0){ + args.SetValue(key, value); + } + } + + return args; + } + private readonly HashSet<string> flags = new HashSet<string>(); private readonly Dictionary<string, string> values = new Dictionary<string, string>(); diff --git a/Program.cs b/Program.cs index 3c012d85..cb34ef61 100644 --- a/Program.cs +++ b/Program.cs @@ -140,7 +140,7 @@ private static void Main(){ #endif }; - CommandLineArgsParser.ReadCefArguments(UserConfig.CustomCefArgs).ToDictionary(settings.CefCommandLineArgs); + CommandLineArgs.ReadCefArguments(UserConfig.CustomCefArgs).ToDictionary(settings.CefCommandLineArgs); BrowserUtils.SetupCefArgs(settings.CefCommandLineArgs); Cef.EnableHighDPISupport(); diff --git a/TweetDuck.csproj b/TweetDuck.csproj index 2eac9d60..1f707782 100644 --- a/TweetDuck.csproj +++ b/TweetDuck.csproj @@ -201,7 +201,6 @@ </Compile> <Compile Include="Core\Bridge\CallbackBridge.cs" /> <Compile Include="Data\CommandLineArgs.cs" /> - <Compile Include="Core\Utils\CommandLineArgsParser.cs" /> <Compile Include="Core\Notification\Screenshot\FormNotificationScreenshotable.cs"> <SubType>Form</SubType> </Compile> diff --git a/tests/Core/TestCommandLineArgsParser.cs b/tests/Core/TestCommandLineArgsParser.cs deleted file mode 100644 index ef7cf88a..00000000 --- a/tests/Core/TestCommandLineArgsParser.cs +++ /dev/null @@ -1,33 +0,0 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using TweetDuck.Core.Utils; -using TweetDuck.Data; - -namespace UnitTests.Core{ - [TestClass] - public class TestCommandLineArgsParser{ - [TestMethod] - public void TestEmptyString(){ - Assert.AreEqual(0, CommandLineArgsParser.ReadCefArguments("").Count); - Assert.AreEqual(0, CommandLineArgsParser.ReadCefArguments(" ").Count); - } - - [TestMethod] - public void TestValidString(){ - CommandLineArgs args = CommandLineArgsParser.ReadCefArguments("--aaa --bbb --first-value=123 --SECOND-VALUE=\"a b c d e\"\r\n--ccc"); - // cef has no flags, flag arguments have a value of 1 - // the processing removes all dashes in front of each key - - Assert.AreEqual(5, args.Count); - Assert.IsTrue(args.HasValue("aaa")); - Assert.IsTrue(args.HasValue("bbb")); - Assert.IsTrue(args.HasValue("ccc")); - Assert.IsTrue(args.HasValue("first-value")); - Assert.IsTrue(args.HasValue("second-value")); - Assert.AreEqual("1", args.GetValue("aaa", string.Empty)); - Assert.AreEqual("1", args.GetValue("bbb", string.Empty)); - Assert.AreEqual("1", args.GetValue("ccc", string.Empty)); - Assert.AreEqual("123", args.GetValue("first-value", string.Empty)); - Assert.AreEqual("a b c d e", args.GetValue("second-value", string.Empty)); - } - } -} diff --git a/tests/Data/TestCommandLineArgs.cs b/tests/Data/TestCommandLineArgs.cs index 70da4965..0dbe8102 100644 --- a/tests/Data/TestCommandLineArgs.cs +++ b/tests/Data/TestCommandLineArgs.cs @@ -153,5 +153,30 @@ public void TestValidStringArray(){ Assert.IsTrue(args.HasValue("-value")); Assert.AreEqual("Here is some text!", args.GetValue("-value", string.Empty)); } + + [TestMethod] + public void TestCefEmptyString(){ + Assert.AreEqual(0, CommandLineArgs.ReadCefArguments("").Count); + Assert.AreEqual(0, CommandLineArgs.ReadCefArguments(" ").Count); + } + + [TestMethod] + public void TestCefValidString(){ + CommandLineArgs args = CommandLineArgs.ReadCefArguments("--aaa --bbb --first-value=123 --SECOND-VALUE=\"a b c d e\"\r\n--ccc"); + // cef has no flags, flag arguments have a value of 1 + // the processing removes all dashes in front of each key + + Assert.AreEqual(5, args.Count); + Assert.IsTrue(args.HasValue("aaa")); + Assert.IsTrue(args.HasValue("bbb")); + Assert.IsTrue(args.HasValue("ccc")); + Assert.IsTrue(args.HasValue("first-value")); + Assert.IsTrue(args.HasValue("second-value")); + Assert.AreEqual("1", args.GetValue("aaa", string.Empty)); + Assert.AreEqual("1", args.GetValue("bbb", string.Empty)); + Assert.AreEqual("1", args.GetValue("ccc", string.Empty)); + Assert.AreEqual("123", args.GetValue("first-value", string.Empty)); + Assert.AreEqual("a b c d e", args.GetValue("second-value", string.Empty)); + } } } diff --git a/tests/UnitTests.csproj b/tests/UnitTests.csproj index a5bb36ad..b4aa6eee 100644 --- a/tests/UnitTests.csproj +++ b/tests/UnitTests.csproj @@ -51,7 +51,6 @@ <Compile Include="Data\TestCombinedFileStream.cs" /> <Compile Include="Core\TestBrowserUtils.cs" /> <Compile Include="Data\TestCommandLineArgs.cs" /> - <Compile Include="Core\TestCommandLineArgsParser.cs" /> <Compile Include="Data\TestFileSerializer.cs" /> <Compile Include="Data\TestInjectedHTML.cs" /> <Compile Include="Data\TestTwoKeyDictionary.cs" />