Cocona icon indicating copy to clipboard operation
Cocona copied to clipboard

Force Rendering Help?

Open justinyoo opened this issue 4 years ago • 5 comments

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?

justinyoo avatar Jul 09 '20 12:07 justinyoo

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).

mayuki avatar Jul 10 '20 17:07 mayuki

Cocona v1.5.0 introduces an easy way to generate help messages. We hope you'll give it a try.

mayuki avatar Jul 12 '20 09:07 mayuki

Thank you! Let me give it a try.

justinyoo avatar Jul 17 '20 10:07 justinyoo

Errr... I'm using Cocona.Lite. Is it still OK to use this approach?

justinyoo avatar Jul 17 '20 10:07 justinyoo

Of course, Cocona.Lite also supports it!

mayuki avatar Jul 17 '20 17:07 mayuki