Beispiel #1
0
        public void MissingRequiredStringParamThrows()
        {
            var root = _builder.ExternalCommand("funky")
                .WithPositionalString("Name", true)
                .Root;

            var parser = new Parser(root);

            Should.Throw<CommandLineParserException>(() => parser.Parse("funky"));
        }
Beispiel #2
0
        public void SpecifiedRequiredStringParamIsSet()
        {
            var root = _builder.ExternalCommand("funky")
                .WithPositionalString("Name", true)
                .Root;

            var parser = new Parser(root);

            var result = parser.Parse("funky foo");

            result.Kind.ShouldBe(ParseResultKind.ExternalCommand);

            result.PositionalParameters.Count.ShouldBe(1);
            result.PositionalParameters[0].Parameter.PropertyName.ShouldBe("Name");
            result.PositionalParameters[0].Values.ShouldBe(new[] { "foo" });
        }
Beispiel #3
0
        public void BoolDoesNotGobblePositional()
        {
            var root = _builder.ExternalCommand("funky")
                .WithNamedBool("Test")
                .WithPositionalString("Name")
                .Root;

            var parser = new Parser(root);

            var result = parser.Parse("funky -t bar");

            result.Kind.ShouldBe(ParseResultKind.ExternalCommand);

            // TODO - can _builder help validate parameters?
            result.NamedParameters.Count.ShouldBe(1);
            result.NamedParameters[0].Parameter.PropertyName.ShouldBe("Test");
            result.NamedParameters[0].Value.ShouldBe(true.ToString());

            result.PositionalParameters.Count.ShouldBe(1);
            result.PositionalParameters[0].Parameter.PropertyName.ShouldBe("Name");
            result.PositionalParameters[0].Values.ShouldBe(new[] { "bar" });
        }
Beispiel #4
0
        // TODO - test global long-name AFTER command: add --file foo.txt where --file is global, and not command-specific
        private ParseResult DoParse(string text)
        {
            Parser parser = new Parser(_config);

            var result = parser.Parse(text);

            result.Errors.ShouldBeEmpty();

            return result;
        }
Beispiel #5
0
        private ParseResult DoBadParse(string text)
        {
            Parser parser = new Parser(_config);

            return parser.Parse(text);
        }
Beispiel #6
0
        public void EnumerableOverloadCanHandleParameterWithSpaces()
        {
            var command = _config.AddCommand("exit");
            command.AddNamedParameter("Speed");

            Parser parser = new Parser(_config);

            var result = parser.Parse(new[] { "exit", "--speed", "super fast" });

            result.Errors.ShouldBeEmpty();

            var value = result.CommandValues.FirstOrDefault(x => x.Name == "Speed");

            value.ShouldNotBe(null);
            value.Value.ShouldBe("super fast");
        }
Beispiel #7
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();
        }