/// <summary> /// Parses commandline arguments into a generic CommandLineOptions object. /// /// Supported Format /// /// [parameter1 parameter2 parameter3] /flag /flagWithValue value /// [parameter1 parameter2 parameter3] -flag -flagWithValue value /// [parameter1 parameter2 parameter3] --flag --flagWithValue value /// /// /// Specifing a flag without a value is equally as setting it to true /// Example /// '-quiet' has the same effect '-quiet true' /// /// </summary> /// <param name="args"></param> /// <returns></returns> public static CommandLineOptions ParseCommandLineArgs(string[] args) { var commandLineOptions = new CommandLineOptions(); var flagParser = new Regex("^[/|-](.*)$"); string currentFlag = null; for (int i = 0; i != args.Length; ++i) { string current = args[i]; if (flagParser.IsMatch(current)) { if (currentFlag != null) { commandLineOptions.SetParameterEnabled(currentFlag); } currentFlag = flagParser.Match(current).Groups[1].Value; } else { // Simple Argument without any flag/id if (currentFlag == null) { commandLineOptions.AddArgument(current); } else { commandLineOptions.SetParameterValue(currentFlag, current); currentFlag = null; } } } // If last param is flag without param if (currentFlag != null) { commandLineOptions.SetParameterEnabled(currentFlag); } return commandLineOptions; }