public static void DisplayVersion(this ParseResultBase parseResult) { var parseResultType = parseResult.GetType(); var parserAttribute = parseResultType.GetCustomAttribute <CommandLineParserAttribute>(); var programAssembly = parserAttribute.ProgramAssemblyType.Assembly; var assemblyNameParts = programAssembly.GetNameParts(); var assemblyAttributes = programAssembly.GetAttributes(); var assemblyName = assemblyNameParts.AssemblyName; Console.WriteLine("{0} v{1}", assemblyName, assemblyAttributes.Version); Console.WriteLine(assemblyAttributes.Company); Console.WriteLine(assemblyAttributes.Copyright); Console.Write("Press any key to continue . . ."); Console.ReadKey(); parseResult.VersionDisplayed = true; }
public static void DisplayHelp(this ParseResultBase parseResult) { var parseResultType = parseResult.GetType(); var parserAttribute = parseResultType.GetCustomAttribute <CommandLineParserAttribute>(); var switchType = parserAttribute.SwitchType; var commandLineDescription = parserAttribute.Description; var programAssembly = parserAttribute.ProgramAssemblyType.Assembly; var assemblyNameParts = programAssembly.GetNameParts(); var assemblyAttributes = programAssembly.GetAttributes(); var assemblyName = assemblyNameParts.AssemblyName; var syntaxBuilder = new StringBuilder(); var switchListingBuilder = new StringBuilder(); var switches = new List <CommandLineSwitchAttribute>(); var switchNames = new List <string>(); int maxSwitchName; int x; foreach (var match in commandLineDescription.GetAttributeStringExpressionMatches()) { var expression = match.GetGroupValue("expression"); switch (expression) { case "AssemblyProduct": commandLineDescription = match.Replace(commandLineDescription, assemblyAttributes.Product ?? assemblyName); break; case "AssemblyName": commandLineDescription = match.Replace(commandLineDescription, assemblyName); break; default: DebugUtils.Break(); break; } } Console.WriteLine(commandLineDescription); Console.WriteLine(); Console.WriteLine("Syntax:"); syntaxBuilder.Append(assemblyName); foreach (var switchProperty in parserAttribute.SwitchType.GetConstants()) { if (switchProperty.HasCustomAttribute <CommandLineSwitchAttribute>()) { var switchAttribute = switchProperty.GetCustomAttribute <CommandLineSwitchAttribute>(); var switchName = (string)switchProperty.GetValue(null); syntaxBuilder.AppendFormat(" [/{0}]", switchName); switchNames.Add(switchName); switches.Add(switchAttribute); } } maxSwitchName = switchNames.Max(n => n.Length); x = 0; foreach (var switchAttribute in switches) { var switchName = switchNames.ElementAt(x); var padding = maxSwitchName + (switchAttribute.DescriptionLeftPaddingTabCount * 8); var attributeDescription = switchAttribute.Description; var switchNamePadded = switchName.PadRight(padding, ' '); foreach (var match in attributeDescription.GetAttributeStringExpressionMatches()) { var expression = match.GetGroupValue("expression"); var property = switchType.GetProperty(expression); var value = (string)property.GetValue(null); var lines = value.GetLines().Select(l => ' '.Repeat(12 + switchNamePadded.Length) + l); var ending = attributeDescription.RegexGet(match.Value + @"(?<ending>[^\{]*)", "ending"); value = lines.Join(); attributeDescription = match.Replace(attributeDescription, value); if (ending.Length > 0) { attributeDescription = attributeDescription.Replace(ending, "\r\n" + ' '.Repeat(4 + switchNamePadded.Length) + ending); } } switchListingBuilder.AppendLineFormat(@"{0}/{1}{2}", ' '.Repeat(4), switchNamePadded, attributeDescription); x++; } Console.WriteLine(syntaxBuilder); Console.WriteLine(); Console.WriteLine(switchListingBuilder); Console.Write("Press any key to continue . . ."); Console.ReadKey(); parseResult.HelpDisplayed = true; }