static void MainWithExceptions(string[] args) { if (args.Length == 0) { Console.Write(helpMessage()); Environment.Exit(1); } 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; case "-f": arg = "--force"; 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 = Programmer.getConnectedDevices(); // Make sure there is a device available.. if (list.Count == 0) { throw new Exception("No " + Programmer.englishName + "s 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, the 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 " + Programmer.englishName + " with serial number " + opts["device"] + ".\n"+ "To list devices, use the --list option."); } } Programmer programmer = new Programmer(item); if (opts.ContainsKey("bootloader")) { programmer.startBootloader(); return; } if (opts.ContainsKey("restoredefaults")) { programmer.restoreDefaultSettings(); } if (opts.ContainsKey("freq")) { setFrequency(programmer, opts["freq"]); } if (opts.ContainsKey("vddmin")) { programmer.setTargetVccAllowedMinimum(stringToMillivolts(opts["vddmin"])); } if (opts.ContainsKey("vddmaxrange")) { programmer.setTargetVccAllowedMaximumRange(stringToMillivolts(opts["vddmaxrange"])); } if (opts.ContainsKey("linea")) { programmer.setLineAIdentity(stringToLineIdentity(opts["linea"], opts.ContainsKey("force"))); } if (opts.ContainsKey("lineb")) { programmer.setLineBIdentity(stringToLineIdentity(opts["lineb"], opts.ContainsKey("force"))); } if (opts.ContainsKey("swminor")) { programmer.setSoftwareVersionMinor(stringToVersionNumber(opts["swminor"])); } if (opts.ContainsKey("swmajor")) { programmer.setSoftwareVersionMajor(stringToVersionNumber(opts["swmajor"])); } if (opts.ContainsKey("hw")) { programmer.setHardwareVersion(stringToVersionNumber(opts["hw"])); } if (opts.ContainsKey("status")) { displayStatus(programmer); } programmer.disconnect(); }