Exemplo n.º 1
0
        private int?HandleVersionOption(CommandInput commandInput)
        {
            // Version should be rendered if it was requested on a default command
            var shouldRenderVersion = !commandInput.HasArguments() && commandInput.IsVersionOptionSpecified();

            // If shouldn't render version, pass execution to the next handler
            if (!shouldRenderVersion)
            {
                return(null);
            }

            // Render version text
            _console.Output.WriteLine(_metadata.VersionText);

            // Short-circuit with exit code 0
            return(0);
        }
Exemplo n.º 2
0
        private int?HandleHelpOption(CommandInput commandInput,
                                     IReadOnlyList <CommandSchema> availableCommandSchemas, CommandCandidate?commandCandidate)
        {
            // Help should be rendered if it was requested, or when executing a command which isn't defined
            var shouldRenderHelp = commandInput.IsHelpOptionSpecified() || commandCandidate == null;

            // If shouldn't render help, pass execution to the next handler
            if (!shouldRenderHelp)
            {
                return(null);
            }

            // Keep track whether there was an error in the input
            var isError = false;

            // Report error if no command matched the arguments
            if (commandCandidate is null)
            {
                // If a command was specified, inform the user that the command is not defined
                if (commandInput.HasArguments())
                {
                    _console.WithForegroundColor(ConsoleColor.Red,
                                                 () => _console.Error.WriteLine($"No command could be matched for input [{string.Join(" ", commandInput.Arguments)}]"));
                    isError = true;
                }

                commandCandidate = new CommandCandidate(CommandSchema.StubDefaultCommand, new string[0], commandInput);
            }

            // Build help text source
            var helpTextSource = new HelpTextSource(_metadata, availableCommandSchemas, commandCandidate.Schema);

            // Render help text
            _helpTextRenderer.RenderHelpText(_console, helpTextSource);

            // Short-circuit with appropriate exit code
            return(isError ? -1 : 0);
        }