Cocona
Cocona copied to clipboard
Force Rendering Help?
Hi,
If I have a certain condition, I want to render the help message like:
if (condition == true)
{
string helpMessage = GenerateHelpMessage();
Console.Write(helpMessage);
return;
}
It's just a pseudo-code, but I'd like to know how to get the GenerateHelpMessage()
method. How can I do it?
Sorry, There is currently no easy way to display help messages. However, You can call Cocona's core services and build and output help messages as well as the built-in commands.
public void MyCommand(
[FromService]ICoconaAppContextAccessor appContext,
[FromService]ICoconaCommandProvider commandProvider,
[FromService]ICoconaHelpRenderer helpRenderer,
[FromService]ICoconaCommandHelpProvider commandHelpProvider
)
{
if (condition)
{
// from Cocona.Command.BuiltIn.BuiltInPrimaryCommand.ShowDefaultMessage method.
var commandStack = appContext.Current!.Features.Get<ICoconaCommandFeature>().CommandStack!;
var commandCollection = commandStack.LastOrDefault()?.SubCommands ?? commandProvider.GetCommandCollection();
Console.Write(helpRenderer.Render(commandHelpProvider.CreateCommandsIndexHelp(commandCollection, commandStack)));
return;
}
}
I can agree with your needs and would like to consider a different way to render the help or invoke the command (e.g. call BuiltInPrimaryCommand.ShowDefaultMessage
to render the help message).
Cocona v1.5.0 introduces an easy way to generate help messages. We hope you'll give it a try.
Thank you! Let me give it a try.
Errr... I'm using Cocona.Lite. Is it still OK to use this approach?
Of course, Cocona.Lite also supports it!