Skip to content

Commit 90ef25f

Browse files
committed
Introduce SectionEggsMLAttribute
1 parent 9e6b225 commit 90ef25f

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

External/RT.Util

Submodule RT.Util updated 48 files

Src/Attributes.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,17 @@ public sealed class SectionAttribute(string heading) : Attribute
146146
public string Heading { get; private set; } = heading;
147147
}
148148

149+
/// <summary>
150+
/// Adds a section header on the help screen above the option or parameter that has this attribute. The section heading is
151+
/// to be specified in <see cref="EggsML"/>, which is interpreted as described in <see
152+
/// cref="CommandLineParser.Colorize(EggsNode)"/>.</summary>
153+
[AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = true)]
154+
public sealed class SectionEggsMLAttribute(string heading) : Attribute
155+
{
156+
/// <summary>Specifies the section heading.</summary>
157+
public string Heading { get; private set; } = heading;
158+
}
159+
149160
/// <summary>
150161
/// Optionally use this on a class containing command-line options (either the main class or a class representing a
151162
/// subcommand) to specify some formatting parameters for the help screen.</summary>

Src/CommandLineParser.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ private static Func<int, ConsoleColoredString> getHelpGenerator(Type type, Func<
644644
//
645645

646646
var anyCommandsWithSuboptions = false;
647-
var paramsTables = new List<(string heading, TextTable table)>();
647+
var paramsTables = new List<(ConsoleColoredString heading, TextTable table)>();
648648
TextTable curTable = null;
649649
var curRow = 0;
650650
var lastMandatory = false;
@@ -653,11 +653,13 @@ private static Func<int, ConsoleColoredString> getHelpGenerator(Type type, Func<
653653
.Concat(optionalPositional.Select(fld => (mandatory: false, positional: true, field: fld)))
654654
.Concat(optionalOptions.Select(fld => (mandatory: false, positional: false, field: fld))))
655655
{
656-
var section = field.GetCustomAttribute<SectionAttribute>();
657-
if (curTable == null || lastMandatory != mandatory || section != null)
656+
var sectionHeading = (
657+
field.GetCustomAttribute<SectionAttribute>() is { } sec ? sec.Heading.Color(CmdLineColor.HelpHeading) :
658+
field.GetCustomAttribute<SectionEggsMLAttribute>() is { } egg ? Colorize(EggsML.Parse(egg.Heading)) : null);
659+
if (curTable == null || lastMandatory != mandatory || sectionHeading != null)
658660
{
659661
curTable = new TextTable { MaxWidth = wrapWidth - fmtOpt.LeftMargin, ColumnSpacing = fmtOpt.ColumnSpacing, RowSpacing = fmtOpt.RowSpacing, LeftMargin = fmtOpt.LeftMargin, StretchLastCell = true };
660-
paramsTables.Add((section?.Heading ?? $"{(mandatory ? "Required" : "Optional")} parameters:", curTable));
662+
paramsTables.Add((sectionHeading ?? $"{(mandatory ? "Required" : "Optional")} parameters:".Color(CmdLineColor.HelpHeading), curTable));
661663
curRow = 0;
662664
}
663665
anyCommandsWithSuboptions |= createParameterHelpRow(ref curRow, curTable, field, positional, helpProcessor);
@@ -668,7 +670,7 @@ private static Func<int, ConsoleColoredString> getHelpGenerator(Type type, Func<
668670
{
669671
for (var i = 0; i < fmtOpt.BlankLinesBeforeSection; i++)
670672
helpString.Add(ConsoleColoredString.NewLine);
671-
helpString.Add(new ConsoleColoredString(heading, CmdLineColor.HelpHeading));
673+
helpString.Add(heading);
672674
for (var i = -1; i < fmtOpt.BlankLinesAfterSection; i++)
673675
helpString.Add(ConsoleColoredString.NewLine);
674676
table.RemoveEmptyColumns();

0 commit comments

Comments
 (0)