コード例 #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>
        /// <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);
        }
コード例 #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>
        /// <returns>The parsed argument values.</returns>
        public CommandLineParseResult Parse(CommandLineDefinition definition, IEnumerable <string> args)
        {
            var result = new CommandLineParseResult();

            Parse(definition, args, result);
            return(result);
        }
コード例 #3
0
        private static CommandLineArgument FindArgument(CommandLineDefinition definition, string argName)
        {
            var argObj = definition.FindArgument(argName);

            if (argObj == null)
            {
                throw new UnrecognisedCommandLineArgumentException();
            }
            return(argObj);
        }
コード例 #4
0
        /// <summary>
        /// Generates the usage information for a given command-line argument set.
        /// </summary>
        /// <returns>The usage information for the given command-line argument set.</returns>
        public string GenerateHelpText(CommandLineDefinition definition, string applicationName, string applicationDescription)
        {
            var helpText = new StringBuilder();

            helpText.AppendLine(applicationDescription);
            helpText.AppendLine();

            helpText.Append(applicationName);
            foreach (var arg in (from a in definition where a.Required orderby a.ShortName select a))
            {
                helpText.AppendFormat(" -{0}", arg.ShortName);
                if (arg.RequiresValue)
                {
                    helpText.Append(" \"value\"");
                }
            }

            foreach (var arg in (from a in definition where !a.Required orderby a.ShortName select a))
            {
                helpText.Append(" [");
                helpText.AppendFormat("-{0}", arg.ShortName);
                if (arg.RequiresValue)
                {
                    helpText.Append(" \"value\"");
                }
                helpText.Append("]");
            }

            helpText.AppendLine();
            helpText.AppendLine();

            var longestShortName = (from arg in definition select arg.ShortName.Length).Max() * -1;
            var longestLongName  = (from arg in definition select arg.LongName.Length).Max() * -1;

            var formatString = string.Format(" {{0}} -{{1,{0}}} --{{2,{1}}}  {{3}}{{4}}", longestShortName, longestLongName);

            foreach (var arg in definition.OrderByDescending(a => a.Required).ThenBy(a => a.ShortName))
            {
                helpText.AppendFormat(formatString, arg.Required ? "R" : " ", arg.ShortName, arg.LongName, arg.Description, Environment.NewLine);
            }
            return(helpText.ToString());
        }
コード例 #5
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 object Parse(CommandLineDefinition definition, string args)
 {
     return(Parse(definition, Split(args)));
 }