Пример #1
0
        /// <summary>
        /// Writes the usage summary for the supplied <see cref="command"/>
        /// </summary>
        /// <param name="command"></param>
        protected virtual void AddUsage(ICommand command)
        {
            var usage = new List <string>();

            var subcommands = command
                              .RecurseWhileNotNull(c => c.Parent as ICommand)
                              .Reverse();

            foreach (var subcommand in subcommands)
            {
                usage.Add(subcommand.Name);

                if (subcommand != command)
                {
                    foreach (var argument in subcommand.Arguments)
                    {
                        if (argument.ShouldShowHelp())
                        {
                            usage.Add($"<{argument.Name}>");
                        }
                    }
                }
            }

            var hasOptionHelp = command.Children
                                .OfType <IOption>()
                                .Any(option => option.ShouldShowHelp());

            if (hasOptionHelp)
            {
                usage.Add(Usage.Options);
            }

            foreach (var argument in command.Arguments)
            {
                if (argument.ShouldShowHelp())
                {
                    usage.Add($"<{argument.Name}>");
                }
            }

            var hasCommandHelp = command.Children
                                 .OfType <ICommand>()
                                 .Any(f => f.ShouldShowHelp());

            if (hasCommandHelp)
            {
                usage.Add(Usage.Command);
            }

            if (!command.TreatUnmatchedTokensAsErrors)
            {
                usage.Add(Usage.AdditionalArguments);
            }

            HelpSection.Write(this, Usage.Title, string.Join(" ", usage));
        }
Пример #2
0
        protected virtual void AddAdditionalArguments(ICommand command)
        {
            if (command.TreatUnmatchedTokensAsErrors)
            {
                return;
            }

            HelpSection.Write(this, AdditionalArguments.Title, AdditionalArguments.Description);
        }
Пример #3
0
        /// <summary>
        /// Writes the usage summary for the supplied <see cref="command"/>
        /// </summary>
        /// <param name="command"></param>
        protected virtual void AddUsage(ICommand command)
        {
            var usage = new List <string>();

            IEnumerable <ICommand> subcommands;

            if (command is Command cmd)
            {
                subcommands = cmd
                              .RecurseWhileNotNull(c => c.Parents
                                                   .OfType <Command>()
                                                   .FirstOrDefault())
                              .Reverse();
            }
            else
            {
                subcommands = Enumerable.Empty <ICommand>();
            }

            foreach (var subcommand in subcommands)
            {
                usage.Add(subcommand.Name);

                if (subcommand != command)
                {
                    usage.Add(FormatArgumentUsage(subcommand.Arguments.ToArray()));
                }
            }

            var hasOptionHelp = command.Children
                                .OfType <IOption>()
                                .Any(ShouldShowHelp);

            if (hasOptionHelp)
            {
                usage.Add(Usage.Options);
            }

            usage.Add(FormatArgumentUsage(command.Arguments.ToArray()));

            var hasCommandHelp = command.Children
                                 .OfType <ICommand>()
                                 .Any(ShouldShowHelp);

            if (hasCommandHelp)
            {
                usage.Add(Usage.Command);
            }

            if (!command.TreatUnmatchedTokensAsErrors)
            {
                usage.Add(Usage.AdditionalArguments);
            }

            HelpSection.Write(this, Usage.Title, string.Join(" ", usage.Where(u => !string.IsNullOrWhiteSpace(u))));
        }
Пример #4
0
        /// <summary>
        /// Writes the help content of the <see cref="Command"/> subcommands, if any,
        /// for the supplied <see cref="command"/>
        /// </summary>
        /// <param name="command"></param>
        protected virtual void AddSubcommands(ICommand command)
        {
            var subcommands = command
                              .Children
                              .OfType <ICommand>()
                              .Where(ShouldShowHelp)
                              .ToArray();

            HelpSection.Write(this, Commands.Title, subcommands, GetOptionHelpItems);
        }
Пример #5
0
        /// <summary>
        /// Writes the <see cref="Option"/> help content, if any,
        /// for the supplied <see cref="command"/>
        /// </summary>
        /// <param name="command"></param>
        protected virtual void AddOptions(ICommand command)
        {
            var options = command
                          .Children
                          .OfType <IOption>()
                          .Where(ShouldShowHelp)
                          .ToArray();

            HelpSection.Write(this, Options.Title, options, GetOptionHelpItems);
        }
Пример #6
0
        /// <summary>
        /// Writes the help content of the <see cref="Command"/> subcommands, if any,
        /// for the supplied <see cref="command"/>
        /// </summary>
        /// <param name="command"></param>
        protected virtual void AddSubcommands(ICommand command)
        {
            var subcommands = command
                              .Children
                              .OfType <ICommand>()
                              .Where(subCommand => subCommand.ShouldShowHelp())
                              .ToArray();

            HelpSection.Write(this, Commands.Title, subcommands, OptionFormatter);
        }
Пример #7
0
        /// <summary>
        /// Writes the <see cref="Option"/> help content, if any,
        /// for the supplied <see cref="command"/>
        /// </summary>
        /// <param name="command"></param>
        protected virtual void AddOptions(ICommand command)
        {
            var options = command
                          .Children
                          .OfType <IOption>()
                          .Where(opt => opt.ShouldShowHelp())
                          .ToArray();

            HelpSection.Write(this, Options.Title, options, OptionFormatter);
        }
Пример #8
0
        /// <summary>
        /// Writes a summary, if configured, for the supplied <see cref="command"/>
        /// </summary>
        /// <param name="command"></param>
        protected virtual void AddSynopsis(ICommand command)
        {
            if (!ShouldShowHelp(command))
            {
                return;
            }

            var title = $"{command.Name}:";

            HelpSection.Write(this, title, command.Description);
        }
Пример #9
0
        /// <summary>
        /// Writes the usage summary for the supplied <see cref="command"/>
        /// </summary>
        /// <param name="command"></param>
        protected virtual void AddUsage(ICommand command)
        {
            var usage = new List <string>();

            var subcommands = command
                              .RecurseWhileNotNull(commandDef => commandDef.Parent)
                              .Reverse();

            foreach (var subcommand in subcommands)
            {
                usage.Add(subcommand.Name);

                if (subcommand != command &&
                    ShouldDisplayArgumentHelp(subcommand, out var subcommandArgName))
                {
                    usage.Add($"<{subcommandArgName}>");
                }
            }

            var hasOptionHelp = command.Children
                                .OfType <IOption>()
                                .Any(symbolDef => symbolDef.ShouldShowHelp());

            if (hasOptionHelp)
            {
                usage.Add(Usage.Options);
            }

            if (ShouldDisplayArgumentHelp(command, out var commandArgName))
            {
                usage.Add($"<{commandArgName}>");
            }

            var hasCommandHelp = command.Children
                                 .OfType <ICommand>()
                                 .Any(f => f.ShouldShowHelp());

            if (hasCommandHelp)
            {
                usage.Add(Usage.Command);
            }

            if (!command.TreatUnmatchedTokensAsErrors)
            {
                usage.Add(Usage.AdditionalArguments);
            }

            HelpSection.Write(this, Usage.Title, string.Join(" ", usage));
        }
Пример #10
0
        /// <summary>
        /// Writes the arguments, if any, for the supplied <see cref="command"/>
        /// </summary>
        /// <param name="command"></param>
        protected virtual void AddArguments(ICommand command)
        {
            var commands = new List <ICommand>();

            if (ShouldDisplayArgumentHelp(command.Parent, out var _))
            {
                commands.Add(command.Parent);
            }

            if (ShouldDisplayArgumentHelp(command, out var _))
            {
                commands.Add(command);
            }

            HelpSection.Write(this, Arguments.Title, commands, ArgumentFormatter);
        }
Пример #11
0
        /// <summary>
        /// Writes the arguments, if any, for the supplied <see cref="command"/>
        /// </summary>
        /// <param name="command"></param>
        protected virtual void AddArguments(ICommand command)
        {
            var commands = new List <ICommand>();

            if (command is Command cmd &&
                cmd.Parents.FirstOrDefault() is ICommand parent &&
                ShouldDisplayArgumentHelp(parent))
            {
                commands.Add(parent);
            }

            if (ShouldDisplayArgumentHelp(command))
            {
                commands.Add(command);
            }

            HelpSection.Write(this, Arguments.Title, commands, GetArgumentHelpItems);
        }