static void MainWithExceptions(string[] args) { // If no arguments are given, just show the help message. if (args.Length == 0) { Console.Write(helpMessage()); Environment.Exit(2); } // Parse the arguments. Dictionary <String, String> opts = new Dictionary <string, string>(); string name = null; foreach (string rawArg in args) { string arg = rawArg; // Transform the short names in to the long names. switch (arg) { case "-l": arg = "--list"; break; case "-d": arg = "--device"; break; case "-s": arg = "--status"; break; } Match m = Regex.Match(arg, "^--(.*)"); if (m.Success) { name = m.Groups[1].ToString(); opts[name] = ""; // start it off with no string value } else if (name != null) { // This argument is right after a -- argument, so this argument // is its value. opts[name] = arg; name = null; } else { throw new ArgumentException("Unexpected argument \"" + arg + "\"."); } } if (opts.ContainsKey("list")) { if (args.Length > 1) { throw new ArgumentException("If --list is present, it must be the only option."); } listDevices(); return; } // Otherwise, we have to connect to a device. List <DeviceListItem> list = Jrk.getConnectedDevices(); // Make sure there is a device available.. if (list.Count == 0) { throw new Exception("No jrks found."); } DeviceListItem item = null; if (!opts.ContainsKey("device")) { // No serial number specified: connect to the first item in the list. item = list[0]; } else { // Serial number specified. // Remove the leading # sign. It is not standard to put it there, // but if someone writes it, this program should still work. string check_serial_number = opts["device"].TrimStart('#'); // Find the device with the specified serial number. foreach (DeviceListItem check_item in list) { if (check_item.serialNumber == check_serial_number) { item = check_item; break; } } if (item == null) { throw new Exception("Could not find a jrk with serial number " + opts["device"] + ".\n" + "To list devices, use the --list option."); } } // Connect to the device. jrk = new Jrk(item); if (opts.ContainsKey("bootloader")) { jrk.startBootloader(); return; } if (opts.ContainsKey("restoredefaults")) { jrk.setJrkParameter(jrkParameter.PARAMETER_INITIALIZED, 0xFF); jrk.reinitialize(); Thread.Sleep(1000); } if (opts.ContainsKey("configure")) { string filename = opts["configure"]; Stream stream = File.Open(filename, FileMode.Open); StreamReader sr = new StreamReader(stream); ConfigurationFile.load(sr, jrk); sr.Close(); stream.Close(); jrk.reinitialize(); } if (opts.ContainsKey("getconf")) { string filename = opts["getconf"]; Stream stream = File.Open(filename, FileMode.Create); StreamWriter sw = new StreamWriter(stream); ConfigurationFile.save(sw, jrk); sw.Close(); stream.Close(); } if (opts.ContainsKey("clearerrors")) { jrk.clearErrors(); } if (opts.ContainsKey("target")) { UInt16 target = stringToU12(opts["target"]); jrk.setTarget(target); } if (opts.ContainsKey("run")) { UInt16 target = jrk.getVariables().target; jrk.setTarget(target); } if (opts.ContainsKey("stop")) { jrk.motorOff(); } if (opts.ContainsKey("status")) { displayStatus(jrk); } if (opts.ContainsKey("stream")) { streamVariables(jrk, opts); } jrk.disconnect(); }