Exemple #1
0
        /// <summary>
        /// Parses a collection of command-line parameters.
        /// </summary>
        /// <param name="definition">The definition that the command-line arguments must correspond to.</param>
        /// <param name="args">The arguments to parse.</param>
        /// <returns>The parsed argument values.</returns>
        public CommandLineParseResult Parse(CommandLineDefinition definition, IEnumerable <string> args)
        {
            var result = new CommandLineParseResult();

            Parse(definition, args, result);
            return(result);
        }
Exemple #2
0
        /// <summary>
        /// Parses a collection of command-line parameters.
        /// </summary>
        /// <param name="definition">The definition that the command-line arguments must correspond to.</param>
        /// <param name="args">The arguments to parse.</param>
        /// <param name="result">The </param>
        public void Parse(CommandLineDefinition definition, IEnumerable <string> args, CommandLineParseResult result)
        {
            var enumerator = args.GetEnumerator();

            while (enumerator.MoveNext())
            {
                var arg = enumerator.Current;
                if (string.IsNullOrEmpty(arg))
                {
                    continue;
                }

                var argName  = ParseArgumentName(arg);
                var argObj   = FindArgument(definition, argName);
                var argValue = ParseArgumentValue(argObj, enumerator);

                if (result.Arguments.ContainsKey(argValue))
                {
                    result.Arguments[argObj.LongName] = argValue;
                }
                else
                {
                    result.Arguments.Add(argObj.LongName, argValue);
                }
            }
            EnsureRequiredArgumentsPresent(definition, result);
        }
Exemple #3
0
        private static void EnsureRequiredArgumentsPresent(IEnumerable <CommandLineArgument> definition, CommandLineParseResult argumentValues)
        {
            var missingRequiredArgs = from a in definition
                                      where a.Required && !argumentValues.Arguments.ContainsKey(a.LongName)
                                      select a;

            foreach (var requiredArgument in missingRequiredArgs)
            {
                throw new MissingRequiredCommandLineArgumentException(requiredArgument);
            }
        }