public CommandLineHelper(ArgumentCommand command, List <ArgumentOption> options, ProcessDelegate usage, ProcessDelegate emptyArguments, bool acceptEmptyArguments /*, bool isInSpecificOrder*/) { Command = command; if (options == null) { options = new List <ArgumentOption>(); } Options = options; Usage = usage; EmptyArguments = emptyArguments; // IsInSpecificOrder = isInSpecificOrder; AcceptEmptyArguments = acceptEmptyArguments; }
public void Process(string[] args) { // If there are no args, try to call EmptyArguments if (args.Length == 0) { if (!AcceptEmptyArguments) { throw new ArgumentException("You must enter some argument! Enter -? for usage."); } if (EmptyArguments == null) { throw new ArgumentException("The EmptyArguments method must be set!"); } EmptyArguments(); return; } // Show usage options, if Usage delegate is defined if (Usage != null) { foreach (var arg in args) { if (arg == "-?" || arg == "/?" || arg.ToLower() == "-h" || arg.ToLower() == "--help") { Usage(); return; } } } // Process options and commands IArgument currentArgument = null; var expectedArgs = 0; var commandList = new List <IArgument>(); var optionList = new Dictionary <string, IArgument>(); var errorMessageStr = new StringBuilder(); foreach (var arg in args) { // Verify if it is a option and remove prefixes from arg string argClean = CleanOptionArgument(arg); bool isOption = argClean != null; // If are not expecting some internal argument, begin processing options if (expectedArgs == 0) { // Process the option if (isOption) { // Search for the option in the Options list IArgument currentOption = GetCurrentOption(argClean); if (currentOption != null) { // If the option has already been added, generates an exception if (optionList.Count > 0 && optionList[currentOption.Name] != null) { throw new ArgumentException(string.Format("Could not process option '{0}' argument '{1}' twice! Enter -? for usage.", currentOption.Name, arg)); } // Add the option to the option list optionList[currentOption.Name] = currentOption; currentArgument = currentOption; expectedArgs = currentOption.InternalArguments.Count; continue; } // If option not found, then we got a wrong argument throw new ArgumentException(string.Format("Option '{0}' doesn't exist! Enter '-?' for usage.", arg)); } // Now process the Command, if exists if (Command != null) { // If the command doesn't allow multiple executions, generates an exception if (commandList.Count > 0 && !Command.AllowMultipleExecutions) { throw new ArgumentException(string.Format("Could not process command '{0}' argument '{1}' twice! Enter '-?' for usage.", Command.Name, arg)); } // Create a copy of internal arguments into a new list var internalArguments = Command.InternalArguments; List <ArgumentInternal> newInternalArguments = GetNewInternalArguments(internalArguments); // Set the first internal argument to arg newInternalArguments[0].Value = arg; // Create a new command and add to the commandList (to be executed after the options) var currentCommand = new ArgumentCommand(Command.Name, Command.Description, Command.AllowMultipleExecutions, newInternalArguments, Command.Process); commandList.Add(currentCommand); // Set the currentArgument to the new command and set the expectedArgs currentArgument = currentCommand; expectedArgs = newInternalArguments.Count - 1; continue; } // If it gets here, there is something wrong throw new ArgumentException(string.Format("Unespected argument '{0}'! Enter '-?' for usage.", arg)); } // Now we are expecting some internal argument, so set it's values to arg if (expectedArgs > 0 && currentArgument != null) { // If the argument is a option, there is something wrong if (isOption) { throw new ArgumentException(string.Format("Argument '{0}' invalid to option '{1}'! Enter '-?' for usage.", arg, currentArgument.Name)); } currentArgument.InternalArguments[currentArgument.InternalArguments.Count - expectedArgs].Value = arg; expectedArgs--; continue; } // If it gets here, there is something wrong throw new ArgumentException(string.Format("Unespected argument '{0}'! Enter '-?' for usage.", arg)); } // Process all the options, in the order that they were created foreach (var option in Options) { foreach (var optionKeyValue in optionList) { if (option.Name == optionKeyValue.Key) { try { RunCurrentProcess(optionKeyValue.Value); } catch (Exception e) { errorMessageStr.AppendLine(e.Message); } } } } // After processing all options, process the commands foreach (var command in commandList) { try { RunCurrentProcess(command); } catch (Exception e) { errorMessageStr.AppendLine(e.Message); } } // If any errors happened running the options or commands, throw an exception if (errorMessageStr.ToString() != "") { throw new ArgumentException(errorMessageStr.ToString()); } }
public CommandLineHelper(ArgumentCommand command, List <ArgumentOption> options, ProcessDelegate usage) : this(command, options, usage, null) { }
public CommandLineHelper(ArgumentCommand command, List <ArgumentOption> options, ProcessDelegate usage, ProcessDelegate emptyArguments) : this(command, options, usage, emptyArguments, true /*, false*/) { }