public void Parse(string input, string tokenTypes, string csvExpected) { var types = new StringBuilder(); var csv = new StringBuilder(); var tokenizer = ParameterStringTokenizer.New(input); while (tokenizer.MoveToNextToken()) { var token = tokenizer.PeekAtToken(0); switch (token) { case ParameterStringTokenOption _: types.Append("o"); csv.Append(token.Value).Append(';'); break; case ParameterStringTokenValue _: types.Append("v"); csv.Append(token.Value).Append(';'); break; case ParameterStringTokenWhiteSpace _: types.Append("s"); break; default: types.Append("e"); break; } } Assert.AreEqual(tokenTypes, types.ToString()); Assert.AreEqual($"{csvExpected};", csv.ToString()); }
/// <summary> /// Append arguments from an argument string. /// This should be used when a user passes several arguments via a full string. /// Double quotes are used to specify arguments containing spaces. /// To use a double quote inside a quoted argument, double it. /// e.g. -opt "my ""quoted"" value" is a correctly formatted string. /// </summary> /// <param name="argString"></param> /// <returns></returns> public virtual ProcessArgs AppendFromQuotedArgs(string argString) { if (argString != null) { var tokenizer = ParameterStringTokenizer.New(argString); while (tokenizer.MoveToNextToken()) { var token = tokenizer.PeekAtToken(0); if (token is ParameterStringTokenOption || token is ParameterStringTokenValue) { Append(token.Value); } } } return this; }
/// <summary> /// Returns one or more <see cref="UoeDatabaseConnection"/> parsed from the given <paramref name="connectionString"/>. /// </summary> /// <param name="connectionString"></param> /// <returns></returns> /// <exception cref="UoeDatabaseConnectionParseException"></exception> public static IEnumerable <UoeDatabaseConnection> GetConnectionStrings(string connectionString) { if (string.IsNullOrEmpty(connectionString)) { yield break; } var tokenizer = ParameterStringTokenizer.New(connectionString); bool lastTokenWasValue = false; var currentConnectionString = new UoeDatabaseConnection(); while (tokenizer.MoveToNextToken()) { var token = tokenizer.PeekAtToken(0); if (token is ParameterStringTokenValue) { if (lastTokenWasValue) { throw new UoeDatabaseConnectionParseException($"The value {token.Value.PrettyQuote()} does not seem to belong to any option in the connection string: {connectionString.PrettyQuote()}."); } lastTokenWasValue = true; currentConnectionString.ExtraOptions.Append(token.Value); continue; } lastTokenWasValue = false; if (token is ParameterStringTokenOption) { switch (token.Value) { case "-db": if (currentConnectionString.DatabaseLocation != null) { currentConnectionString.Validate(connectionString); yield return(currentConnectionString); currentConnectionString = new UoeDatabaseConnection(); } var dbName = tokenizer.MoveAndPeekAtToken(2); if (dbName is ParameterStringTokenValue) { currentConnectionString.DatabaseLocation = new UoeDatabaseLocation(dbName.Value); } else { throw new UoeDatabaseConnectionParseException($"Expecting a database name or database path after the -db option in the connection string: {connectionString.PrettyQuote()}."); } break; case "-ld": var logicalName = tokenizer.MoveAndPeekAtToken(2); if (logicalName is ParameterStringTokenValue) { currentConnectionString.LogicalName = logicalName.Value; } else { throw new UoeDatabaseConnectionParseException($"Expecting a logical name after the -ld option in the connection string: {connectionString.PrettyQuote()}."); } break; case "-1": currentConnectionString.SingleUser = true; break; case "-S": var serviceName = tokenizer.MoveAndPeekAtToken(2); if (serviceName is ParameterStringTokenValue) { currentConnectionString.Service = serviceName.Value; } else { throw new UoeDatabaseConnectionParseException($"Expecting a service name or port number after the -S option in the connection string: {connectionString.PrettyQuote()}."); } break; case "-H": var hostName = tokenizer.MoveAndPeekAtToken(2); if (hostName is ParameterStringTokenValue) { currentConnectionString.HostName = hostName.Value; } else { throw new UoeDatabaseConnectionParseException($"Expecting a host name after the -H option in the connection string: {connectionString.PrettyQuote()}."); } break; case "-U": var userId = tokenizer.MoveAndPeekAtToken(2); if (userId is ParameterStringTokenValue) { currentConnectionString.UserId = userId.Value; } else { throw new UoeDatabaseConnectionParseException($"Expecting a userid after the -U option in the connection string: {connectionString.PrettyQuote()}."); } break; case "-P": var password = tokenizer.MoveAndPeekAtToken(2); if (password is ParameterStringTokenValue) { currentConnectionString.Password = password.Value; } else { throw new UoeDatabaseConnectionParseException($"Expecting a password after the -P option in the connection string: {connectionString.PrettyQuote()}."); } break; case "-ct": var maxTry = tokenizer.MoveAndPeekAtToken(2); if (maxTry is ParameterStringTokenValue) { try { currentConnectionString.MaxConnectionTry = int.Parse(maxTry.Value); break; } catch (FormatException) { // throw later } } throw new UoeDatabaseConnectionParseException($"Expecting a number value after the -ct option in the connection string: {connectionString.PrettyQuote()}."); default: currentConnectionString.ExtraOptions.Append(token.Value); break; } } } currentConnectionString.Validate(connectionString); yield return(currentConnectionString); }