コード例 #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());
                }
        }
コード例 #2
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);
        }
コード例 #3
0
ファイル: HelpWriter.cs プロジェクト: pdylanross/spectre.cli
        private static void WriteCommands(
            RenderableComposer composer,
            CommandModel model,
            ICommandContainer command,
            bool isDefaultCommand)
        {
            var commands = isDefaultCommand ? model.Commands : command.Commands;

            if (commands.Count > 0)
            {
                composer.Color(ConsoleColor.Yellow, c => c.Text("COMMANDS:")).LineBreak();
                var maxCommandLength = commands.Max(x => x.Name.Length);
                foreach (var child in commands)
                {
                    composer.Tab().Text(child.Name);
                    composer.Spaces(maxCommandLength - child.Name.Length);
                    composer.Tab().Text(child.Description?.TrimEnd('.') ?? string.Empty);
                    composer.LineBreak();
                }
            }
        }
コード例 #4
0
        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 ConfigurationException.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 ConfigurationException.CommandNameConflict(command, alias);
                    }
                }
            }

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

            if (settings.ValidateExamples)
            {
                ValidateExamples(model, settings);
            }
        }
コード例 #5
0
        public static IRenderable WriteCommand(CommandModel model, CommandInfo command)
        {
            var composer = new RenderableComposer();

            composer.LineBreak();

            // Usage
            WriteUsage(composer, model, command);
            composer.LineBreak().LineBreak();

            // Arguments
            WriteArguments(composer, command);

            // Options
            WriteOptions(composer, command);

            // Commands
            WriteCommands(composer, model, (ICommandContainer)command ?? model, command?.IsDefaultCommand ?? false);

            composer.LineBreak();
            return(composer);
        }
コード例 #6
0
 public static IEnumerable <IRenderable> Write(CommandModel model)
 {
     return(WriteCommand(model, null));
 }
コード例 #7
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 Stack <string>();

            if (command == null)
            {
                parameters.Push("[aqua]<COMMAND>[/]");
                parameters.Push("[grey][[OPTIONS]][/]");
            }
            else
            {
                var current = command;
                if (command.IsBranch)
                {
                    parameters.Push("[aqua]<COMMAND>[/]");
                }

                while (current != null)
                {
                    var isCurrent = current == command;
                    if (isCurrent)
                    {
                        parameters.Push("[grey][[OPTIONS]][/]");
                    }

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

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

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

                    current = current.Parent;
                }
            }

            composer.Join(" ", parameters);
            composer.LineBreaks(2);

            return(new[]
            {
                composer,
            });
        }
コード例 #8
0
ファイル: HelpWriter.cs プロジェクト: pdylanross/spectre.cli
        private static void WriteUsage(RenderableComposer composer, CommandModel model, CommandInfo command)
        {
            var parameters = new Stack <IRenderable>();

            if (command == null)
            {
                parameters.Push(new ColorElement(ConsoleColor.Cyan, new TextElement("<COMMAND>")));
                parameters.Push(new ColorElement(ConsoleColor.DarkGray, new TextElement("[OPTIONS]")));
            }
            else
            {
                var current = command;
                if (command.IsBranch)
                {
                    parameters.Push(new ColorElement(ConsoleColor.Cyan, new TextElement("<COMMAND>")));
                }

                while (current != null)
                {
                    var builder = new BlockElement();

                    if (!current.IsDefaultCommand)
                    {
                        builder.Append(new TextElement(current.Name));
                    }

                    if (current.Parameters.OfType <CommandArgument>().Any())
                    {
                        var isCurrent = current == command;
                        if (isCurrent)
                        {
                            foreach (var argument in current.Parameters.OfType <CommandArgument>()
                                     .Where(a => a.Required).OrderBy(a => a.Position).ToArray())
                            {
                                var text = new TextElement($" <{argument.Value}>");
                                builder.Append(new ColorElement(ConsoleColor.Cyan, text));
                            }
                        }

                        var optionalArguments = current.Parameters.OfType <CommandArgument>().Where(x => !x.Required).ToArray();
                        if (optionalArguments.Length > 0 || !isCurrent)
                        {
                            foreach (var optionalArgument in optionalArguments)
                            {
                                var text = new TextElement($" [{optionalArgument.Value}]");
                                builder.Append(new ColorElement(ConsoleColor.Gray, text));
                            }
                        }
                    }

                    if (current == command)
                    {
                        builder.Append(new ColorElement(ConsoleColor.DarkGray, new TextElement(" [OPTIONS]")));
                    }

                    parameters.Push(builder);
                    current = current.Parent;
                }
            }

            composer.Color(ConsoleColor.Yellow, c => c.Text("USAGE:")).LineBreak();
            composer.Tab().Text(model.ApplicationName ?? Path.GetFileName(Assembly.GetEntryAssembly().Location));

            // Root or not a default command?
            if (command?.IsDefaultCommand != true)
            {
                composer.Space();
            }

            composer.Join(" ", parameters);
        }
コード例 #9
0
 public XmlDocCommand(IConfiguration configuration, CommandModel model)
 {
     _model  = model ?? throw new ArgumentNullException(nameof(model));
     _writer = configuration?.Settings?.Console ?? new DefaultConsoleWriter();
 }
コード例 #10
0
 public XmlDocCommand(IConfiguration configuration, CommandModel model)
 {
     _model  = model ?? throw new ArgumentNullException(nameof(model));
     _writer = (configuration?.Settings?.Console ?? new ConsoleSettings()).CreateConsole();
 }
コード例 #11
0
 public CommandTreeParser(CommandModel configuration, ParsingMode?parsingMode = null)
 {
     _configuration = configuration;
     _parsingMode   = parsingMode ?? _configuration.ParsingMode;
     _help          = new CommandOptionAttribute("-h|--help");
 }