public void IListTests() { var a0 = new CommandArg("abc"); var a1 = new CommandArg("def"); var d0 = new CommandLine($"--{a0.Name}"); Assert.Single(d0); d0.Insert(0, a1); Assert.Equal(a0, d0[a0.Name]); Assert.False(d0.IsReadOnly); d0.Clear(); Assert.Empty(d0); d0.Add(a0); Assert.True(d0.Contains(a0)); Assert.Single(d0); var r0 = new CommandArg[1]; d0.CopyTo(r0, 0); Assert.Equal(a0.Name, r0[0].Name); Assert.Equal(0, d0.IndexOf(r0[0])); int i = 0; foreach (var arg in d0) { ++i; } Assert.Equal(1, i); Assert.True(d0.Remove(r0[0])); Assert.Empty(d0); d0.Add(r0[0]); d0.RemoveAt(0); Assert.Empty(d0); }
public void GetValueTest() { int a0 = 123; string n0 = "a"; string v0 = "abc"; int i0 = 1; string s0 = "b"; var d = new CommandLine(); d.Add(new CommandArg(n0, v0)); d.Add(new CommandArg(i0, a0.ToString())); d.Add(new CommandArg(v0, s0)); Assert.Equal(v0, d[n0].Value); Assert.Equal(a0, d.GetValue <int>(i0)); Assert.Equal(s0, d[v0].Value); }
public void GetResolvedValueTest() { string a0 = "123"; string n0 = "a"; string v0 = "abc"; int i0 = 1; string s0 = "b"; var d = new CommandLine(); d.Add(new CommandArg(n0, v0)); d.Add(new CommandArg(i0, a0.ToString())); Assert.Equal(v0, d.GetResolvedValue <string>(n0)); Assert.Equal(a0, d.GetResolvedValue <string>(i0)); Assert.Equal(s0, d.GetResolvedValue <string>(v0, s0)); Assert.Equal(s0, d.GetResolvedValue <string>(2, s0)); }
public void Loaded(object sender, EventArgs eventargs) { Saveable s; try { k.w(VersionInfo.Get().FullVersion); bool exit = false; CommandLine cl = new CommandLine((t) => { }, (t) => { }, () => { return(""); }); CommandArgs ca = new CommandArgs("configpath", 1, new CommandArgs.Subcommand("folder", 1), new CommandArgs.Subcommand("filename", 1)); ca.AliasSubcommand("filename", "name"); ca.AliasSubcommand("filename", "file"); ca.AliasSubcommand("folder", "path"); cl.Add(ca, (c) => { c.GetArg("folder", Saveable.Path); c.GetArg("filename", Saveable.Name); string tmp = ""; if (c.GetArgBool("configpath", Saveable.Path + Saveable.Name, out tmp)) { Saveable.Name = Path.DirectorySeparatorChar + Path.GetFileName(tmp); Saveable.Path = Path.GetDirectoryName(tmp); } if (c.GetArgBool("folder", Saveable.Path, out tmp)) { Saveable.Path = tmp; } if (c.GetArgBool("filename", Saveable.Name, out tmp)) { Saveable.Name = Path.DirectorySeparatorChar + tmp; } return(Saveable.Path + Saveable.Name); }); cl.AddAlias("configpath", "path"); cl.AddAlias("configpath", "config"); cl.Add(new CommandArgs("loadv1"), (c) => { Form1 f = new Form1(); f.ShowDialog(); exit = true; return("Loaded"); }); cl.Add(new CommandArgs("loadv2"), (c) => { s = Saveable.DeserializeItem(); Camviewer f = new Camviewer(s); f.ShowDialog(); exit = true; return("Loaded"); }); cl.Add(new CommandArgs("crash"), (c) => { try { ErrorTest0(); } catch (Exception e) { throw new TestException("This is a test exception.", e); } return("Crashed"); }); string tmp0 = ""; string tmp1 = ""; foreach (string tmp2 in Cam_Program.Camviewer.cla) { tmp0 += tmp2 + " "; } cl.Read(tmp0, out tmp1); if (!exit) { s = Saveable.DeserializeItem(); if (File.Exists(Saveable.Path + crashdump)) { if (s.Askfiledeletecrash) { if (new FileInfo(Saveable.Path + crashdump).Length > crashsize) { string str = Ruaraidheulib.Winforms.MessageBox.Show("Crash dump file is getting big do you want to clear it?", "Large File", Ruaraidheulib.Winforms.MessageBox.ButtonLayout.Four, Ruaraidheulib.Winforms.MessageBox.ButtonLayout.Four, "Yes", "No", "Always", "Never"); switch (str) { case "Yes": File.Delete(Saveable.Path + crashdump); break; case "No": break; case "Always": File.Delete(Saveable.Path + crashdump); s.Askfiledeletecrash = false; s.Alwaysdeletecrash = true; break; case "Never": s.Askfiledeletecrash = false; s.Alwaysdeletecrash = false; break; } } } else { if (s.Alwaysdeletecrash && new FileInfo(Saveable.Path + crashdump).Length > crashsize) { File.Delete(Saveable.Path + crashdump); } } } if (File.Exists(Saveable.Path + logfile)) { if (s.Askfiledeletelog) { if (new FileInfo(Saveable.Path + logfile).Length > logsize) { string str = Ruaraidheulib.Winforms.MessageBox.Show("Log file is getting big do you want to clear it?", "Large File", Ruaraidheulib.Winforms.MessageBox.ButtonLayout.Two, Ruaraidheulib.Winforms.MessageBox.ButtonLayout.One, "Yes", "No", "Always", "Never"); switch (str) { case "Yes": File.Delete(Saveable.Path + logfile); break; case "No": break; case "Always": File.Delete(Saveable.Path + logfile); s.Askfiledeletelog = false; s.Alwaysdeletelog = true; break; case "Never": s.Askfiledeletelog = false; s.Alwaysdeletelog = false; break; } } } else { if (s.Alwaysdeletelog && new FileInfo(Saveable.Path + logfile).Length > logsize) { File.Delete(Saveable.Path + logfile); } } } using (Camviewer cv = new Camviewer(s)) cv.ShowDialog(); } } catch (Exception e) { Ruaraidheulib.Winforms.MessageBox.Show("This program has been closed due to a fatal " + Ruaraidheulib.Winforms.MessageBox.ErrorWrite(e), "Fatal Exception", Ruaraidheulib.Winforms.MessageBox.ButtonLayout.One, Ruaraidheulib.Winforms.MessageBox.ButtonLayout.One, "OK"); try { if (Camviewer.sv != null) { Saveable.SerializeItem(Camviewer.sv); } } catch (Exception ex) { Ruaraidheulib.Winforms.MessageBox.Show("Could not save data." + Environment.NewLine + Environment.NewLine + Ruaraidheulib.Winforms.MessageBox.ErrorWrite(ex), "Exception", Ruaraidheulib.Winforms.MessageBox.ButtonLayout.One, Ruaraidheulib.Winforms.MessageBox.ButtonLayout.One, "OK"); } try { File.AppendAllText(Saveable.Path + crashdump, Environment.NewLine + "--------------------------------------------------" + Environment.NewLine + Ruaraidheulib.Winforms.MessageBox.ErrorWrite(e)); } catch (Exception ex) { Ruaraidheulib.Winforms.MessageBox.Show("Could not save error dump." + Environment.NewLine + Environment.NewLine + Ruaraidheulib.Winforms.MessageBox.ErrorWrite(ex), "Exception", Ruaraidheulib.Winforms.MessageBox.ButtonLayout.One, Ruaraidheulib.Winforms.MessageBox.ButtonLayout.One, "OK"); } } this.Close(); }