private static IRenderable CreatePrettyMessage(string arguments, CommandTreeToken token, string message, string details)
        {
            var composer = new Composer();

            var position = token?.Position ?? 0;
            var value    = token?.Representation ?? arguments;

            // Header
            composer.LineBreak();
            composer.Style("red", "Error:");
            composer.Space().Text(message.EscapeMarkup());
            composer.LineBreak();

            // Template
            composer.LineBreak();
            composer.Spaces(7).Text(arguments.EscapeMarkup());

            // Error
            composer.LineBreak();
            composer.Spaces(7).Spaces(position);

            composer.Style("red", error =>
            {
                error.Repeat('^', value.Length);
                error.Space();
                error.Text(details.TrimEnd('.').EscapeMarkup());
                error.LineBreak();
            });

            composer.LineBreak();

            return(composer);
        }
예제 #2
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>());
        }
        private static IRenderable CreatePrettyMessage(string template, TemplateToken?token, string message, string details)
        {
            var composer = new Composer();

            var position = token?.Position ?? 0;
            var value    = token?.Representation ?? template;

            // Header
            composer.LineBreak();
            composer.Style("red", "Error:");
            composer.Space().Text("An error occured when parsing template.");
            composer.LineBreak();
            composer.Spaces(7).Style("yellow", message.EscapeMarkup());
            composer.LineBreak();

            if (string.IsNullOrWhiteSpace(template))
            {
                // Error
                composer.LineBreak();
                composer.Style("red", message.EscapeMarkup());
                composer.LineBreak();
            }
            else
            {
                // Template
                composer.LineBreak();
                composer.Spaces(7).Text(template.EscapeMarkup());

                // Error
                composer.LineBreak();
                composer.Spaces(7).Spaces(position);
                composer.Style("red", error =>
                {
                    error.Repeat('^', value.Length);
                    error.Space();
                    error.Text(details.TrimEnd('.').EscapeMarkup());
                    error.LineBreak();
                });
            }

            composer.LineBreak();

            return(composer);
        }
예제 #4
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,
            });
        }