Skip to content

Commit daf751f

Browse files
vookimedloMatthiee
authored andcommitted
Usage output fixed (#33)
* Usage output fixed - options and commands descriptions shifting should be based on the longest one * Usage output fixed - part2 * Usage fix: review comments addressed * CodeFactor finding solved
1 parent 38d0b89 commit daf751f

2 files changed

Lines changed: 28 additions & 13 deletions

File tree

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);
11-
void PrintOption(ICommandLineOption option);
12-
void PrintCommandDescriptions(IEnumerable<ICommandLineCommand> commands);
13-
void PrintCommandDescription(ICommandLineCommand command);
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);
1414
void PrintCommand(string name, ICommandLineCommandContainer container);
1515
}
1616
}

CommandLineParser/Core/Usage/UsageBuilder.cs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ internal class UsageBuilder : IUsageBuilder
1414
{
1515
private readonly StringBuilder stringBuilder = new StringBuilder();
1616
private readonly CommandLineParserOptions parserOptions;
17+
private readonly string optionSeparator = "|";
1718

1819
public UsageBuilder(CommandLineParserOptions parserOptions)
1920
=> this.parserOptions = parserOptions;
@@ -46,40 +47,54 @@ public void PrintCommand(string name, ICommandLineCommandContainer container)
4647
PrintCommandDescriptions(container.Commands);
4748
}
4849

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

52-
public void PrintCommandDescriptions(IEnumerable<ICommandLineCommand> commands)
53+
public void PrintCommandDescriptions(IEnumerable<ICommandLineCommand> commands, int descriptionShift = 4)
5354
{
5455
if (!commands.Any()) return;
5556

5657
stringBuilder.AppendLine().AppendLine("Commands: ");
5758

59+
var longestCommandName = commands.Max(x => x.Name.Length);
5860
foreach (var cmd in commands)
59-
PrintCommandDescription(cmd);
61+
PrintCommandDescription(cmd, longestCommandName - cmd.Name.Length + descriptionShift);
6062
}
6163

62-
public void PrintOption(ICommandLineOption option)
64+
public void PrintOption(ICommandLineOption option, int descriptionShift = 4, bool compensateSeparator = false)
6365
{
6466
bool hasShort = option.HasShortName;
6567
bool hasLong = option.HasLongName;
6668
bool hasBoth = hasShort && hasLong;
6769

68-
string hasBothSeperator = hasBoth ? "|" : string.Empty;
70+
string hasBothSeparator = hasBoth ? optionSeparator : string.Empty;
6971
string shortName = hasShort ? option.ShortName : string.Empty;
7072
string longName = hasLong ? option.LongName : string.Empty;
7173

72-
stringBuilder.AppendLine($" {shortName}{hasBothSeperator}{longName}\t{option.Description}");
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);
77+
78+
stringBuilder.AppendLine($" {shortName}{hasBothSeparator}{longName}{indentation}{option.Description}");
7379
}
7480

75-
public void PrintOptions(IEnumerable<ICommandLineOption> options)
81+
public void PrintOptions(IEnumerable<ICommandLineOption> options, int descriptionShift = 4)
7682
{
7783
if (!options.Any()) return;
7884

7985
stringBuilder.AppendLine().AppendLine("Options: ");
8086

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+
8190
foreach (var opt in options)
82-
PrintOption(opt);
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);
97+
}
8398
}
8499
}
85100
}

0 commit comments

Comments
 (0)