Esempio n. 1
0
        private static IEnumerable <IRenderable> GetExamples(CommandModel model, CommandInfo?command)
        {
            var maxExamples = int.MaxValue;

            var examples = command?.Examples ?? model.Examples ?? new List <string[]>();

            if (examples.Count == 0)
            {
                // Since we're not checking direct examples,
                // make sure that we limit the number of examples.
                maxExamples = 5;

                // Get the current root command.
                var root  = command ?? (ICommandContainer)model;
                var queue = new Queue <ICommandContainer>(new[] { root });

                // Traverse the command tree and look for examples.
                // As soon as a node contains commands, bail.
                while (queue.Count > 0)
                {
                    var current = queue.Dequeue();

                    foreach (var cmd in current.Commands.Where(x => !x.IsHidden))
                    {
                        if (cmd.Examples.Count > 0)
                        {
                            examples.AddRange(cmd.Examples);
                        }

                        queue.Enqueue(cmd);
                    }

                    if (examples.Count >= maxExamples)
                    {
                        break;
                    }
                }
            }

            if (examples.Count > 0)
            {
                var composer = new Composer();
                composer.LineBreak();
                composer.Style("yellow", "EXAMPLES:").LineBreak();

                for (var index = 0; index < Math.Min(maxExamples, examples.Count); index++)
                {
                    var args = string.Join(" ", examples[index]);
                    composer.Tab().Text(model.GetApplicationName()).Space().Style("grey", args);
                    composer.LineBreak();
                }

                return(new[] { composer });
            }

            return(Array.Empty <IRenderable>());
        }
Esempio n. 2
0
        private static IEnumerable <IRenderable> GetUsage(CommandModel model, CommandInfo?command)
        {
            var composer = new Composer();

            composer.Style("yellow", "USAGE:").LineBreak();
            composer.Tab().Text(model.GetApplicationName());

            var parameters = new List <string>();

            if (command == null)
            {
                parameters.Add("[grey][[OPTIONS]][/]");
                parameters.Add("[aqua]<COMMAND>[/]");
            }
            else
            {
                foreach (var current in command.Flatten())
                {
                    var isCurrent = current == command;

                    if (!current.IsDefaultCommand)
                    {
                        if (isCurrent)
                        {
                            parameters.Add($"[underline]{current.Name.EscapeMarkup()}[/]");
                        }
                        else
                        {
                            parameters.Add($"{current.Name.EscapeMarkup()}");
                        }
                    }

                    if (current.Parameters.OfType <CommandArgument>().Any())
                    {
                        if (isCurrent)
                        {
                            foreach (var argument in current.Parameters.OfType <CommandArgument>()
                                     .Where(a => a.Required).OrderBy(a => a.Position).ToArray())
                            {
                                parameters.Add($"[aqua]<{argument.Value.EscapeMarkup()}>[/]");
                            }
                        }

                        var optionalArguments = current.Parameters.OfType <CommandArgument>().Where(x => !x.Required).ToArray();
                        if (optionalArguments.Length > 0 || !isCurrent)
                        {
                            foreach (var optionalArgument in optionalArguments)
                            {
                                parameters.Add($"[silver][[{optionalArgument.Value.EscapeMarkup()}]][/]");
                            }
                        }
                    }

                    if (isCurrent)
                    {
                        parameters.Add("[grey][[OPTIONS]][/]");
                    }
                }

                if (command.IsBranch)
                {
                    parameters.Add("[aqua]<COMMAND>[/]");
                }
            }

            composer.Join(" ", parameters);
            composer.LineBreak();

            return(new[]
            {
                composer,
            });
        }