Exemple #1
0
        public void CanPushBool()
        {
            ParserParameter param = new ParserNamedParameter("Add", true);
            ParseResult result = new ParseResult();
            result.AddCommandValue(param, "true");

            var pusher = new CommandPusher(result);

            var command = new BoolCommand();

            pusher.Push(command);

            command.Add.ShouldBe(true);
        }
Exemple #2
0
        public void CanPushString()
        {
            ParserParameter param = new ParserNamedParameter("Name", false);
            ParseResult result = new ParseResult();
            result.AddCommandValue(param, "value");

            var pusher = new CommandPusher(result);

            var command = new StringCommand();

            pusher.Push(command);

            command.Name.ShouldBe("value");
        }
Exemple #3
0
        public void CanPushStringList()
        {
            ParserParameter param = new ParserPositionalParameter("Names", true);
            ParseResult result = new ParseResult();

            result.AddCommandListValue(param, "Fred");
            result.AddCommandListValue(param, "Barney");

            var pusher = new CommandPusher(result);

            var command = new ListCommand();

            pusher.Push(command);

            command.Names.ShouldContain(x => x == "Fred");
            command.Names.ShouldContain(x => x == "Barney");
        }
Exemple #4
0
        /// <summary>
        /// Parse the given command line.
        /// </summary>
        /// <param name="raw">The raw list of arguments passed into the program.</param>
        /// <returns>The parsed command</returns>
        public ISubCommand Parse(IEnumerable<string> raw)
        {
            if (!_initialized)
            {
                Initialize();
                _initialized = true;
            }

            Parser parser = new Parser(_configuration);

            var result = parser.Parse(raw);

            if (result.Errors.Any())
            {
                // TODO - build better error text if there are multiple errors
                throw new CommandLineParserException(result.Errors.First());
            }

            // Find the command...
            ISubCommand command = null;
            if (result.GlobalValues.Any(x => x.Name == "Help"))
            {
                // So, they want help. If we have a command that isn't help, swap 'em around (handles "myapp mycmd --help" case)
                if ((result.Command != null))
                {
                    _helpCommand.Commands = new List<string> { result.Command.Text };
                }
                command = _helpCommand;
            }
            else if (result.Command != null)
            {
                command = _commandMap[result.Command.Text];

                if (command != null)
                {
                    // Populate all the parameters on the command based on the parse result...
                    var pusher = new CommandPusher(result);

                    pusher.Push(command);
                }
            }

            if (command != null)
            {
                // TODO - what about the global values?

                // If this is the help command, give it a little assist by looking up the help target (if any)...
                if (command == _helpCommand)
                {
                    if ((_helpCommand.Commands != null) && (_helpCommand.Commands.Any()))
                    {
                        string text = string.Join(" ", _helpCommand.Commands);

                        var helpTarget = _commandMap[text];

                        if (helpTarget == null)
                        {
                            // TODO - better error handling
                            throw new CommandLineParserException("No help for command '{0}', as it is not known.", text);
                        }

                        _helpCommand.Target = _configuration.Commands.FirstOrDefault(x => x.Text == text);
                    }
                }
            }

            // If we didn't find a command, return an empty one.
            return command ?? new EmptyCommand();
        }