public void Multiple_Context_Index()
        {
            // $ ./app-multiple --help
            var commandProvider       = new CoconaCommandProvider(new[] { typeof(TestCommands_Multiple) });
            var commandCollectionRoot = commandProvider.GetCommandCollection();
            var commandStack          = Array.Empty <CommandDescriptor>();

            var targetCommand     = BuiltInPrimaryCommand.GetCommand(string.Empty); // `TestCommands_Multiple` has no primary command. Use BuiltInPrimaryCommand.
            var commandCollection = commandCollectionRoot;

            var appContext = new CoconaAppContext(targetCommand, default);

            appContext.Features.Set <ICoconaCommandFeature>(new CoconaCommandFeature(commandCollection, targetCommand, commandStack, new object() /* unused */));
            var helpBuilder = new CoconaHelpMessageBuilder(
                new FakeAppContextAccessor(appContext),
                new FakeCommandHelpProvider(),
                new FakeHelpRenderer(),
                commandProvider
                );

            var help = helpBuilder.BuildAndRenderForCurrentContext();

            help.Should().Be("IndexHelp;Commands=Foo,Bar;SubCommandStack=");
        }
示例#2
0
        public HelpMessage CreateCommandsIndexHelp(CommandCollection commandCollection, IReadOnlyList <CommandDescriptor> subCommandStack)
        {
            var help = new HelpMessage();

            // Usage
            var usageSection     = new HelpSection(HelpSectionId.Usage);
            var subCommandParams = (subCommandStack.Count > 0) ? string.Join(" ", subCommandStack.Select(x => x.Name)) + " " : "";

            if (commandCollection.All.Count != 1)
            {
                usageSection.Children.Add(new HelpUsage($"Usage: {_applicationMetadataProvider.GetExecutableName()} {subCommandParams}[command]"));
            }
            if (commandCollection.Primary != null && (commandCollection.All.Count == 1 || commandCollection.Primary.Options.Any() || commandCollection.Primary.Arguments.Any()))
            {
                usageSection.Children.Add(new HelpUsage($"Usage: {CreateUsageCommandOptionsAndArgs(commandCollection.Primary, subCommandStack)}"));
            }
            help.Children.Add(usageSection);

            // Description
            var description = !string.IsNullOrWhiteSpace(commandCollection.Description)
                ? commandCollection.Description
                : !string.IsNullOrWhiteSpace(commandCollection.Primary?.Description)
                    ? commandCollection.Primary?.Description
                    : !string.IsNullOrWhiteSpace(_applicationMetadataProvider.GetDescription())
                        ? _applicationMetadataProvider.GetDescription()
                        : string.Empty;

            if (!string.IsNullOrWhiteSpace(description))
            {
                help.Children.Add(new HelpSection(HelpSectionId.Description, new HelpDescription(description !)));
            }

            // Commands
            var commandsExceptPrimary = commandCollection.All.Where(x => !x.IsPrimaryCommand && !x.IsHidden).ToArray();

            if (commandsExceptPrimary.Any())
            {
                help.Children.Add(new HelpSection(HelpSectionId.Commands,
                                                  new HelpHeading("Commands:"),
                                                  new HelpSection(
                                                      new HelpLabelDescriptionList(
                                                          commandsExceptPrimary
                                                          .Select((x, i) =>
                                                                  new HelpLabelDescriptionListItem(x.Name, x.Description)
                                                                  )
                                                          .ToArray()
                                                          )
                                                      )
                                                  ));
            }

            // Show helps for primary command.
            if (commandCollection.Primary != null)
            {
                // Arguments
                AddHelpForCommandArguments(help, commandCollection.Primary.Arguments);

                // Options
                AddHelpForCommandOptions(help, commandCollection.Primary.Options.OfType <ICommandOptionDescriptor>().Concat(commandCollection.Primary.OptionLikeCommands));
            }

            // Transform help document
            if (commandCollection.Primary != null)
            {
                var transformers = FilterHelper.GetFilters <ICoconaHelpTransformer>(commandCollection.Primary.Method, _serviceProvider);

                // TODO: This is ad-hoc workaround for default primary command.
                if (BuiltInPrimaryCommand.IsBuiltInCommand(commandCollection.Primary))
                {
                    transformers = commandCollection.All
                                   .Select(x => x.CommandType)
                                   .Distinct()
                                   .SelectMany(x => FilterHelper.GetFilters <ICoconaHelpTransformer>(x, _serviceProvider))
                                   .ToArray();
                }

                foreach (var transformer in transformers)
                {
                    transformer.TransformHelp(help, commandCollection.Primary);
                }
            }

            return(help);
        }