Exemple #1
0
        public static string Serialize(CommandModel model)
        {
            var settings = new XmlWriterSettings
            {
                Indent             = true,
                IndentChars        = "  ",
                NewLineChars       = "\n",
                OmitXmlDeclaration = false,
                Encoding           = Encoding.UTF8,
            };

            using (var buffer = new StringWriterWithEncoding(Encoding.UTF8))
                using (var xmlWriter = XmlWriter.Create(buffer, settings))
                {
                    SerializeModel(model).WriteTo(xmlWriter);
                    xmlWriter.Flush();
                    return(buffer.GetStringBuilder().ToString());
                }
        }
            public static IReadOnlyList <HelpOption> Get(CommandModel model, CommandInfo?command)
            {
                var parameters = new List <HelpOption>();

                parameters.Add(new HelpOption("h", "help", null, null, "Prints help information"));

                // At the root and no default command?
                if (command == null && model?.DefaultCommand == null)
                {
                    parameters.Add(new HelpOption("v", "version", null, null, "Prints version information"));
                }

                parameters.AddRange(command?.Parameters?.OfType <CommandOption>()?.Select(o =>
                                                                                          new HelpOption(
                                                                                              o.ShortNames.FirstOrDefault(), o.LongNames.FirstOrDefault(),
                                                                                              o.ValueName, o.ValueIsOptional, o.Description))
                                    ?? Array.Empty <HelpOption>());
                return(parameters);
            }
Exemple #3
0
        private static XmlDocument SerializeModel(CommandModel model)
        {
            var document = new XmlDocument();
            var root     = document.CreateElement("Model");

            if (model.DefaultCommand != null)
            {
                root.AppendChild(document.CreateComment("DEFAULT COMMAND"));
                root.AppendChild(CreateCommandNode(document, model.DefaultCommand, isDefaultCommand: true));
            }

            foreach (var command in model.Commands.Where(x => !x.IsHidden))
            {
                root.AppendChild(document.CreateComment(command.Name.ToUpperInvariant()));
                root.AppendChild(CreateCommandNode(document, command));
            }

            document.AppendChild(root);
            return(document);
        }
        public static void Validate(CommandModel model, CommandAppSettings settings)
        {
            if (model is null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            if (settings is null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            if (model.Commands.Count == 0 && model.DefaultCommand == null)
            {
                throw CommandConfigurationException.NoCommandConfigured();
            }

            foreach (var command in model.Commands)
            {
                // Alias collision?
                foreach (var alias in command.Aliases)
                {
                    if (model.Commands.Any(x => x.Name.Equals(alias, StringComparison.OrdinalIgnoreCase)))
                    {
                        throw CommandConfigurationException.CommandNameConflict(command, alias);
                    }
                }
            }

            Validate(model.DefaultCommand);
            foreach (var command in model.Commands)
            {
                Validate(command);
            }

            if (settings.ValidateExamples)
            {
                ValidateExamples(model, settings);
            }
        }
        private static IEnumerable <IRenderable> GetOptions(CommandModel model, CommandInfo?command)
        {
            // Collect all options into a single structure.
            var parameters = HelpOption.Get(model, command);

            if (parameters.Count == 0)
            {
                return(Array.Empty <IRenderable>());
            }

            var result = new List <IRenderable>
            {
                new Markup("[yellow]OPTIONS:[/]"),
                new Markup("\n"),
            };

            var grid = new Grid();

            grid.AddColumn(new GridColumn {
                Padding = new Padding(4, 4), NoWrap = true
            });
            grid.AddColumn(new GridColumn {
                Padding = new Padding(0, 0)
            });
 public static IEnumerable <IRenderable> Write(CommandModel model)
 {
     return(WriteCommand(model, null));
 }
        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.LineBreaks(2);

            return(new[]
            {
                composer,
            });
        }
Exemple #8
0
 public XmlDocCommand(IConfiguration configuration, CommandModel model)
 {
     _model  = model ?? throw new ArgumentNullException(nameof(model));
     _writer = configuration.Settings.Console.GetConsole();
 }