Example #1
0
        private int ParseMultiValueAsParameter(MultiValueOption <TEnvironment> parameter, TEnvironment env, string[] args, int parameterIndex)
        {
            var arguments = new List <string>();

            string value = null;

            do
            {
                value = this.GetParameterValue(parameter, args, parameterIndex);

                if (value != null)
                {
                    parameterIndex++;
                    arguments.Add(value);
                }
            } while (value != null);

            // Leave on error
            if (parameter.IsRequired && arguments.Count == 0)
            {
                env.Errors.Add(string.Format("Parameter {0} is required", parameter.LongName ?? parameter.ShortName));
                // Move 1 position past to the parameter
                return(parameterIndex + 1);
            }

            parameter.Handler.Invoke(env, arguments.ToArray());

            // Move 1 position past to the last value
            return(parameterIndex + 1);
        }
Example #2
0
        public Option <TEnvironment> Add(string shortopt, string longopt, string description, Action <TEnvironment, string[]> action, OptionAttributes attributes)
        {
            var p = new MultiValueOption <TEnvironment>(shortopt, longopt, description, attributes, action);

            Options.Add(p);
            return(p);
        }
Example #3
0
        public void Description_RequiredMultiStringOption_ShouldIncludeMultipleOccurrencesAllowedAndRequired()
        {
            // arrange
            var multiStringOption = new MultiValueOption <string>(StringOption.CreateRequired("value option", "value"));

            // act
            var description = multiStringOption.Info.Description;

            // assert
            Assert.Equal("value option. Option can be specified multiple times. (required)", description);
        }
Example #4
0
        public void Description_RequiredMultiStringOption_ShouldIncludeMultipleOccurrencesAllowedAndRequired()
        {
            // arrange
            var multiStringOption = new MultiValueOption<string>(StringOption.CreateRequired("value option", "value"));

            // act
            var description = multiStringOption.Info.Description;

            // assert
            Assert.Equal("value option. Option can be specified multiple times. (required)", description);
        }
Example #5
0
        private int ParseMultiValueParameter(MultiValueOption <TEnvironment> parameter, TEnvironment env, string[] args, int parameterIndex)
        {
            if (parameter.Attributes.HasFlag(OptionAttributes.SubModule))
            {
                this.ParseMultiValueAsSubModule(parameter, env, args.Skip(parameterIndex + 1).ToArray());

                // The submodule process the rest of the arguments, so we need to leave
                return(args.Length);
            }
            else if (parameter.Attributes.HasFlag(OptionAttributes.MultiValue))
            {
                parameterIndex = this.ParseMultiValueAsParameter(parameter, env, args, parameterIndex);
            }

            return(parameterIndex);
        }
Example #6
0
        public void ParseMultiStringOption_WithMultipleStringInputArguments_ReturnsMultipleStrings()
        {
            // arrange
            string inputArgument1    = "input";
            string inputArgument2    = "input2";
            var    multiStringOption = new MultiValueOption <string>(StringOption.Create("value option", "value"));

            string[] stringInputArgument =
            {
                "--value", inputArgument1, "--value", inputArgument2
            };

            // act
            var result = multiStringOption.Parse(stringInputArgument);

            // assert
            Assert.True(result.Success);
            Assert.Equal(inputArgument1, result.Result[0]);
            Assert.Equal(inputArgument2, result.Result[1]);
        }
Example #7
0
        public void ParseMultiFileOption_WithMultipleFileArguments_ReturnsListOfFileLocations(TemporaryDirectoryFixture tempDirectory)
        {
            // arrange
            MultiValueOption <IFileLocation> multiFileOption = new MultiValueOption <IFileLocation>(FileOption.CreateRequired("multiple files", "file"));
            var file1 = tempDirectory.CreateFile("file1");
            var file2 = tempDirectory.CreateFile("file2");

            string[] args =
            {
                "--file", file1.ToString(), "--file", file2.ToString()
            };

            // act
            var result = multiFileOption.Parse(args);

            // assert
            Assert.Equal("", result.ErrorMessage);
            Assert.True(result.Success);
            Assert.Equal(file1, result.Result[0]);
            Assert.Equal(file2, result.Result[1]);
        }
Example #8
0
        public void ParseMultiFileOption_WithMultipleFileArguments_ReturnsListOfFileLocations(TemporaryDirectoryFixture tempDirectory)
        {
            // arrange
            MultiValueOption<IFileLocation> multiFileOption = new MultiValueOption<IFileLocation>(FileOption.CreateRequired("multiple files", "file"));
            var file1 = tempDirectory.CreateFile("file1");
            var file2 = tempDirectory.CreateFile("file2");
            string[] args =
            {
                "--file", file1.ToString(), "--file", file2.ToString()
            };

            // act
            var result = multiFileOption.Parse(args);

            // assert
            Assert.Equal("", result.ErrorMessage);
            Assert.True(result.Success);
            Assert.Equal(file1, result.Result[0]);
            Assert.Equal(file2, result.Result[1]);
        }
Example #9
0
        public void ParseMultiStringOption_WithMultipleStringInputArguments_ReturnsMultipleStrings()
        {
            // arrange
            string inputArgument1 = "input";
            string inputArgument2 = "input2";
            var multiStringOption = new MultiValueOption<string>(StringOption.Create("value option", "value"));
            string[] stringInputArgument =
            {
                "--value", inputArgument1, "--value", inputArgument2
            };

            // act
            var result = multiStringOption.Parse(stringInputArgument);

            // assert
            Assert.True(result.Success);
            Assert.Equal(inputArgument1, result.Result[0]);
            Assert.Equal(inputArgument2, result.Result[1]);
        }
Example #10
0
 private void ParseMultiValueAsSubModule(MultiValueOption <TEnvironment> parameter, TEnvironment env, string[] args)
 {
     parameter.Handler.Invoke(env, args);
 }