Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show help for default command #953

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Spectre.Console.Cli/Internal/CommandExecutor.cs
Expand Up @@ -66,6 +66,14 @@ public async Task<int> Execute(IConfiguration configuration, IEnumerable<string>
return leaf.ShowHelp ? 0 : 1;
}

// Is this the default and is it called without arguments when there are required arguments?
if (leaf.Command.IsDefaultCommand && args.Count() == 0 && leaf.Command.Parameters.Any(p => p.Required))
{
// Display help for default command.
configuration.Settings.Console.SafeRender(HelpWriter.WriteCommand(model, leaf.Command));
return 1;
}

// Register the arguments with the container.
_registrar.RegisterInstance(typeof(IRemainingArguments), parsedResult.Remaining);

Expand Down
17 changes: 17 additions & 0 deletions test/Spectre.Console.Cli.Tests/Data/Commands/GreeterCommand.cs
@@ -0,0 +1,17 @@
using Spectre.Console;

public class GreeterCommand : Command<OptionalArgumentWithDefaultValueSettings>
{
private readonly IAnsiConsole _console;

public GreeterCommand(IAnsiConsole console)
{
_console = console;
}

public override int Execute([NotNull] CommandContext context, [NotNull] OptionalArgumentWithDefaultValueSettings settings)
{
_console.WriteLine(settings.Greeting);
return 0;
}
}
@@ -0,0 +1,16 @@
DESCRIPTION:
The lion command.

USAGE:
myapp <TEETH> [LEGS] [OPTIONS]

ARGUMENTS:
<TEETH> The number of teeth the lion has
[LEGS] The number of legs

OPTIONS:
-h, --help Prints help information
-a, --alive Indicates whether or not the animal is alive
-n, --name <VALUE>
--agility <VALUE> The agility between 0 and 100
-c <CHILDREN> The number of children the lion has
@@ -0,0 +1,19 @@
DESCRIPTION:
The lion command.

USAGE:
myapp <TEETH> [LEGS] [OPTIONS]

ARGUMENTS:
<TEETH> The number of teeth the lion has
[LEGS] The number of legs

OPTIONS:
-h, --help Prints help information
-a, --alive Indicates whether or not the animal is alive
-n, --name <VALUE>
--agility <VALUE> The agility between 0 and 100
-c <CHILDREN> The number of children the lion has

COMMANDS:
giraffe <LENGTH> The giraffe command
@@ -0,0 +1 @@
Hello World
11 changes: 11 additions & 0 deletions test/Spectre.Console.Cli.Tests/Spectre.Console.Cli.Tests.csproj
Expand Up @@ -29,4 +29,15 @@
<ProjectReference Include="..\..\src\Spectre.Console\Spectre.Console.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="Expectations\Help\Default_Without_Args_Additional.Output.verified.txt">
<ParentFile>$([System.String]::Copy('%(FileName)').Split('.')[0])</ParentFile>
<DependentUpon>%(ParentFile).cs</DependentUpon>
</None>
<None Update="Expectations\Help\Greeter_Default.Output.verified.txt">
<ParentFile>$([System.String]::Copy('%(FileName)').Split('.')[0])</ParentFile>
<DependentUpon>%(ParentFile).cs</DependentUpon>
</None>
</ItemGroup>

</Project>