/// <summary> /// Parse a string[] of command-line arguments into a ScanArgs object. /// </summary> /// <exception cref="ArgumentException">Throws when an invalid switch is passed.</exception> /// <param name="args">string[] of command-line arguments</param> /// <returns></returns> public static ScanArgs Parse(string[] args) { ScanArgs sargs = new ScanArgs(); if (args.Length == 0) { sargs.ShowHelp = true; } for (int i = 0; i < args.Length; i++) { //check if last arg is file or directory name (or at least not a switch starting with "-" if (i == args.Length - 1 && !args[i].StartsWith("-")) { sargs.FileSystem = FileSystemFactory.CreateInstance(args[i]); } else { switch (args[i]) { case FLAG_CLAMD_HOST_SHORT: goto case FLAG_CLAMD_HOST_LONG; case FLAG_CLAMD_HOST_LONG: if (args.Length > i + 1 && !args[i + 1].StartsWith("-")) { sargs.Host = args[++i]; } else { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, ResourceManagers.Strings.GetString(STR_PARSE_ARGUMENT_EXCEPTION_HOSTNAME), FLAG_CLAMD_HOST_SHORT, FLAG_CLAMD_HOST_LONG)); } break; case FLAG_CLAMD_PORT_SHORT: goto case FLAG_CLAMD_PORT_LONG; case FLAG_CLAMD_PORT_LONG: if (args.Length > i + 1 && !args[i + 1].StartsWith("-")) { try { sargs.Port = int.Parse(args[++i], CultureInfo.InvariantCulture); } catch (FormatException ex) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, ResourceManagers.Strings.GetString(STR_PARSE_ARGUMENT_EXCEPTION_PORT), ex.Message)); } catch (OverflowException ex) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, ResourceManagers.Strings.GetString(STR_PARSE_ARGUMENT_EXCEPTION_PORT), ex.Message)); } } else { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, ResourceManagers.Strings.GetString(STR_PARSE_ARGUMENT_EXCEPTION_PORT_MISSING), FLAG_CLAMD_PORT_SHORT, FLAG_CLAMD_PORT_LONG)); } break; case FLAG_VERSION_SHORT: goto case FLAG_VERSION_LONG; case FLAG_VERSION_LONG: sargs.ShowVersion = true; break; case FLAG_HELP_SHORT: goto case FLAG_HELP_LONG; case FLAG_HELP_LONG: sargs.ShowHelp = true; break; case FLAG_RECURSE_SHORT: goto case FLAG_RECURSE_LONG; case FLAG_RECURSE_LONG: sargs.Recurse = true; break; case FLAG_VERBOSE_SHORT: goto case FLAG_VERBOSE_LONG; case FLAG_VERBOSE_LONG: sargs.Verbose = true; break; default: throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, ResourceManagers.Strings.GetString(STR_PARSE_ARGUMENT_EXCEPTION_UNKNOWN), args[1])); } } } return(sargs); }