internal string GetHelpText(StringCollector collector, string executable)
        {
            var options = Options.Select(o => o.GetUsage(Parser.OptionChar));

            collector.AppendLine("SYNTAX");
            collector.Indent = 2;
            collector.Append(executable);

            foreach (var option in options)
            {
                collector.Append(option);
            }
            collector.AppendLine("");
            collector.Indent = 0;

            collector.AppendLine("");
            collector.AppendLine("OPTIONS");
            collector.Indent = 2;
            foreach (var option in Options)
            {
                collector.AppendLine(option.GetUsage(Parser.OptionChar));
                if (option.Description != "")
                {
                    collector.Indent += 2;
                    collector.AppendLine(option.Description);
                    collector.Indent -= 2;
                }
                if (option.Property.PropertyType.IsEnum)
                {
                    collector.Indent += 4;
                    foreach (var name in Enum.GetNames(option.Property.PropertyType))
                    {
                        var description = option.Property.PropertyType.GetField(name).GetCustomAttribute <DescriptionAttribute>()?.Description;
                        if (description != null)
                        {
                            collector.AppendLine($"{name} - {description}");
                        }
                    }
                    collector.Indent -= 4;
                }
                if (option.DefaultValue != null)
                {
                    collector.Indent += 2;
                    collector.AppendLine($"default: '{option.DefaultValue.Value}'");
                    collector.Indent -= 2;
                }
                collector.AppendLine("");
            }
            collector.Indent = 0;

            return(collector.ToString());
        }
예제 #2
0
        /// <summary>
        /// Gets the help text.
        /// </summary>
        /// <param name="verb">creates the help text for this option.</param>
        /// <param name="width">Width of the genreated text. (default: 80)</param>
        /// <returns>The help text</returns>
        /// <remarks>
        /// The returned text is not complete. Work in progress.
        /// </remarks>
        public string GetHelpText(string verb = null, int width = 80)
        {
            var collector = new StringCollector(width);
            var assembly  = Assembly.GetEntryAssembly();

            string name;
            string executable;
            string version;

            if (assembly != null)
            {
                version    = assembly.GetName().Version.ToString();
                executable = Path.GetFileName(assembly.Location);

                var titleAttribute = assembly.GetCustomAttribute <AssemblyTitleAttribute>();
                if (titleAttribute != null)
                {
                    name = titleAttribute.Title;
                }
                else
                {
                    name = Path.GetFileNameWithoutExtension(executable);
                }
            }
            else
            {
                name       = "<no assembly title>";
                version    = "<no assembly version>";
                executable = "<no executable>";
            }
            collector.AppendLine($"{name} - {version}");
            collector.AppendLine("");

            var descriptionAttribute = assembly?.GetCustomAttribute <AssemblyDescriptionAttribute>();

            if (descriptionAttribute != null)
            {
                collector.AppendLine("DESCRIPTION");
                collector.Indent = 2;
                collector.AppendLine(descriptionAttribute.Description);
                collector.Indent = 0;
                collector.AppendLine("");
            }

            if (verb != null)
            {
                OptionTypes[verb].GetHelpText(collector, executable);
            }
            else if (OptionTypes.Count == 1)
            {
                OptionType.GetHelpText(collector, executable);
            }
            else
            {
                collector.AppendLine("SYNTAX");
                collector.Indent = 2;
                collector.AppendLine($"{executable} <verb> <options>");
                collector.AppendLine("");
                collector.Indent = 0;

                collector.AppendLine("VERBS");
                collector.Indent = 2;
                foreach (var optionType in OptionTypes.Values)
                {
                    collector.Append(optionType.Verb);
                    if (optionType.Description != "")
                    {
                        collector.AppendLine($"- {optionType.Description}");
                    }
                    else
                    {
                        collector.AppendLine("");
                    }
                }
                collector.Indent = 0;
            }

            return(collector.ToString());
        }