예제 #1
0
        public override ValueTask <int> DispatchAsync(CommandDispatchContext ctx)
        {
            var hasHelpOption = ctx.ParsedCommandLine.Options.Any(x => x.Option == BuiltInCommandOption.Help);

            if (hasHelpOption)
            {
                var feature           = _appContext.Current !.Features.Get <ICoconaCommandFeature>() !;
                var commandCollection = feature.CommandCollection ?? _commandProvider.GetCommandCollection();

                var help = (ctx.Command.IsPrimaryCommand)
                    ? _commandHelpProvider.CreateCommandsIndexHelp(commandCollection, feature.CommandStack)
                    : _commandHelpProvider.CreateCommandHelp(ctx.Command, feature.CommandStack);

                _console.Output.Write(_helpRenderer.Render(help));
                return(new ValueTask <int>(129));
            }

            var hasVersionOption = ctx.ParsedCommandLine.Options.Any(x => x.Option == BuiltInCommandOption.Version);

            if (hasVersionOption)
            {
                _console.Output.Write(_helpRenderer.Render(_commandHelpProvider.CreateVersionHelp()));
                return(new ValueTask <int>(0));
            }

            return(Next(ctx));
        }
예제 #2
0
        private HelpMessage BuildFromCurrentContextCore(bool respectCurrentCommand)
        {
            var feature           = _appContext.Current !.Features.Get <ICoconaCommandFeature>() !;
            var commandCollection = feature.CommandCollection ?? _commandProvider.GetCommandCollection(); // nested or root

            // If `respectCurrentCommand` is `true`, treats the current command as a target.
            // When called by `--help`, the original command is put on the CommandStack.
            // When directly call the method, the CommandStack may be empty.
            var targetCommand = respectCurrentCommand ? feature.Command : feature.CommandStack.LastOrDefault();

            var help = targetCommand is null
                ? _commandHelpProvider.CreateCommandsIndexHelp(commandCollection, Array.Empty <CommandDescriptor>())
                : targetCommand.IsPrimaryCommand || targetCommand.Flags.HasFlag(CommandFlags.SubCommandsEntryPoint)
                    ? _commandHelpProvider.CreateCommandsIndexHelp(commandCollection, feature.CommandStack.Take(feature.CommandStack.Count - 1).ToArray())
                    : _commandHelpProvider.CreateCommandHelp(targetCommand, feature.CommandStack.Take(feature.CommandStack.Count - 1).ToArray());

            return(help);
        }
예제 #3
0
        public override ValueTask <int> DispatchAsync(CommandDispatchContext ctx)
        {
            var hasHelpOption = ctx.ParsedCommandLine.Options.Any(x => x.Option == BuiltInCommandOption.Help);

            if (hasHelpOption)
            {
                var help = (ctx.Command.IsPrimaryCommand)
                    ? _commandHelpProvider.CreateCommandsIndexHelp(_commandProvider.GetCommandCollection())
                    : _commandHelpProvider.CreateCommandHelp(ctx.Command);

                _console.Output.Write(_helpRenderer.Render(help));
                return(new ValueTask <int>(129));
            }

            var hasVersionOption = ctx.ParsedCommandLine.Options.Any(x => x.Option == BuiltInCommandOption.Version);

            if (hasVersionOption)
            {
                _console.Output.Write(_helpRenderer.Render(_commandHelpProvider.CreateVersionHelp()));
                return(new ValueTask <int>(0));
            }

            return(Next(ctx));
        }