Esempio n. 1
0
        private IReadOnlyList <Section> GenerateSections(ArgumentSetUsageInfo info)
        {
            var sections = new List <Section>();

            if (_options.Logo?.Include ?? false &&
                info.Logo != null && !string.IsNullOrEmpty(info.Logo))
            {
                sections.Add(new Section(ArgumentSetHelpSectionType.Logo, _options, _options.Logo, info.Logo));
            }

            // Append basic usage info.
            if (_options.Syntax?.Include ?? false)
            {
                var basicSyntax = new List <ColoredString>();

                if (!string.IsNullOrEmpty(_options.Name))
                {
                    basicSyntax.Add(new ColoredString(_options.Name, _options.Syntax?.CommandNameColor));
                    basicSyntax.Add(" ");
                }

                basicSyntax.Add(info.GetBasicSyntax(_options.Syntax));

                sections.Add(new Section(ArgumentSetHelpSectionType.Syntax, _options, _options.Syntax, new[] { new ColoredMultistring(basicSyntax) }));
            }

            if ((_options.Description?.Include ?? false) && !string.IsNullOrEmpty(info.Description))
            {
                sections.Add(new Section(ArgumentSetHelpSectionType.ArgumentSetDescription, _options, _options.Description, info.Description));
            }

            // If needed, get help info for enum values.
            IReadOnlyDictionary <ArgumentUsageInfo, List <IEnumArgumentType> > inlineDocumented = null;
            IEnumerable <IEnumArgumentType> separatelyDocumented = null;

            if (_options.EnumValues?.Include ?? false)
            {
                GetEnumsToDocument(info, out inlineDocumented, out separatelyDocumented);
            }

            // Process parameters sections.
            var includeRequiredArgs = _options.Arguments?.RequiredArguments?.Include ?? false;
            var includeOptionalArgs = _options.Arguments?.OptionalArguments?.Include ?? false;

            if (includeRequiredArgs || includeOptionalArgs)
            {
                var argBlockIndentWidth = 0;
                if (includeRequiredArgs)
                {
                    argBlockIndentWidth = Math.Max(argBlockIndentWidth, _options.Arguments.RequiredArguments.BlockIndent.GetValueOrDefault(_options.SectionEntryBlockIndentWidth));
                }

                if (includeOptionalArgs)
                {
                    argBlockIndentWidth = Math.Max(argBlockIndentWidth, _options.Arguments.OptionalArguments.BlockIndent.GetValueOrDefault(_options.SectionEntryBlockIndentWidth));
                }

                var currentMaxWidth =
                    _options.MaxWidth.GetValueOrDefault(ArgumentSetHelpOptions.DefaultMaxWidth) - argBlockIndentWidth;

                var requiredEntries = includeRequiredArgs ?
                                      (IReadOnlyList <ParameterEntry>)GetParameterEntries(currentMaxWidth, info.RequiredParameters, info, inlineDocumented).ToList() :
                                      Array.Empty <ParameterEntry>();

                var optionalEntries = includeOptionalArgs ?
                                      (IReadOnlyList <ParameterEntry>)GetParameterEntries(currentMaxWidth, info.OptionalParameters, info, inlineDocumented).ToList() :
                                      Array.Empty <ParameterEntry>();

                if (requiredEntries.Count + optionalEntries.Count > 0)
                {
                    var maxLengthOfParameterSyntax = requiredEntries.Concat(optionalEntries).Max(e => e.Syntax.Length);

                    if (requiredEntries.Count > 0)
                    {
                        var formattedEntries = FormatParameterEntries(currentMaxWidth, requiredEntries, maxLengthOfParameterSyntax).ToList();
                        sections.Add(new Section(ArgumentSetHelpSectionType.RequiredParameters, _options, _options.Arguments.RequiredArguments, formattedEntries));
                    }

                    if (optionalEntries.Count > 0)
                    {
                        var formattedEntries = FormatParameterEntries(currentMaxWidth, optionalEntries, maxLengthOfParameterSyntax).ToList();
                        sections.Add(new Section(ArgumentSetHelpSectionType.OptionalParameters, _options, _options.Arguments.OptionalArguments, formattedEntries));
                    }
                }
            }

            // If needed, provide help for shared enum values.
            if (separatelyDocumented != null)
            {
                foreach (var enumType in separatelyDocumented)
                {
                    var maxWidth = _options.MaxWidth.GetValueOrDefault(ArgumentSetHelpOptions.DefaultMaxWidth);
                    maxWidth -= _options.EnumValues.BlockIndent.GetValueOrDefault(_options.SectionEntryBlockIndentWidth);

                    var enumValueEntries = GetEnumValueEntries(maxWidth, enumType).ToList();
                    if (enumValueEntries.Count > 0)
                    {
                        sections.Add(new Section(ArgumentSetHelpSectionType.EnumValues, _options,
                                                 _options.EnumValues,
                                                 enumValueEntries,
                                                 name: string.Format(Strings.UsageInfoEnumValueHeaderFormat, enumType.DisplayName)));
                    }
                }
            }

            // If present, append "EXAMPLES" section.
            if ((_options.Examples?.Include ?? false) && info.Examples.Any())
            {
                sections.Add(new Section(ArgumentSetHelpSectionType.Examples, _options, _options.Examples, info.Examples));
            }

            return(sections);
        }