public DialogSettingsRestart(CommandLineArgs currentArgs) { InitializeComponent(); try{ object[] locales = Directory.EnumerateFiles(Path.Combine(Program.ProgramPath, "locales"), "*.pak", SearchOption.TopDirectoryOnly).Select(Path.GetFileNameWithoutExtension).ToArray <object>(); comboLocale.Items.AddRange(locales); }catch { comboLocale.Items.Add(DefaultLocale); } cbLogging.Checked = currentArgs.HasFlag(Arguments.ArgLogging); cbDebugUpdates.Checked = currentArgs.HasFlag(Arguments.ArgDebugUpdates); comboLocale.SelectedItem = currentArgs.GetValue(Arguments.ArgLocale, DefaultLocale); cbLogging.CheckedChanged += control_Change; cbDebugUpdates.CheckedChanged += control_Change; comboLocale.SelectedValueChanged += control_Change; if (Program.IsPortable) { tbDataFolder.Text = "Not available in portable version"; tbDataFolder.Enabled = false; } else { tbDataFolder.Text = currentArgs.GetValue(Arguments.ArgDataFolder, string.Empty); tbDataFolder.TextChanged += control_Change; } control_Change(this, EventArgs.Empty); Text = Program.BrandName + " Arguments"; }
public void SetTest() { CommandLineArgs args = new CommandLineArgs(); args.RegisterArgument("set", new SetArgument(new[] { "v1", "v2" }, "v1")); Assert.IsTrue(args.Validate("/set=v1")); Assert.AreEqual("v1", args.GetValue <string>("set")); Assert.IsTrue(args.Validate("/set=v2")); Assert.AreEqual("v2", args.GetValue <string>("set")); Assert.IsFalse(args.Validate("/set=v3")); }
public void DefaultArgumentTest() { CommandLineArgs args = new CommandLineArgs(); args.RegisterArgument("default", new OptionArgument(null, true)); args.SetDefaultArgument("default"); Assert.IsTrue(args.Validate("value")); Assert.AreEqual("value", args.GetValue <string>("default")); args.RegisterArgument("flag", new FlagArgument(false, true)); Assert.IsTrue(args.Validate("value /flag")); Assert.AreEqual("value", args.GetValue <string>("default")); Assert.AreEqual(true, args.GetValue <bool>("flag")); Assert.IsTrue(args.Validate("/flag value")); Assert.AreEqual("value", args.GetValue <string>("default")); Assert.AreEqual(true, args.GetValue <bool>("flag")); Assert.IsTrue(args.Validate("/flag flag")); Assert.AreEqual("flag", args.GetValue <string>("default")); Assert.AreEqual(true, args.GetValue <bool>("flag")); args = new CommandLineArgs(); args.RegisterArgument("default", new OptionArgument(null, true)); Assert.IsFalse(args.Validate("value")); ExceptionAssert.Assert <ArgumentException>(() => args.SetDefaultArgument("nonexisting")); }
public void TestValidString() { CommandLineArgs args = CommandLineArgsParser.ReadCefArguments("--aaa --bbb --first-value=123 --SECOND-VALUE=\"a b c d e\" --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)); }
public void FlagTest() { CommandLineArgs args = new CommandLineArgs(); args.RegisterArgument("flag", new FlagArgument()); Assert.IsTrue(args.Validate(string.Empty)); Assert.IsFalse(args.GetValue <bool>("flag")); ExceptionAssert.Assert <KeyNotFoundException>(() => args.GetValue <bool>("nonexisting")); Assert.IsTrue(args.Validate("-flag")); Assert.IsTrue(args.GetValue <bool>("flag")); Assert.IsTrue(args.Validate("--flag")); Assert.IsTrue(args.GetValue <bool>("flag")); Assert.IsTrue(args.Validate("/flag")); Assert.IsTrue(args.GetValue <bool>("flag")); }
public void CollectionTest() { CommandLineArgs args = new CommandLineArgs(); args.RegisterArgument("option", new CollectionArgument()); Assert.IsTrue(args.Validate("/option=value1")); CollectionAssert.AreEqual(new[] { "value1" }, args.GetValue <string[]>("option")); Assert.IsTrue(args.Validate("/option=value1 /option=value2")); var values = args.GetValue <string[]>("option"); CollectionAssert.AreEqual(new[] { "value1", "value2" }, values); Assert.IsTrue(args.Validate("/option=value1 --option=value2")); values = args.GetValue <string[]>("option"); CollectionAssert.AreEqual(new[] { "value1", "value2" }, values); ExceptionAssert.Assert <KeyNotFoundException>(() => args.GetValue <string[]>("nonexisting")); }
public void ValidateTest() { CommandLineArgs args = new CommandLineArgs(); args.RegisterArgument("flag", new FlagArgument(true, true)); Assert.IsFalse(args.Validate(string.Empty)); args = new CommandLineArgs(); args.RegisterHelpArgument(); args.RegisterArgument("flag", new FlagArgument()); Assert.IsTrue(args.Validate(new[] { "/help", "/flag" })); Assert.IsTrue(args.GetValue <bool>("help")); Assert.IsTrue(args.GetValue <bool>("flag")); OptionalOut <string[]> outErrors = new OptionalOut <string[]>(); Assert.IsFalse(args.Validate("/unknown", outErrors)); Assert.AreEqual(1, outErrors.Result.Length); Assert.AreEqual("Unknown option: 'unknown'", outErrors.Result[0]); args.RegisterArgument("option", new OptionArgument(null)); Assert.IsFalse(args.Validate("/option", outErrors)); Assert.AreEqual(1, outErrors.Result.Length); Assert.AreEqual("Missing value for option 'option'", outErrors.Result[0]); Assert.IsFalse(args.Validate("/option /flag", outErrors)); Assert.AreEqual(1, outErrors.Result.Length); Assert.AreEqual("Missing value for option 'option'", outErrors.Result[0]); args = new CommandLineArgs(); args.RegisterArgument("option", new OptionArgument(null, true)); Assert.IsFalse(args.Validate("")); Assert.IsTrue(args.Validate("/option=value")); }
public void DefaultCollectionTest() { CommandLineArgs args = new CommandLineArgs(); args.RegisterArgument("default", new CollectionArgument(true)); args.RegisterArgument("flag", new FlagArgument()); args.SetDefaultArgument("default"); Assert.IsTrue(args.Validate("test1 test2")); string[] values = args.GetValue <string[]>("default"); CollectionAssert.AreEqual(new[] { "test1", "test2" }, values); Assert.IsTrue(args.Validate("test1 test2 /flag")); values = args.GetValue <string[]>("default"); CollectionAssert.AreEqual(new[] { "test1", "test2" }, values); Assert.IsTrue(args.GetValue <bool>("flag")); Assert.IsTrue(args.Validate("/flag test1 test2")); values = args.GetValue <string[]>("default"); CollectionAssert.AreEqual(new[] { "test1", "test2" }, values); Assert.IsTrue(args.GetValue <bool>("flag")); }
static void Main(string[] args) { CommandLineArgs cmd = new CommandLineArgs(); cmd.RegisterArgument("h", new OptionArgument("192.168.1.1")); cmd.RegisterArgument("ru", new OptionArgument("root")); cmd.RegisterArgument("rp", new OptionArgument("root")); cmd.RegisterArgument("pu", new OptionArgument("", true)); cmd.RegisterArgument("pp", new OptionArgument("", true)); if (!cmd.Validate(args)) { Console.WriteLine("exp:\r\nSmartRouter h RouterIP ru RouterUser rp RoterPwd pu PPPoEUser pp PPPoEPwd"); return; } host = cmd.GetValue <string>("h"); rUser = cmd.GetValue <string>("ru"); rPwd = cmd.GetValue <string>("rp"); pUser = cmd.GetValue <string>("pu"); pPwd = cmd.GetValue <string>("pp"); Console.WriteLine("{0} : {1}", "Host", host); Console.WriteLine("{0} : {1}", "Username", rUser); Console.WriteLine("{0} : {1}", "Password", rPwd); Console.WriteLine("{0} : {1}", "PPPoEUser", pUser); Console.WriteLine("{0} : {1}", "PPPoEPwd", pPwd); Login(); ChangeWANConfig(); ConnectWAN(); PrintSysLog(); Console.ReadLine(); }
public void TestValues() { CommandLineArgs args = new CommandLineArgs(); args.SetValue("test_value", "My Test Value"); args.SetValue("aAaAa", "aaaaa"); Assert.IsFalse(args.HasFlag("aAaAa")); Assert.AreEqual(2, args.Count); Assert.IsTrue(args.HasValue("test_value")); Assert.IsTrue(args.HasValue("aaaaa")); Assert.IsTrue(args.HasValue("AAAAA")); Assert.AreEqual("My Test Value", args.GetValue("test_value", string.Empty)); Assert.AreEqual("aaaaa", args.GetValue("aaaaa", string.Empty)); Assert.AreEqual("test_value \"My Test Value\" aaaaa \"aaaaa\"", args.ToString()); args.RemoveValue("Aaaaa"); Assert.AreEqual(1, args.Count); Assert.IsTrue(args.HasValue("test_value")); Assert.IsFalse(args.HasValue("aaaaa")); Assert.AreEqual("test_value \"My Test Value\"", args.ToString()); }
public void HelpArgumentTest() { CommandLineArgs args = new CommandLineArgs(); args.RegisterHelpArgument(); Assert.IsTrue(args.Validate("/help")); Assert.IsTrue(args.GetValue <bool>("help")); using (StringWriter writer = new StringWriter()) { args.OutputWriter = writer; args.Process(); Assert.IsFalse(string.IsNullOrWhiteSpace(writer.ToString())); } }
public void TestEmpty() { CommandLineArgs args = new CommandLineArgs(); Assert.AreEqual(0, args.Count); Assert.AreEqual(string.Empty, args.ToString()); Assert.IsFalse(args.HasFlag("x")); Assert.IsFalse(args.HasValue("x")); Assert.AreEqual("default", args.GetValue("x", "default")); args.RemoveFlag("x"); args.RemoveValue("x"); var dict = new Dictionary <string, string>(); args.ToDictionary(dict); Assert.AreEqual(0, dict.Count); }
public DialogSettingsRestart(CommandLineArgs currentArgs) { InitializeComponent(); cbLogging.Checked = currentArgs.HasFlag(Arguments.ArgLogging); cbLogging.CheckedChanged += control_Change; if (Program.IsPortable) { tbDataFolder.Text = "Not available in portable version"; tbDataFolder.Enabled = false; } else { tbDataFolder.Text = currentArgs.GetValue(Arguments.ArgDataFolder) ?? string.Empty; tbDataFolder.TextChanged += control_Change; } control_Change(this, EventArgs.Empty); Text = Program.BrandName + " Arguments"; }
public static string GetValue(string key, string defaultValue) { return(Current.GetValue(key, defaultValue)); }
public static string GetValue(string key) { return(Current.GetValue(key)); }
public void OptionTest() { CommandLineArgs args = new CommandLineArgs(); args.RegisterArgument("option", new OptionArgument("123")); Assert.IsTrue(args.Validate(string.Empty)); Assert.AreEqual("123", args.GetValue <string>("option")); Assert.IsTrue(args.Validate("/option=42")); Assert.AreEqual("42", args.GetValue <string>("option")); Assert.IsTrue(args.Validate("/option:42")); Assert.AreEqual("42", args.GetValue <string>("option")); Assert.IsTrue(args.Validate("/option 42")); Assert.AreEqual("42", args.GetValue <string>("option")); Assert.IsTrue(args.Validate("--option=42")); Assert.AreEqual("42", args.GetValue <string>("option")); Assert.IsTrue(args.Validate("--option:42")); Assert.AreEqual("42", args.GetValue <string>("option")); Assert.IsTrue(args.Validate("--option 42")); Assert.AreEqual("42", args.GetValue <string>("option")); Assert.IsTrue(args.Validate("-option 42")); Assert.AreEqual("42", args.GetValue <string>("option")); Assert.IsTrue(args.Validate("-option=42")); Assert.AreEqual("42", args.GetValue <string>("option")); Assert.IsTrue(args.Validate("-option:42")); Assert.AreEqual("42", args.GetValue <string>("option")); ExceptionAssert.Assert <KeyNotFoundException>(() => args.GetValue <string>("nonexisting")); Assert.IsTrue(args.Validate("-option:42 /option=444")); Assert.AreEqual("444", args.GetValue <string>("option")); }
public void PingTest() { CommandLineArgs args = new CommandLineArgs(); args.RegisterArgument("target_name", new OptionArgument(null, true)); args.RegisterArgument("t", new FlagArgument()); args.RegisterArgument("4", new FlagArgument()); args.RegisterArgument("6", new FlagArgument()); args.SetDefaultArgument("target_name"); Assert.IsTrue(args.Validate("localhost")); Assert.AreEqual("localhost", args.GetValue <string>("target_name")); Assert.IsFalse(args.GetValue <bool>("4")); Assert.IsFalse(args.GetValue <bool>("6")); Assert.IsFalse(args.GetValue <bool>("t")); Assert.IsTrue(args.Validate("localhost -t")); Assert.AreEqual("localhost", args.GetValue <string>("target_name")); Assert.IsFalse(args.GetValue <bool>("4")); Assert.IsFalse(args.GetValue <bool>("6")); Assert.IsTrue(args.GetValue <bool>("t")); Assert.IsTrue(args.Validate("localhost -4")); Assert.AreEqual("localhost", args.GetValue <string>("target_name")); Assert.IsTrue(args.GetValue <bool>("4")); Assert.IsFalse(args.GetValue <bool>("6")); Assert.IsFalse(args.GetValue <bool>("t")); Assert.IsTrue(args.Validate("localhost -6")); Assert.AreEqual("localhost", args.GetValue <string>("target_name")); Assert.IsFalse(args.GetValue <bool>("4")); Assert.IsTrue(args.GetValue <bool>("6")); Assert.IsFalse(args.GetValue <bool>("t")); Assert.IsTrue(args.Validate("localhost -6 -t")); Assert.AreEqual("localhost", args.GetValue <string>("target_name")); Assert.IsFalse(args.GetValue <bool>("4")); Assert.IsTrue(args.GetValue <bool>("6")); Assert.IsTrue(args.GetValue <bool>("t")); }
public void PositionalArgumentsTest() { CommandLineArgs args = new CommandLineArgs(); args.RegisterArgument("first", new OptionArgument(null, true, 0)); args.RegisterArgument("second", new OptionArgument(null, true, 1)); args.RegisterArgument("third", new OptionArgument(null, false, 2)); Assert.IsFalse(args.Validate("one")); Assert.IsTrue(args.Validate("one two")); Assert.AreEqual("one", args.GetValue <string>("first")); Assert.AreEqual("two", args.GetValue <string>("second")); Assert.AreEqual(null, args.GetValue <string>("third")); Assert.IsTrue(args.Validate("one two three")); Assert.AreEqual("one", args.GetValue <string>("first")); Assert.AreEqual("two", args.GetValue <string>("second")); Assert.AreEqual("three", args.GetValue <string>("third")); args.RegisterArgument("flag", new FlagArgument()); Assert.IsTrue(args.Validate("one two three")); Assert.AreEqual(false, args.GetValue <bool>("flag")); Assert.IsTrue(args.Validate("one two three /flag")); Assert.AreEqual(true, args.GetValue <bool>("flag")); args.RegisterArgument("default", new OptionArgument("def")); args.SetDefaultArgument("default"); Assert.IsTrue(args.Validate("one two three /flag zero")); Assert.AreEqual(true, args.GetValue <bool>("flag")); Assert.AreEqual("zero", args.GetValue <string>("default")); Assert.IsTrue(args.Validate("one two three zero /flag")); Assert.AreEqual(true, args.GetValue <bool>("flag")); Assert.AreEqual("zero", args.GetValue <string>("default")); }