Esempio n. 1
0
        public bool TryGetMethodDescription(MethodInfo info, out CommandHelpMetadata commandHelpMetadata)
        {
            commandHelpMetadata = null;

            var sb = new StringBuilder();

            sb.Append("M:");
            AppendTypeName(sb, info.DeclaringType);
            sb.Append(".")
            .Append(info.Name)
            .Append("(");

            bool first = true;

            foreach (ParameterInfo param in info.GetParameters())
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    sb.Append(",");
                }

                AppendTypeName(sb, param.ParameterType);
            }

            sb.Append(")");

            string name = sb.ToString();

            XElement member = _members.Elements("member")
                              .FirstOrDefault(m => string.Equals(m.Attribute("name")?.Value, name));

            if (member == null)
            {
                return(false);
            }

            commandHelpMetadata = new CommandHelpMetadata();

            foreach (XElement element in member.Elements())
            {
                switch (element.Name.ToString())
                {
                case "summary":
                    commandHelpMetadata.Description = element.Value?.Trim();
                    break;

                case "param":
                    commandHelpMetadata.ParameterDescriptions.Add(element.Attribute("name")?.Value, element.Value?.Trim());
                    break;
                }
            }

            return(true);
        }
Esempio n. 2
0
        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));
            }

            var metadata = new CommandHelpMetadata();

            if (XmlDocReader.TryLoad(xmlDocsFilePath ?? GetDefaultXmlDocsFileLocation(method.DeclaringType.Assembly), 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;
                        }
                        else
                        {
                            var argument = builder.Command.Argument;
                            if (argument != null && !string.IsNullOrEmpty(argument.Name) && argument.Name.Equals(kebabCasedParameterName, StringComparison.OrdinalIgnoreCase))
                            {
                                argument.Description = parameterDescription.Value;
                            }
                        }
                    }

                    metadata.Name = method.DeclaringType.Assembly.GetName().Name;
                }
            }

            return(builder);
        }
        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);
        }
Esempio n. 4
0
        public bool TryGetMethodDescription(MethodInfo info, out CommandHelpMetadata commandHelpMetadata)
        {
            commandHelpMetadata = null;

            var sb = new StringBuilder();

            sb.Append("M:");
            AppendTypeName(sb, info.DeclaringType);
            sb.Append(".")
            .Append(info.Name)
            .Append("(");

            bool first = true;

            foreach (ParameterInfo param in info.GetParameters())
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    sb.Append(",");
                }

                AppendTypeName(sb, param.ParameterType);
            }

            sb.Append(")");

            string name = sb.ToString();

            XElement member = _members.Elements("member")
                              .FirstOrDefault(m => string.Equals(m.Attribute("name")?.Value, name));

            if (member == null)
            {
                return(false);
            }

            commandHelpMetadata = new CommandHelpMetadata();

            foreach (XElement element in member.Elements())
            {
                switch (element.Name.ToString())
                {
                case "summary":
                    if (element.HasElements)
                    {
                        var val = string.Join(string.Empty,
                                              element.Elements().Select(e =>
                                                                        e.Value + (e.Name.ToString().ToLower() == "para" ? Environment.NewLine : string.Empty)));
                        commandHelpMetadata.Description = val.TrimEnd(Environment.NewLine.ToCharArray());
                    }
                    else
                    {
                        commandHelpMetadata.Description = element.Value.Trim();
                    }
                    break;

                case "param":
                    var value = element.Attribute("name")?.Value;
                    if (value != null)
                    {
                        commandHelpMetadata.ParameterDescriptions.Add(value,
                                                                      element.Value.Trim());
                    }
                    break;
                }
            }

            return(true);
        }