/// <summary> /// Parses a single argument. /// </summary> /// <param name="parser">An instance of the </param> /// <param name="testForExtendedArgument"></param> /// <returns></returns> private string ParseArgument(TextParser parser, bool testForExtendedArgument) { if (QuoteChars.Contains(parser.Peek())) { return parser.ParseQuotedText(); } else { // Parse unquoted argument int start = parser.Position; while (!parser.EndOfText && !Char.IsWhiteSpace(parser.Peek()) && !FlagChars.Contains(parser.Peek()) && (!testForExtendedArgument || parser.Peek() != ExtendedArgumentDelimiter)) parser.MoveAhead(); return parser.Extract(start, parser.Position); } }
/// <summary> /// Parses a new command line. Populates the Arguments property with the results. /// </summary> /// <param name="commandLine">Command line to parse.</param> /// <param name="supportExtendedArguments">If true, extended arguments are supported in the /// form /f:filename or filename:mode</param> public void ParseCommandLine(string commandLine, bool supportExtendedArguments = false) { // Parse all arguments on command line Arguments = new List<CommandLineArgument>(); TextParser parser = new TextParser(commandLine); while (!parser.EndOfText) { // Skip whitespace parser.MovePastWhitespace(); // Create new argument CommandLineArgument argument = new CommandLineArgument(); // Determine if this is a flag argument.IsFlag = FlagChars.Contains(parser.Peek()); if (argument.IsFlag) parser.MoveAhead(); // Parse argument argument.Argument = ParseArgument(parser, supportExtendedArguments); // Parse extended argument if (parser.Peek() == ExtendedArgumentDelimiter) { parser.MoveAhead(); argument.ExtendedArgument = ParseArgument(parser, false); } // Add to list of arguments if not empty if (!String.IsNullOrWhiteSpace(argument.Argument)) Arguments.Add(argument); } }