예제 #1
0
 private static CommandWithStructuredInputBase <HttpState, ICoreParseResult> GetStructuredCommand(ICommand <HttpState, ICoreParseResult> rawCommand)
 {
     return(rawCommand switch
     {
         CommandWithStructuredInputBase <HttpState, ICoreParseResult> structuredCommand => structuredCommand,
         TelemetryCommandWrapper telemetryWrapper when telemetryWrapper.Command is CommandWithStructuredInputBase <HttpState, ICoreParseResult> structuredCommand => structuredCommand,
         _ => null
     });
예제 #2
0
        public Task ExecuteAsync(IShellState shellState, HttpState programState, ICoreParseResult parseResult, CancellationToken cancellationToken)
        {
            shellState = shellState ?? throw new ArgumentNullException(nameof(shellState));

            parseResult = parseResult ?? throw new ArgumentNullException(nameof(parseResult));

            programState = programState ?? throw new ArgumentNullException(nameof(programState));

            if (shellState.CommandDispatcher is ICommandDispatcher <HttpState, ICoreParseResult> dispatcher)
            {
                if (parseResult.Sections.Count == 1)
                {
                    CoreGetHelp(shellState, dispatcher, programState);
                }
                else
                {
                    bool          anyHelp = false;
                    StringBuilder output  = new StringBuilder();

                    if (parseResult.Slice(1) is ICoreParseResult continuationParseResult)
                    {
                        foreach (ICommand <HttpState, ICoreParseResult> command in dispatcher.Commands)
                        {
                            string help = command.GetHelpDetails(shellState, programState, continuationParseResult);

                            if (!string.IsNullOrEmpty(help))
                            {
                                anyHelp = true;
                                output.AppendLine();
                                output.AppendLine(help);

                                CommandWithStructuredInputBase <HttpState, ICoreParseResult> structuredCommand = GetStructuredCommand(command);
                                if (structuredCommand is not null && structuredCommand.InputSpec.Options.Any())
                                {
                                    output.AppendLine();
                                    output.AppendLine(Strings.Options.Bold());
                                    foreach (CommandOptionSpecification option in structuredCommand.InputSpec.Options)
                                    {
                                        string optionText = string.Empty;
                                        foreach (string form in option.Forms)
                                        {
                                            if (!string.IsNullOrEmpty(optionText))
                                            {
                                                optionText += "|";
                                            }
                                            optionText += form;
                                        }
                                        output.AppendLine($"    {optionText}");
                                    }
                                }

                                break;
                            }
                        }
                    }

                    if (!anyHelp)
                    {
                        output.AppendLine(Strings.HelpCommand_Error_UnableToLocateHelpInfo);
                    }

                    shellState.ConsoleManager.Write(output.ToString());
                }
            }

            return(Task.CompletedTask);
        }