Skip to content

Commit f3dfdb3

Browse files
authored
Add alignment component to UsageBuilder, fixes #32 (#46)
1 parent 52e2c38 commit f3dfdb3

File tree

4 files changed

+74
-29
lines changed

4 files changed

+74
-29
lines changed

CommandLineParser.Tests/Usage/UsagePrinterTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public void CustomInvokedPrinterWorksCorrectly(string[] args, bool cmdPassed, bo
101101

102102
builderMock.Verify(mock => mock.Print(), Times.Never());
103103
builderMock.Verify(mock => mock.PrintCommand(It.IsAny<string>(), It.IsAny<ICommandLineCommandContainer>()), Times.Never());
104-
builderMock.Verify(mock => mock.PrintOption(It.IsAny<ICommandLineOption>(), It.IsAny<int>(), It.IsAny<bool>()), Times.Never());
104+
builderMock.Verify(mock => mock.PrintOption(It.IsAny<ICommandLineOption>()), Times.Never());
105105

106106
if (result.HelpRequested)
107107
parser.Printer.PrintUsage(result.HelpRequestedFor);
@@ -125,7 +125,7 @@ public void CustomInvokedPrinterWorksCorrectly(string[] args, bool cmdPassed, bo
125125
ToTimes(cmdPassed));
126126

127127
builderMock.Verify(
128-
mock => mock.PrintOption(It.IsAny<ICommandLineOption>(), It.IsAny<int>(), It.IsAny<bool>()),
128+
mock => mock.PrintOption(It.IsAny<ICommandLineOption>()),
129129
ToTimes(optPassed));
130130
}
131131

CommandLineParser/Abstractions/Usage/IUsageBuilder.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ public interface IUsageBuilder
77
{
88
void Print();
99
void PrintUsage(string name, bool hasOptions, bool hasCommands);
10-
void PrintOptions(IEnumerable<ICommandLineOption> options, int descriptionShift = 4);
11-
void PrintOption(ICommandLineOption option, int descriptionShift = 4, bool compensateSeparator = false);
12-
void PrintCommandDescriptions(IEnumerable<ICommandLineCommand> commands, int descriptionShift = 4);
13-
void PrintCommandDescription(ICommandLineCommand command, int descriptionShift = 4);
10+
void PrintOptions(IEnumerable<ICommandLineOption> options);
11+
void PrintOption(ICommandLineOption option);
12+
void PrintCommandDescriptions(IEnumerable<ICommandLineCommand> commands);
13+
void PrintCommandDescription(ICommandLineCommand command);
1414
void PrintCommand(string name, ICommandLineCommandContainer container);
1515
}
1616
}

CommandLineParser/CommandLineParser.xml

Lines changed: 58 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CommandLineParser/Core/Usage/UsageBuilder.cs

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,20 @@ public void PrintCommand(string name, ICommandLineCommandContainer container)
4747
PrintCommandDescriptions(container.Commands);
4848
}
4949

50-
public void PrintCommandDescription(ICommandLineCommand command, int descriptionShift = 4)
51-
=> stringBuilder.AppendLine($" {command.Name}{new string(' ', descriptionShift)}{command.Description}");
50+
public void PrintCommandDescription(ICommandLineCommand command)
51+
=> stringBuilder.AppendLine($" {command.Name,-20}{command.Description,-50}");
5252

53-
public void PrintCommandDescriptions(IEnumerable<ICommandLineCommand> commands, int descriptionShift = 4)
53+
public void PrintCommandDescriptions(IEnumerable<ICommandLineCommand> commands)
5454
{
5555
if (!commands.Any()) return;
5656

5757
stringBuilder.AppendLine().AppendLine("Commands: ");
5858

59-
var longestCommandName = commands.Max(x => x.Name.Length);
6059
foreach (var cmd in commands)
61-
PrintCommandDescription(cmd, longestCommandName - cmd.Name.Length + descriptionShift);
60+
PrintCommandDescription(cmd);
6261
}
6362

64-
public void PrintOption(ICommandLineOption option, int descriptionShift = 4, bool compensateSeparator = false)
63+
public void PrintOption(ICommandLineOption option)
6564
{
6665
bool hasShort = option.HasShortName;
6766
bool hasLong = option.HasLongName;
@@ -71,29 +70,20 @@ public void PrintOption(ICommandLineOption option, int descriptionShift = 4, boo
7170
string shortName = hasShort ? option.ShortName : string.Empty;
7271
string longName = hasLong ? option.LongName : string.Empty;
7372

74-
// We neeed to compensate a separator if given option doesn't have both (short & long) names.
75-
int indentationLength = descriptionShift + ((compensateSeparator && !hasBoth) ? optionSeparator.Length : 0);
76-
string indentation = new string(' ', indentationLength);
73+
string key = $"{shortName}{hasBothSeparator}{longName}";
7774

78-
stringBuilder.AppendLine($" {shortName}{hasBothSeparator}{longName}{indentation}{option.Description}");
75+
stringBuilder.AppendLine($" {key,-20}{option.Description,-50}");
7976
}
8077

81-
public void PrintOptions(IEnumerable<ICommandLineOption> options, int descriptionShift = 4)
78+
public void PrintOptions(IEnumerable<ICommandLineOption> options)
8279
{
8380
if (!options.Any()) return;
8481

8582
stringBuilder.AppendLine().AppendLine("Options: ");
8683

87-
var longestOptionName = options.Max(x => (x.HasShortName ? x.ShortName.Length : 0) + (x.HasLongName ? x.LongName.Length : 0));
88-
var compensateSeparator = options.Any(x => x.HasShortName && x.HasLongName);
89-
9084
foreach (var opt in options)
91-
{
92-
var longNameLength = opt.HasLongName ? opt.LongName.Length : 0;
93-
var shortNameLength = opt.HasShortName ? opt.ShortName.Length : 0;
94-
descriptionShift = longestOptionName - longNameLength - shortNameLength + descriptionShift;
95-
96-
PrintOption(opt, descriptionShift, compensateSeparator);
85+
{
86+
PrintOption(opt);
9787
}
9888
}
9989
}

0 commit comments

Comments
 (0)