Add child command usage in help text (#77)

This commit is contained in:
Nikiforov Alexey
2020-10-22 16:02:58 +03:00
committed by GitHub
parent 6a38c04c11
commit c322b7029c

View File

@@ -105,31 +105,52 @@ namespace CliFx.Domain
WriteLine(); WriteLine();
} }
private void WriteCommandUsage(CommandSchema command, IReadOnlyList<CommandSchema> childCommands) private void WriteCommandUsage(
CommandSchema command,
IReadOnlyList<CommandSchema> childCommands)
{ {
if (!IsEmpty) if (!IsEmpty)
WriteVerticalMargin(); WriteVerticalMargin();
WriteHeader("Usage"); WriteHeader("Usage");
// Exe name WriteCommandUsageLineItem(command, childCommands.Count > 0);
WriteHorizontalMargin();
Write(_metadata.ExecutableName);
if (!IsEmpty)
WriteVerticalMargin();
foreach (var childCommand in childCommands)
{
WriteCommandUsageLineItem(childCommand, compactCommand: false, size: 4);
}
}
private void WriteCommandUsageLineItem(
CommandSchema command,
bool compactCommand = true,
int size = 2)
{
WriteHorizontalMargin(size);
if (compactCommand)
{
// Exe name
Write(_metadata.ExecutableName);
Write(' ');
}
// Command name // Command name
if (!string.IsNullOrWhiteSpace(command.Name)) if (!string.IsNullOrWhiteSpace(command.Name))
{ {
Write(' '); // this is fragile, because we rely that subcommand name consists
// of all required tokens
Write(ConsoleColor.Cyan, command.Name); Write(ConsoleColor.Cyan, command.Name);
} }
// Child command placeholder // Child command placeholder
if (childCommands.Any()) if (compactCommand)
{ {
Write(' '); Write(' ');
Write(ConsoleColor.Cyan, "[command]"); Write(ConsoleColor.Cyan, "[command]");
} }
// Parameters // Parameters
foreach (var parameter in command.Parameters) foreach (var parameter in command.Parameters)
{ {
@@ -334,7 +355,9 @@ namespace CliFx.Domain
CommandSchema command, CommandSchema command,
IReadOnlyDictionary<CommandArgumentSchema, object?> defaultValues) IReadOnlyDictionary<CommandArgumentSchema, object?> defaultValues)
{ {
var childCommands = root.GetChildCommands(command.Name); var commandName = command.Name;
var childCommands = root.GetChildCommands(commandName);
var descendantCommands = root.GetDescendantCommands(commandName);
_console.ResetColor(); _console.ResetColor();
@@ -342,7 +365,7 @@ namespace CliFx.Domain
WriteApplicationInfo(); WriteApplicationInfo();
WriteCommandDescription(command); WriteCommandDescription(command);
WriteCommandUsage(command, childCommands); WriteCommandUsage(command, descendantCommands);
WriteCommandParameters(command); WriteCommandParameters(command);
WriteCommandOptions(command, defaultValues); WriteCommandOptions(command, defaultValues);
WriteCommandChildren(command, childCommands); WriteCommandChildren(command, childCommands);
@@ -385,4 +408,4 @@ namespace CliFx.Domain
} }
} }
} }
} }