示例#1
0
        private static CommandLineArgument FindArgument(CommandLineDefinition definition, string argName)
        {
            var argObj = definition.FindArgument(argName);

            if (argObj == null)
            {
                throw new UnrecognisedCommandLineArgumentException();
            }
            return(argObj);
        }
示例#2
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());
        }
示例#3
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 Dictionary <string, string> Parse(CommandLineDefinition definition, IEnumerable <string> args)
        {
            var argValues  = new Dictionary <string, string>();
            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);
                argValues.Add(argObj.LongName, argValue);
            }
            EnsureRequiredArgumentsPresent(definition, argValues);
            return(argValues);
        }
示例#4
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)));
 }