private static void ValidateExamples(CommandModel model)
        {
            var examples = new List <string[]>();

            examples.AddRangeIfNotNull(model.Examples);

            // Get all examples.
            var queue = new Queue <ICommandContainer>(new[] { model });

            while (queue.Count > 0)
            {
                var current = queue.Dequeue();

                foreach (var command in current.Commands)
                {
                    examples.AddRangeIfNotNull(command.Examples);
                    queue.Enqueue(command);
                }
            }

            // Validate all examples.
            foreach (var example in examples)
            {
                try
                {
                    var parser = new CommandTreeParser(model, ParsingMode.Strict);
                    parser.Parse(example);
                }
                catch (Exception ex)
                {
                    throw new ConfigurationException("Validation of examples failed.", ex);
                }
            }
        }
Example #2
0
        public Task <int> Execute(IConfiguration configuration, IEnumerable <string> args)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            _registrar.RegisterInstance(typeof(IConfiguration), configuration);

            // Create the command model.
            var model = CommandModelBuilder.Build(configuration);

            _registrar.RegisterInstance(typeof(CommandModel), model);
            _registrar.RegisterDependencies(model);

            // Parse and map the model against the arguments.
            var parser       = new CommandTreeParser(model);
            var parsedResult = parser.Parse(args);

            _registrar.RegisterInstance(typeof(CommandTreeParserResult), parsedResult);

            // Currently the root?
            if (parsedResult.Tree == null)
            {
                // Display help.
                ConsoleRenderer.Render(
                    HelpWriter.Write(model),
                    configuration.Settings.Console);
                return(Task.FromResult(0));
            }

            // Get the command to execute.
            var leaf = parsedResult.Tree.GetLeafCommand();

            if (leaf.Command.IsBranch || leaf.ShowHelp)
            {
                // Branches can't be executed. Show help.
                ConsoleRenderer.Render(
                    HelpWriter.WriteCommand(model, leaf.Command),
                    configuration.Settings.Console);
                return(Task.FromResult(leaf.ShowHelp ? 0 : 1));
            }

            // Register the arguments with the container.
            _registrar.RegisterInstance(typeof(IRemainingArguments), parsedResult.Remaining);

            // Create the resolver and the context.
            var resolver = new TypeResolverAdapter(_registrar.Build());
            var context  = new CommandContext(parsedResult.Remaining, leaf.Command.Name, leaf.Command.Data);

            // Execute the command tree.
            return(Execute(leaf, parsedResult.Tree, context, resolver, configuration));
        }