コード例 #1
0
        private int?HandleHelpOption(CommandInput commandInput,
                                     IReadOnlyList <CommandSchema> availableCommandSchemas, CommandSchema targetCommandSchema)
        {
            // Help should be rendered if it was requested, or when executing a command which isn't defined
            var shouldRenderHelp = commandInput.IsHelpOptionSpecified() || targetCommandSchema == 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;

            // If target command isn't defined, find its contextual replacement
            if (targetCommandSchema == null)
            {
                // If command was specified, inform the user that it's not defined
                if (commandInput.IsCommandSpecified())
                {
                    _console.WithForegroundColor(ConsoleColor.Red,
                                                 () => _console.Error.WriteLine($"Specified command [{commandInput.CommandName}] is not defined."));

                    isError = true;
                }

                // Replace target command with closest parent of specified command
                targetCommandSchema = availableCommandSchemas.FindParent(commandInput.CommandName);

                // If there's no parent, replace with stub default command
                if (targetCommandSchema == null)
                {
                    targetCommandSchema     = CommandSchema.StubDefaultCommand;
                    availableCommandSchemas = availableCommandSchemas.Concat(CommandSchema.StubDefaultCommand).ToArray();
                }
            }

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

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

            // Short-circuit with appropriate exit code
            return(isError ? -1 : 0);
        }
コード例 #2
0
        private int?HandleVersionOption(CommandInput commandInput)
        {
            // Version should be rendered if it was requested on a default command
            var shouldRenderVersion = !commandInput.IsCommandSpecified() && 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);
        }