Skip to content

Commit 51bdea1

Browse files
committed
Add a HelpScreenFormattingAttribute to make the help screen formatting (esp. spacing) more customizable.
1 parent b663318 commit 51bdea1

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

Src/Attributes.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,21 @@ public sealed class SectionAttribute(string heading) : Attribute
142142
/// <summary>Specifies the section heading.</summary>
143143
public string Heading { get; private set; } = heading;
144144
}
145+
146+
/// <summary>
147+
/// Optionally use this on a class containing command-line options (either the main class or a class representing a
148+
/// subcommand) to specify some formatting parameters for the help screen.</summary>
149+
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false), RummageKeepUsersReflectionSafe]
150+
public sealed class HelpScreenFormattingAttribute : Attribute
151+
{
152+
/// <summary>Specifies the amount of horizontal separation between the columns in the table(s).</summary>
153+
public int ColumnSpacing { get; set; } = 3;
154+
/// <summary>Specifies the amount of blank lines between the rows in the table(s).</summary>
155+
public int RowSpacing { get; set; } = 1;
156+
/// <summary>Specifies the amount of blank lines before a section header.</summary>
157+
public int BlankLinesBeforeSection { get; set; } = 1;
158+
/// <summary>Specifies the amount of blank lines after a section header.</summary>
159+
public int BlankLinesAfterSection { get; set; } = 1;
160+
/// <summary>Specifies the amount of margin to the left of the table(s).</summary>
161+
public int LeftMargin { get; set; } = 3;
162+
}

Src/CommandLineParser.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -589,11 +589,10 @@ private static Func<int, ConsoleColoredString> getHelpGenerator(Type type, Func<
589589
helpProcessor ??= (s => s);
590590
return wrapWidth =>
591591
{
592-
int leftMargin = 3;
593-
594592
var helpString = new List<ConsoleColoredString>();
595593
var commandNameAttr = type.GetCustomAttributes<CommandNameAttribute>().FirstOrDefault();
596594
string commandName = commandNameAttr == null ? Process.GetCurrentProcess().ProcessName : "... " + commandNameAttr.Names.OrderByDescending(c => c.Length).First();
595+
var fmtOpt = type.GetCustomAttribute<HelpScreenFormattingAttribute>();
597596

598597
//
599598
// ## CONSTRUCT THE “USAGE” LINE
@@ -651,7 +650,7 @@ private static Func<int, ConsoleColoredString> getHelpGenerator(Type type, Func<
651650
var section = field.GetCustomAttribute<SectionAttribute>();
652651
if (curTable == null || lastMandatory != mandatory || section != null)
653652
{
654-
curTable = new TextTable { MaxWidth = wrapWidth - leftMargin, ColumnSpacing = 3, RowSpacing = 1, LeftMargin = leftMargin };
653+
curTable = new TextTable { MaxWidth = wrapWidth - fmtOpt.LeftMargin, ColumnSpacing = fmtOpt.ColumnSpacing, RowSpacing = fmtOpt.RowSpacing, LeftMargin = fmtOpt.LeftMargin };
655654
paramsTables.Add((section?.Heading ?? $"{(mandatory ? "Required" : "Optional")} parameters:", curTable));
656655
curRow = 0;
657656
}
@@ -661,10 +660,11 @@ private static Func<int, ConsoleColoredString> getHelpGenerator(Type type, Func<
661660

662661
foreach (var (heading, table) in paramsTables)
663662
{
664-
helpString.Add(ConsoleColoredString.NewLine);
663+
for (var i = 0; i < fmtOpt.BlankLinesBeforeSection; i++)
664+
helpString.Add(ConsoleColoredString.NewLine);
665665
helpString.Add(new ConsoleColoredString(heading, CmdLineColor.HelpHeading));
666-
helpString.Add(ConsoleColoredString.NewLine);
667-
helpString.Add(ConsoleColoredString.NewLine);
666+
for (var i = -1; i < fmtOpt.BlankLinesAfterSection; i++)
667+
helpString.Add(ConsoleColoredString.NewLine);
668668
table.RemoveEmptyColumns();
669669
helpString.Add(table.ToColoredString());
670670
}
@@ -680,7 +680,7 @@ private static Func<int, ConsoleColoredString> getHelpGenerator(Type type, Func<
680680
}
681681
}
682682

683-
return new ConsoleColoredString(helpString.ToArray());
683+
return new ConsoleColoredString(helpString);
684684
};
685685
}
686686

0 commit comments

Comments
 (0)