/// <summary> /// Parse the command arguments for the read-pbf command. /// </summary> /// <param name="args"></param> /// <param name="idx"></param> /// <param name="command"></param> /// <returns></returns> public override int Parse(string[] args, int idx, out Command command) { // check next argument. if (args.Length < idx) { throw new CommandLineParserException("None", "Invalid file name for read-pbf command!"); } // everything ok, take the next argument as the filename. command = new CommandReadPBF() { File = args[idx] }; return 1; }
/// <summary> /// Parse the command arguments for the write-xml command. /// </summary> /// <param name="args"></param> /// <param name="idx"></param> /// <param name="command"></param> /// <returns></returns> public override int Parse(string[] args, int idx, out Command command) { // check next argument. if (args.Length < idx) { throw new CommandLineParserException("None", "Invalid file name for write-xml command!"); } // everything ok, take the next argument as the filename. command = new CommandWriteRedis() { ConnectionString = args[idx] }; return 1; }
/// <summary> /// Parses the command line arguments for the merge command. /// </summary> /// <param name="args"></param> /// <param name="idx"></param> /// <param name="command"></param> /// <returns></returns> public override int Parse(string[] args, int idx, out Command command) { // everything ok, there are no arguments. command = new CommandFilterMerge(); return 0; }
/// <summary> /// Parses string arguments into an actual command object. /// </summary> /// <param name="args"></param> /// <param name="idx"></param> /// <param name="command"></param> /// <returns></returns> public abstract int Parse(string[] args, int idx, out Command command);
/// <summary> /// Parse the command arguments for the write-xml command. /// </summary> /// <param name="args"></param> /// <param name="idx"></param> /// <param name="command"></param> /// <returns></returns> public override int Parse(string[] args, int idx, out Command command) { CommandWriteScene commandWriteScene = new CommandWriteScene(); // check next argument. if (args.Length < idx) { throw new CommandLineParserException("None", "Invalid arguments for --write-scene!"); } // parse arguments and keep parsing until the next switch. int startIdx = idx; while (args.Length > idx && !CommandParser.IsSwitch(args[idx])) { string[] keyValue; if (CommandParser.SplitKeyValue(args[idx], out keyValue)) { // the command splitting succeeded. keyValue[0] = CommandParser.RemoveQuotes(keyValue[0]); keyValue[1] = CommandParser.RemoveQuotes(keyValue[1]); switch (keyValue[0].ToLower()) { case "scene": commandWriteScene.SceneFile = keyValue[1]; break; case "css": commandWriteScene.MapCSS = keyValue[1]; break; case "cutoffs": string[] values; if (CommandParser.SplitValuesArray(keyValue[1], out values)) { // split the values array. float[] cutoffs = new float[values.Length]; for (int valueIdx = 0; valueIdx < values.Length; valueIdx++) { float value; if (!float.TryParse(values[valueIdx], System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out value)) { throw new CommandLineParserException("--write-scene", string.Format("Invalid parameter value for command --write-scene parameter {0}: {1} not recognized.", keyValue[0], values[valueIdx])); } cutoffs[valueIdx] = value; } commandWriteScene.ZoomLevelCutoffs = cutoffs; } break; default: // the command splitting succeed but one of the arguments is unknown. throw new CommandLineParserException("--write-scene", string.Format("Invalid parameter for command --write-scene: {0} not recognized.", keyValue[0])); } } else { // the command splitting failed and this is not a switch. throw new CommandLineParserException("--write-scene", "Invalid parameter for command --write-scene."); } idx++; // increase the index. } // check command consistency. if (commandWriteScene.ZoomLevelCutoffs == null || commandWriteScene.ZoomLevelCutoffs.Length == 0) { // assign some defaults instead of complaining. commandWriteScene.ZoomLevelCutoffs = new float[] { 16, 13, 10, 7, 4 }; } // everything ok, take the next argument as the filename. command = commandWriteScene; return idx - startIdx; }
/// <summary> /// Parse the command arguments for the bounding-box command. /// </summary> /// <param name="args"></param> /// <param name="idx"></param> /// <param name="command"></param> /// <returns></returns> public override int Parse(string[] args, int idx, out Command command) { // check next argument. if (args.Length < idx + 3) { throw new CommandLineParserException("None", "Invalid bounding-box command!"); } bool topOk = false, bottomOk = false, leftOk = false, rightOk = false; float top = 0, bottom = 0, left = 0, right = 0; for (int currentArg = idx; currentArg < idx + 4; currentArg++) { string[] argSplit = args[currentArg].Split('='); if (argSplit.Length != 2 || argSplit[0] == null || argSplit[1] == null) { throw new CommandLineParserException(args[currentArg], "Invalid boundary condition for boundingbox command!"); } argSplit[0] = argSplit[0].ToLower(); argSplit[0] = CommandParser.RemoveQuotes(argSplit[0]); argSplit[1] = CommandParser.RemoveQuotes(argSplit[1]); if (argSplit[0] == "top") { if ( !float.TryParse(argSplit[1], NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out top)) { throw new CommandLineParserException(args[currentArg], "Invalid boundary condition for boundingbox command!"); } topOk = true; } else if (argSplit[0] == "left") { if ( !float.TryParse(argSplit[1], NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out left)) { throw new CommandLineParserException(args[currentArg], "Invalid boundary condition for boundingbox command!"); } leftOk = true; } else if (argSplit[0] == "bottom") { if ( !float.TryParse(argSplit[1], NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out bottom)) { throw new CommandLineParserException(args[currentArg], "Invalid boundary condition for boundingbox command!"); } bottomOk = true; } else if (argSplit[0] == "right") { if ( !float.TryParse(argSplit[1], NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out right)) { throw new CommandLineParserException(args[currentArg], "Invalid boundary condition for boundingbox command!"); } rightOk = true; } else { throw new CommandLineParserException(args[currentArg], "Invalid boundary condition for boundingbox command!"); } } if (!(bottomOk && topOk && leftOk && rightOk)) { throw new CommandLineParserException("None", "Invalid bounding-box command, at least one of the boundaries is missing!"); } // everything ok, take the next argument as the filename. command = new CommandFilterBoundingBox() { Top = top, Bottom = bottom, Left = left, Right = right }; return 4; }
/// <summary> /// Parse the command arguments for the write-xml command. /// </summary> /// <param name="args"></param> /// <param name="idx"></param> /// <param name="command"></param> /// <returns></returns> public override int Parse(string[] args, int idx, out Command command) { CommandWriteGraph commandWriteGraph = new CommandWriteGraph(); // check next argument. if (args.Length < idx) { throw new CommandLineParserException("None", "Invalid arguments for --write-graph!"); } // set default vehicle to car. commandWriteGraph.Vehicle = OsmSharp.Routing.Vehicles.Vehicle.Car; commandWriteGraph.GraphType = GraphType.Regular; // parse arguments and keep parsing until the next switch. int startIdx = idx; while (args.Length > idx && !CommandParser.IsSwitch(args[idx])) { string[] keyValue; if (CommandParser.SplitKeyValue(args[idx], out keyValue)) { // the command splitting succeeded. keyValue[0] = CommandParser.RemoveQuotes(keyValue[0]); keyValue[1] = CommandParser.RemoveQuotes(keyValue[1]); switch (keyValue[0].ToLower()) { case "graph": commandWriteGraph.GraphFile = keyValue[1]; break; case "type": string typeValue = keyValue[1].ToLower(); switch (typeValue) { case "contracted": commandWriteGraph.GraphType = GraphType.Contracted; break; } break; case "vehicle": string vehicleValue = keyValue[1].ToLower(); var vehicle = OsmSharp.Routing.Vehicles.Vehicle.GetByUniqueName(vehicleValue); if (vehicle == null) { // the vehicle with the given name was not detected. throw new CommandLineParserException("--write-graph", string.Format("Invalid parameter for command --write-graph: vehicle={0} not found.", keyValue[1])); } commandWriteGraph.Vehicle = vehicle; break; case "mm": commandWriteGraph.MemoryMapFile = keyValue[1]; break; default: // the command splitting succeed but one of the arguments is unknown. throw new CommandLineParserException("--write-graph", string.Format("Invalid parameter for command --write-graph: {0} not recognized.", keyValue[0])); } } else { // the command splitting failed and this is not a switch. throw new CommandLineParserException("--write-graph", "Invalid parameter for command --write-graph."); } idx++; // increase the index. } // everything ok, take the next argument as the filename. command = commandWriteGraph; return idx - startIdx; }
/// <summary> /// Parses the command line arguments for the filter command. /// </summary> /// <returns></returns> public override int Parse(string[] args, int idx, out Command command) { var commandFilterPoly = new CommandFilterPoly(); // check next argument. if (args.Length < idx) { throw new CommandLineParserException("None", "Invalid arguments for --bounding-polygon!"); } // parse arguments and keep parsing until the next switch. int startIdx = idx; while (args.Length > idx && !CommandParser.IsSwitch(args[idx])) { string[] keyValue; if (CommandParser.SplitKeyValue(args[idx], out keyValue)) { // the command splitting succeeded. keyValue[0] = CommandParser.RemoveQuotes(keyValue[0]); keyValue[1] = CommandParser.RemoveQuotes(keyValue[1]); switch (keyValue[0].ToLower()) { case "file": commandFilterPoly.File = keyValue[1]; break; default: // the command splitting succeed but one of the arguments is unknown. throw new CommandLineParserException("--bounding-polygon", string.Format("Invalid parameter for command --bounding-polygon: {0} not recognized.", keyValue[0])); } } else { // the command splitting failed and this is not a switch. throw new CommandLineParserException("--bounding-polygon", "Invalid parameter for command --bounding-polygon."); } idx++; // increase the index. } command = commandFilterPoly; return 1; }
/// <summary> /// Parses the command line arguments for the sort command. /// </summary> /// <param name="args"></param> /// <param name="idx"></param> /// <param name="command"></param> /// <returns></returns> public override int Parse(string[] args, int idx, out Command command) { CommandFilterStyle commandFilterStyle = new CommandFilterStyle(); // check next argument. if (args.Length < idx) { throw new CommandLineParserException("None", "Invalid arguments for --write-scene!"); } // parse arguments and keep parsing until the next switch. int startIdx = idx; while (args.Length > idx && !CommandParser.IsSwitch(args[idx])) { string[] keyValue; if (CommandParser.SplitKeyValue(args[idx], out keyValue)) { // the command splitting succeeded. keyValue[0] = CommandParser.RemoveQuotes(keyValue[0]); keyValue[1] = CommandParser.RemoveQuotes(keyValue[1]); switch (keyValue[0].ToLower()) { case "file": commandFilterStyle.File = keyValue[1]; break; case "type": string typeValue = keyValue[1].ToLower(); switch (typeValue) { case "mapcss": commandFilterStyle.StyleType = StyleType.MapCSS; break; } break; default: // the command splitting succeed but one of the arguments is unknown. throw new CommandLineParserException("--filter-style", string.Format("Invalid parameter for command --filter-style: {0} not recognized.", keyValue[0])); } } else { // the command splitting failed and this is not a switch. throw new CommandLineParserException("--write-scene", "Invalid parameter for command --write-scene."); } idx++; // increase the index. } command = commandFilterStyle; return 2; }
/// <summary> /// Parses one commands and returns the number of eaten arguments. /// </summary> /// <param name="args"></param> /// <param name="idx"></param> /// <param name="command"></param> /// <returns></returns> private static int ParseCommand(string[] args, int idx, out Command command) { int eatenArguments = 1; // eat the first argument. string switchCommand = args[idx]; // get the switch command. // make sure no caps mess things up. switchCommand = switchCommand.ToLower(); switchCommand = switchCommand.Trim(); // loop over commands and try to parse. foreach(var current in _commands) { if(current.HasSwitch(switchCommand)) { // this command has the be the one! eatenArguments = eatenArguments + current.Parse(args, idx + 1, out command); return eatenArguments; } } throw new CommandLineParserException(args[idx], "Switch not found!"); }