当前位置: 首页>>代码示例>>C#>>正文


C# ArgumentList.SafeGet方法代码示例

本文整理汇总了C#中ArgumentList.SafeGet方法的典型用法代码示例。如果您正苦于以下问题:C# ArgumentList.SafeGet方法的具体用法?C# ArgumentList.SafeGet怎么用?C# ArgumentList.SafeGet使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ArgumentList的用法示例。


在下文中一共展示了ArgumentList.SafeGet方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

		static int Main(string[] raw)
		{
			ArgumentList args = new ArgumentList(raw);

			using (Log.AppStart(Environment.CommandLine))
			{
				if (args.Contains("nologo") == false)
				{
					Console.WriteLine("StampVersion.exe");
					Console.WriteLine("Copyright 2009 by Roger Knapp, Licensed under the Apache License, Version 2.0");
					Console.WriteLine("");
				}

				if ((args.Unnamed.Count == 0 && args.Count == 0) || args.Contains("?") || args.Contains("help"))
					return DoHelp();

				try
				{
                    string major = null, minor = null, build = null, revision = null;
				    string version;
                    if(args.TryGetValue("version", out version))
                    {
                        string[] dotted = version.Split('.');
                        major = dotted[0];
                        minor = dotted.Length >= 1 ? dotted[1] : null;
                        build = dotted.Length >= 2 ? dotted[2] : null;
                        revision = dotted.Length >= 3 ? dotted[3] : null;
                    }

				    major = GetNumber("Major", args, major);
					minor = GetNumber("Minor", args, minor);
					build = GetNumber("Build", args, build);
					revision = GetNumber("Revision", args, revision);

					if (major == null && minor == null && build == null && revision == null)
						return DoHelp();

				    string fileversion = args.SafeGet("fileversion");

					FileList files = new FileList(@"AssemblyInfo.cs");

					Regex versionPattern = new Regex(@"[^a-z,A-Z,0-9](?<Type>AssemblyVersion|AssemblyFileVersion)\s*\(\s*\" + '\"' +
						@"(?<Version>[0-2]?[0-9]{1,9}\.[0-2]?[0-9]{1,9}(?:(?:\.[0-2]?[0-9]{1,9}(?:(?:\.[0-2]?[0-9]{1,9})|(?:\.\*))?)|(?:\.\*))?)\" + '\"' +
						@"\s*\)");

					foreach (FileInfo file in files.ToArray())
					{
						StringBuilder content = new StringBuilder();
						int lastpos = 0;
						string text = File.ReadAllText(file.FullName);
						foreach (Match match in versionPattern.Matches(text))
						{
							Group verMatch = match.Groups["Version"];
							content.Append(text, lastpos, verMatch.Index - lastpos);
							lastpos = verMatch.Index + verMatch.Length;

							string[] parts = verMatch.Value.Split('.');
							if( parts.Length < 2 )//regex should prevent us getting here
								throw new ApplicationException(String.Format("Bad version string: {0} on line {1}", verMatch.Value, content.ToString().Split('\n').Length));
							if (parts.Length < 3)
								parts = new string[] { parts[0], parts[1], "0" };
							else if( parts.Length == 3 && parts[2] == "*" )
								parts = new string[] { parts[0], parts[1], "0", "*" };
							if (parts.Length == 3 && revision != null)
								parts = new string[] { parts[0], parts[1], parts[2], "0" };
							if( build != null && build == "*" )
								parts = new string[] { parts[0], parts[1], "*" };

							if (major != null && parts.Length > 0)
								parts[0] = major;
							if (minor != null && parts.Length > 1)
								parts[1] = minor;
							if (build != null && parts.Length > 2)
								parts[2] = build;
							if (revision != null && parts.Length > 3)
								parts[3] = revision;

							//AssemblyFileVersion doesn't use '*', so trim off the build and/or revision
							if (match.Groups["Type"].Value == "AssemblyFileVersion")
							{
								if (parts.Length >= 4 && parts[3] == "*")
									parts = new string[] { parts[0], parts[1], parts[2] };
								if (parts.Length >= 3 && parts[2] == "*")
									parts = new string[] { parts[0], parts[1] };

							    if (!String.IsNullOrEmpty(fileversion))
							    {
							        parts = fileversion.Split('.');
							    }
							}

							string newVersion = String.Join(".", parts);
							//Console.WriteLine("Changed '{0}' to '{1}'", verMatch.Value, newVersion);
							content.Append(newVersion);
						}
						content.Append(text, lastpos, text.Length - lastpos);

					    if ((file.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
					        file.Attributes = file.Attributes & ~FileAttributes.ReadOnly;
                            
//.........这里部分代码省略.........
开发者ID:modulexcite,项目名称:CSharpTest.Net.Tools,代码行数:101,代码来源:Program.cs

示例2: GetNumber

		/// <summary>
		/// Retrieves only the numeric value specified or, if needed, reads the
		/// specified configuration file and parses.
		/// </summary>
        static string GetNumber(string optionName, ArgumentList args, string defaultValue)
		{
			ushort value;
			string text = args.SafeGet(optionName);

			if (String.IsNullOrEmpty(text)) return defaultValue;
			if (text == "*") return text;

			if (!ushort.TryParse(text, out value)) // not already a number?
			{
				if (File.Exists(text))
					text = ParseRevisionText(optionName, text); // read from file
				else
					text = String.Format(text, DateTime.Now); // format from date
			}

			try { text = ushort.Parse(text).ToString(); }//makes sure this is a number... 
			catch (Exception e) 
			{ throw new ApplicationException(String.Format("Number '{0}' is not valid: {1}", text, e.Message), e); }
			
			Console.WriteLine("{0} = {1}", optionName, text);
			return text;
		}
开发者ID:modulexcite,项目名称:CSharpTest.Net.Tools,代码行数:27,代码来源:Program.cs

示例3: Main

		static int Main(string[] raw)
		{
			ArgumentList args = new ArgumentList(raw);
			using (Log.AppStart(Environment.CommandLine))
			{
				if (args.Count == 0 || args.Unnamed.Count == 0 || args.Contains("?"))
					return DoHelp();
				if (args.Contains("nologo") == false)
				{
					Console.WriteLine("CSharpTest.Net.CoverageReport.exe");
					Console.WriteLine("Copyright 2009 by Roger Knapp, Licensed under the Apache License, Version 2.0");
					Console.WriteLine("");
				}

				try
				{
					List<string> filesFound = new List<string>();
					FileList files = new FileList();
					files.RecurseFolders = args.Contains("s");
					files.Add(new List<string>(args.Unnamed).ToArray());

					XmlParser parser = new XmlParser( args.SafeGet("exclude").Values );
					foreach (System.IO.FileInfo file in files)
					{
						filesFound.Add(file.FullName);
						using(Log.Start("parsing file: {0}", file.FullName))
							parser.Parse(file.FullName);
					}

					parser.Complete();

					if (args.Contains("module"))
					{
						using (Log.Start("Creating module report."))
						using (XmlReport rpt = new XmlReport(OpenText(args["module"]), parser, "Module Summary", filesFound.ToArray()))
							new MetricReport(parser.ByModule).Write(rpt);
					}

					if (args.Contains("namespace"))
					{
						using (Log.Start("Creating namespace report."))
						using (XmlReport rpt = new XmlReport(OpenText(args["namespace"]), parser, "Namespace Summary", filesFound.ToArray()))
							new MetricReport(parser.ByNamespace).Write(rpt);
					}

					if (args.Contains("class"))
					{
						using (Log.Start("Creating class report."))
						using (XmlReport rpt = new XmlReport(OpenText(args["class"]), parser, "Module Class Summary", filesFound.ToArray()))
							new MetricReport(parser.ByModule, parser.ByNamespace, parser.ByClass).Write(rpt);
					}

                    if (args.Contains("unused"))
                    {
                        using (Log.Start("Creating unused statement report."))
                        using (UnusedReport rpt = new UnusedReport(OpenText(args["unused"]), parser))
                            new MetricReport(parser.ByModule, parser.ByMethod).Write(rpt);
                    }

					if (args.Contains("combine"))
					{
						using (Log.Start("Creating combined coverage file."))
						using (XmlCoverageWriter wtr = new XmlCoverageWriter(OpenText(args["combine"]), parser))
							new MetricReport(parser.ByModule, parser.ByMethod).Write(wtr);
					}
					//foreach (ModuleInfo mi in parser)
					//    Console.WriteLine("{0} hit {1} of {2} for {3}", mi.Name, mi.VisitedPoints, mi.SequencePoints, mi.CoveragePercent);

				}
				catch (Exception e)
				{
					Log.Error(e);
					Console.Error.WriteLine();
					Console.Error.WriteLine("Exception: {0}", e.Message);
					Console.Error.WriteLine();
					Environment.ExitCode = -1;
				}
			}

			if (args.Contains("wait"))
			{
				Console.WriteLine("Press [Enter] to continue...");
				Console.ReadLine();
			}

			return Environment.ExitCode;
		}
开发者ID:hivie7510,项目名称:csharptest-net,代码行数:87,代码来源:Program.cs

示例4: Test

		public void Test()
		{
			ArgumentList args = new ArgumentList("-test=value", "/Test", "\"/other:value\"");
			Assert.AreEqual(2, args.Count);

			Assert.AreEqual(1, args[0].Count);
			Assert.AreEqual("test", args[0].Name);
			Assert.AreEqual("value", args[1].Value);

			Assert.AreEqual(1, args[1].Count);
			Assert.AreEqual("other", args[1].Name);
			Assert.AreEqual("value", args[1].Value);
			
			string[] keys = args.Keys;
			Assert.AreEqual(2, keys.Length);
			Assert.AreEqual("other", keys[0]);//alpha-sorted
			Assert.AreEqual("test", keys[1]);
			Assert.AreEqual(0, new ArgumentList("unnamed").Keys.Length);
			Assert.AreEqual(0, new ArgumentList(/*empty*/).Keys.Length);

			ArgumentList.DefaultComparison = StringComparer.Ordinal;
			Assert.AreEqual(StringComparer.Ordinal, ArgumentList.DefaultComparison);
			
			ArgumentList.NameDelimeters = new char[] { '=' };
			Assert.AreEqual('=', ArgumentList.NameDelimeters[0]);

			ArgumentList.PrefixChars = new char[] { '/' };
			Assert.AreEqual('/'	, ArgumentList.PrefixChars[0]);

			args = new ArgumentList("-test=value", "/Test", "\"/other:value\"");
			Assert.AreEqual(2, args.Count);
			Assert.AreEqual(0, args[0].Count);
			Assert.AreEqual("Test", args[0].Name);
			Assert.AreEqual(null, args[1].Value);

			Assert.AreEqual(1, args.Unnamed.Count);
			foreach(string sval in args.Unnamed)
				Assert.AreEqual("-test=value", sval);

			Assert.AreEqual(0, args[1].Count);
			Assert.AreEqual("other:value", args[1].Name);
			Assert.AreEqual(null, args[1].Value);

			args.Unnamed = new string[0];
			Assert.AreEqual(0, args.Unnamed.Count);

			args.Add("other", "value");
			Assert.AreEqual(null, (string)args["Test"]);
			Assert.AreEqual("value", (string)args["other"]);
			Assert.AreEqual("value", (string)args.SafeGet("other"));
			Assert.IsNotNull(args.SafeGet("other-not-existing"));
			Assert.AreEqual(null, (string)args.SafeGet("other-not-existing"));

			string test;
			ArgumentList.Item item;

			args = new ArgumentList();
			Assert.AreEqual(0, args.Count);
			Assert.IsFalse(args.TryGetValue(String.Empty, out item));
			args.Add(String.Empty, null);
			Assert.IsTrue(args.TryGetValue(String.Empty, out item));

			args = new ArgumentList();
			Assert.AreEqual(0, args.Count);
			Assert.IsFalse(args.TryGetValue(String.Empty, out test));
			args.Add(String.Empty, null);
			Assert.IsTrue(args.TryGetValue(String.Empty, out test));

			test = item;
			Assert.IsNull(test);

			string[] testarry = item;
			Assert.IsNotNull(testarry);
			Assert.AreEqual(0, testarry.Length);

			item.Value = "roger";
			Assert.AreEqual("roger", item.Value);
			Assert.AreEqual(1, item.Values.Length);
			Assert.AreEqual("roger", item.Values[0]);

			Assert.Contains("roger", item.ToArray());
			Assert.AreEqual(1, item.ToArray().Length);

			item.AddRange(new string[] { "wuz", "here" });
			Assert.AreEqual(3, item.Values.Length);
			Assert.AreEqual("roger wuz here", String.Join(" ", item));

			item.Values = new string[] { "roger", "was", "here" };
			Assert.AreEqual("roger was here", String.Join(" ", item));

			KeyValuePair<string, string[]> testkv = item;
			Assert.AreEqual(String.Empty, testkv.Key);
			Assert.AreEqual(3, testkv.Value.Length);
			Assert.AreEqual("roger was here", String.Join(" ", testkv.Value));
		}
开发者ID:hivie7510,项目名称:csharptest-net,代码行数:95,代码来源:TestArgumentList.cs


注:本文中的ArgumentList.SafeGet方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。