public static CommandLineBuilder ConfigureHelpFromXmlComments( this CommandLineBuilder builder, MethodInfo method, string xmlDocsFilePath) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } if (method == null) { throw new ArgumentNullException(nameof(method)); } if (XmlDocReader.TryLoad(xmlDocsFilePath ?? GetDefaultXmlDocsFileLocation(method.DeclaringType.Assembly), out var xmlDocs)) { if (xmlDocs.TryGetMethodDescription(method, out CommandHelpMetadata metadata) && metadata.Description != null) { builder.Command.Description = metadata.Description; var options = builder.Options.ToArray(); foreach (var parameterDescription in metadata.ParameterDescriptions) { var kebabCasedParameterName = parameterDescription.Key.ToKebabCase(); var option = options.FirstOrDefault(o => o.HasAliasIgnorePrefix(kebabCasedParameterName)); if (option != null) { option.Description = parameterDescription.Value; } else { for (var i = 0; i < builder.Command.Arguments.Count; i++) { var argument = builder.Command.Arguments[i]; if (string.Equals( argument.Name, kebabCasedParameterName, StringComparison.OrdinalIgnoreCase)) { argument.Description = parameterDescription.Value; } } } } metadata.Name = method.DeclaringType.Assembly.GetName().Name; } } return(builder); }
public static bool TryLoad(string filePath, out XmlDocReader xmlDocReader) { try { return(TryLoad(File.OpenText(filePath), out xmlDocReader)); } catch { xmlDocReader = null; return(false); } }
public static bool TryLoad(TextReader reader, out XmlDocReader xmlDocReader) { try { xmlDocReader = new XmlDocReader(XDocument.Load(reader)); return(true); } catch { xmlDocReader = null; return(false); } }
public static CommandLineBuilder ConfigureHelpFromXmlComments( this CommandLineBuilder builder, MethodInfo method) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } if (method == null) { throw new ArgumentNullException(nameof(method)); } Assembly assembly = method.DeclaringType.Assembly; string docFilePath = Path.Combine( Path.GetDirectoryName(assembly.Location), Path.GetFileNameWithoutExtension(assembly.Location) + ".xml"); var metadata = new CommandHelpMetadata(); if (XmlDocReader.TryLoad(docFilePath, out var xmlDocs)) { if (xmlDocs.TryGetMethodDescription(method, out metadata) && metadata.Description != null) { builder.Command.Description = metadata.Description; var options = builder.Options; foreach (var parameterDescription in metadata.ParameterDescriptions) { var kebabCasedParameterName = parameterDescription.Key.ToKebabCase(); var option = options.FirstOrDefault(o => o.HasAlias(kebabCasedParameterName)); if (option != null) { option.Description = parameterDescription.Value; } } metadata.Name = assembly.GetName().Name; } } return(builder); }