Пример #1
0
        public void Constructor1Works()
        {
            // Arrange
            int t = 1;

            // Act
            var clo = new CommandLineOption('a', "abc", "desc", ParameterType.None, value => t++);
            clo.SetFunction(null);

            // Assert
            Assert.AreEqual("desc", clo.Description);
            Assert.AreEqual("abc", clo.LongName);
            Assert.AreEqual('a', clo.ShortName);
            Assert.AreEqual(ParameterType.None, clo.ParameterType);
            Assert.AreEqual(2, t);
        }
Пример #2
0
        public void Constructor2Works()
        {
            // Arrange
            int t = 1;

            // Act
            var clo = new CommandLineOption("desc", ParameterType.Integer, value => t++, true);
            clo.SetFunction(null);

            // Assert
            Assert.AreEqual("desc", clo.Description);
            Assert.AreEqual(null, clo.LongName);
            Assert.AreEqual('\0', clo.ShortName);
            Assert.AreEqual(ParameterType.Integer, clo.ParameterType);
            Assert.AreEqual(2, t);
        }
Пример #3
0
        /// <summary>
        /// Parses any parameters on the command line that is associated with this option, and calls the set function
        /// </summary>
        /// <param name="args">
        /// Command line arguments, usually the parameter to the "main" function
        /// </param>
        /// <param name="i">
        /// Index in the args array we should start at
        /// </param>
        /// <param name="option">
        /// Which option we are trying to parse
        /// </param>
        /// <param name="inlineValue">
        /// optional value for parameter if found
        /// </param>
        /// <returns>
        /// How many of the arguments in args we used up
        /// </returns>
        private static int ParseAndDispatch(string[] args, int i, CommandLineOption option, string inlineValue = null)
        {
            if (option.ParameterType != ParameterType.None && args.Length < i + 2 && inlineValue == null)
            {
                throw new CommandLineException(string.Format("Option '{0}' requires a parameter", option.LongName ?? option.ShortName.ToString()));
            }

            switch (option.ParameterType)
            {
                case ParameterType.Double:
                    double tempChar;
                    var ci = new CultureInfo("en-US");
                    if (!double.TryParse(inlineValue ?? args[++i], NumberStyles.Any, ci, out tempChar))
                    {
                        throw new CommandLineException(
                            string.Format("Option '{0}': '{1}' is not a valid numeric value", option.Name, inlineValue ?? args[i]));
                    }

                    if (option.SetFunction != null)
                    {
                        option.SetFunction(tempChar);
                    }

                    return i;
                case ParameterType.Integer:
                    int tempInt;
                    if (!int.TryParse(inlineValue ?? args[++i], out tempInt))
                    {
                        throw new CommandLineException(string.Format("Option '{0}': '{1}' is not a valid integer", option.Name, inlineValue ?? args[i]));
                    }

                    if (option.SetFunction != null)
                    {
                        option.SetFunction(tempInt);
                    }

                    return i;
                case ParameterType.String:
                    i++;
                    if (option.SetFunction != null)
                    {
                        option.SetFunction(inlineValue ?? args[i]);
                    }

                    return i;
                case ParameterType.None:
                    if (option.SetFunction != null)
                    {
                        option.SetFunction(null);
                    }

                    return i;
            }

            return i;
        }