public int Execute(params string[] args) { CommandLineApplication command = this; CommandOption option = null; IEnumerator <CommandArgument> arguments = null; var argumentsAssigned = false; for (var index = 0; index < args.Length; index++) { var arg = args[index]; var processed = false; if (!processed && option == null) { string[] longOption = null; string[] shortOption = null; if (arg.StartsWith("--", StringComparison.Ordinal)) { longOption = arg.Substring(2).Split(new[] { ':', '=' }, 2); } else if (arg.StartsWith("-", StringComparison.Ordinal)) { shortOption = arg.Substring(1).Split(new[] { ':', '=' }, 2); } if (longOption != null) { processed = true; var longOptionName = longOption[0]; option = command.GetOptions().SingleOrDefault(opt => string.Equals(opt.LongName, longOptionName, StringComparison.Ordinal)); if (option == null && _treatUnmatchedOptionsAsArguments) { if (arguments == null) { arguments = new CommandArgumentEnumerator(command.Arguments.GetEnumerator()); } if (arguments.MoveNext()) { processed = true; arguments.Current.Values.Add(arg); argumentsAssigned = true; continue; } //else //{ // argumentsAssigned = false; //} } if (option == null) { var ignoreContinueAfterUnexpectedArg = false; if (string.IsNullOrEmpty(longOptionName) && !command._throwOnUnexpectedArg && AllowArgumentSeparator) { // Skip over the '--' argument separator then consume all remaining arguments. All // remaining arguments are unconditionally stored for further use. index++; ignoreContinueAfterUnexpectedArg = true; } if (HandleUnexpectedArg( command, args, index, argTypeName: "option", ignoreContinueAfterUnexpectedArg)) { continue; } break; } // If we find a help/version option, show information and stop parsing if (command.OptionHelp == option) { command.ShowHelp(); return(0); } else if (command.OptionVersion == option) { command.ShowVersion(); return(0); } if (longOption.Length == 2) { if (!option.TryParse(longOption[1])) { command.ShowHint(); throw new CommandParsingException(command, $"Unexpected value '{longOption[1]}' for option '{option.LongName}'"); } option = null; } else if (option.OptionType == CommandOptionType.NoValue) { // No value is needed for this option option.TryParse(null); option = null; } } if (shortOption != null) { processed = true; option = command.GetOptions().SingleOrDefault(opt => string.Equals(opt.ShortName, shortOption[0], StringComparison.Ordinal)); if (option == null && _treatUnmatchedOptionsAsArguments) { if (arguments == null) { arguments = new CommandArgumentEnumerator(command.Arguments.GetEnumerator()); } if (arguments.MoveNext()) { processed = true; arguments.Current.Values.Add(arg); argumentsAssigned = true; continue; } //else //{ // argumentsAssigned = false; //} } // If not a short option, try symbol option if (option == null) { option = command.GetOptions().SingleOrDefault(opt => string.Equals(opt.SymbolName, shortOption[0], StringComparison.Ordinal)); } if (option == null) { if (HandleUnexpectedArg(command, args, index, argTypeName: "option")) { continue; } break; } // If we find a help/version option, show information and stop parsing if (command.OptionHelp == option) { command.ShowHelp(); return(0); } else if (command.OptionVersion == option) { command.ShowVersion(); return(0); } if (shortOption.Length == 2) { if (!option.TryParse(shortOption[1])) { command.ShowHint(); throw new CommandParsingException(command, $"Unexpected value '{shortOption[1]}' for option '{option.LongName}'"); } option = null; } else if (option.OptionType == CommandOptionType.NoValue) { // No value is needed for this option option.TryParse(null); option = null; } } } if (!processed && option != null) { processed = true; if (!option.TryParse(arg)) { command.ShowHint(); throw new CommandParsingException(command, $"Unexpected value '{arg}' for option '{option.LongName}'"); } option = null; } if (!processed && !argumentsAssigned) { var currentCommand = command; foreach (var subcommand in command.Commands) { if (string.Equals(subcommand.Name, arg, StringComparison.OrdinalIgnoreCase)) { processed = true; command = subcommand; break; } } // If we detect a subcommand if (command != currentCommand) { processed = true; } } if (!processed) { if (arguments == null) { arguments = new CommandArgumentEnumerator(command.Arguments.GetEnumerator()); } if (arguments.MoveNext()) { processed = true; arguments.Current.Values.Add(arg); } } if (!processed) { if (HandleUnexpectedArg(command, args, index, argTypeName: "command or argument")) { continue; } break; } } if (option != null) { command.ShowHint(); throw new CommandParsingException(command, $"Missing value for option '{option.LongName}'"); } return(command.Invoke()); }
public int Execute(params string[] args) { CommandLineApplication command = this; CommandOption option = null; IEnumerator <CommandArgument> arguments = null; for (var index = 0; index < args.Length; index++) { var arg = args[index]; var processed = false; if (!processed && option == null) { string[] longOption = null; string[] shortOption = null; if (arg.StartsWith("--")) { longOption = arg.Substring(2).Split(new[] { ':', '=' }, 2); } else if (arg.StartsWith("-")) { shortOption = arg.Substring(1).Split(new[] { ':', '=' }, 2); } if (longOption != null) { processed = true; option = command.Options.SingleOrDefault(opt => string.Equals(opt.LongName, longOption[0], StringComparison.Ordinal)); if (option == null) { HandleUnexpectedArg(command, args, index, argTypeName: "option"); break; } // If we find a help/version option, show information and stop parsing if (command.OptionHelp == option) { command.ShowHelp(); return(0); } else if (command.OptionVersion == option) { command.ShowVersion(); return(0); } if (longOption.Length == 2) { if (!option.TryParse(longOption[1])) { command.ShowHint(); throw new CommandParsingException(command, $"Unexpected value '{longOption[1]}' for option '{option.LongName}'"); } option = null; } else if (option.OptionType == CommandOptionType.NoValue) { // No value is needed for this option option.TryParse(null); option = null; } } if (shortOption != null) { processed = true; option = command.Options.SingleOrDefault(opt => string.Equals(opt.ShortName, shortOption[0], StringComparison.Ordinal)); // If not a short option, try symbol option if (option == null) { option = command.Options.SingleOrDefault(opt => string.Equals(opt.SymbolName, shortOption[0], StringComparison.Ordinal)); } if (option == null) { HandleUnexpectedArg(command, args, index, argTypeName: "option"); break; } // If we find a help/version option, show information and stop parsing if (command.OptionHelp == option) { command.ShowHelp(); return(0); } else if (command.OptionVersion == option) { command.ShowVersion(); return(0); } if (shortOption.Length == 2) { if (!option.TryParse(shortOption[1])) { command.ShowHint(); throw new CommandParsingException(command, $"Unexpected value '{shortOption[1]}' for option '{option.LongName}'"); } option = null; } else if (option.OptionType == CommandOptionType.NoValue) { // No value is needed for this option option.TryParse(null); option = null; } } } if (!processed && option != null) { processed = true; if (!option.TryParse(arg)) { command.ShowHint(); throw new CommandParsingException(command, $"Unexpected value '{arg}' for option '{option.LongName}'"); } option = null; } if (!processed && arguments == null) { var currentCommand = command; foreach (var subcommand in command.Commands) { if (string.Equals(subcommand.Name, arg, StringComparison.OrdinalIgnoreCase)) { processed = true; command = subcommand; break; } } // If we detect a subcommand if (command != currentCommand) { processed = true; } } if (!processed) { if (arguments == null) { arguments = new CommandArgumentEnumerator(command.Arguments.GetEnumerator()); } if (arguments.MoveNext()) { processed = true; arguments.Current.Values.Add(arg); } } if (!processed) { HandleUnexpectedArg(command, args, index, argTypeName: "command or argument"); break; } } if (option != null) { command.ShowHint(); throw new CommandParsingException(command, $"Missing value for option '{option.LongName}'"); } return(command.Invoke()); }
//public void OnExecute(Func<Task<int>> invoke) //{ // this.Invoke = (Func<int>)(() => invoke().Result); //} public virtual int Execute(params string[] args) { CommandLineApplication command1 = this; CommandOption commandOption = (CommandOption)null; IEnumerator <CommandArgument> enumerator = (IEnumerator <CommandArgument>)null; for (int index = 0; index < args.Length; ++index) { string b = args[index]; bool flag = false; if (!flag && commandOption == null) { string[] strArray = (string[])null; string[] shortOption = (string[])null; if (b.StartsWith("--")) { strArray = b.Substring(2).Split(new char[2] { ':', '=' }, 2); } else if (b.StartsWith("-")) { shortOption = b.Substring(1).Split(new char[2] { ':', '=' }, 2); } if (strArray != null) { flag = true; string longOptionName = strArray[0]; commandOption = command1.GetOptions().SingleOrDefault <CommandOption>((Func <CommandOption, bool>)(opt => string.Equals(opt.LongName, longOptionName, StringComparison.Ordinal))); if (commandOption == null) { if (string.IsNullOrEmpty(longOptionName) && !command1._throwOnUnexpectedArg && this.AllowArgumentSeparator) { ++index; } this.HandleUnexpectedArg(command1, args, index, "option"); break; } if (command1.OptionHelp == commandOption) { command1.ShowHelp((string)null); return(0); } if (command1.OptionVersion == commandOption) { command1.ShowVersion(); return(0); } if (strArray.Length == 2) { if (!commandOption.TryParse(strArray[1])) { command1.ShowHint(); throw new CommandParsingException(command1, string.Format("Unexpected value '{0}' for option '{1}'", (object)strArray[1], (object)commandOption.LongName)); } commandOption = (CommandOption)null; } else if (commandOption.OptionType == CommandOptionType.NoValue) { commandOption.TryParse((string)null); commandOption = (CommandOption)null; } } if (shortOption != null) { flag = true; commandOption = command1.GetOptions().SingleOrDefault <CommandOption>((Func <CommandOption, bool>)(opt => string.Equals(opt.ShortName, shortOption[0], StringComparison.Ordinal))) ?? command1.GetOptions().SingleOrDefault <CommandOption>((Func <CommandOption, bool>)(opt => string.Equals(opt.SymbolName, shortOption[0], StringComparison.Ordinal))); if (commandOption == null) { this.HandleUnexpectedArg(command1, args, index, "option"); break; } if (command1.OptionHelp == commandOption) { command1.ShowHelp((string)null); return(0); } if (command1.OptionVersion == commandOption) { command1.ShowVersion(); return(0); } if (shortOption.Length == 2) { if (!commandOption.TryParse(shortOption[1])) { command1.ShowHint(); throw new CommandParsingException(command1, string.Format("Unexpected value '{0}' for option '{1}'", (object)shortOption[1], (object)commandOption.LongName)); } commandOption = (CommandOption)null; } else if (commandOption.OptionType == CommandOptionType.NoValue) { commandOption.TryParse((string)null); commandOption = (CommandOption)null; } } } if (!flag && commandOption != null) { flag = true; if (!commandOption.TryParse(b)) { command1.ShowHint(); throw new CommandParsingException(command1, string.Format("Unexpected value '{0}' for option '{1}'", (object)b, (object)commandOption.LongName)); } commandOption = (CommandOption)null; } if (!flag && enumerator == null) { CommandLineApplication commandLineApplication = command1; foreach (CommandLineApplication command2 in command1.Commands) { if (string.Equals(command2.Name, b, StringComparison.OrdinalIgnoreCase)) { flag = true; command1 = command2; break; } } if (command1 != commandLineApplication) { flag = true; } } if (!flag) { if (enumerator == null) { enumerator = (IEnumerator <CommandArgument>) new CommandLineApplication.CommandArgumentEnumerator((IEnumerator <CommandArgument>)command1.Arguments.GetEnumerator()); } if (enumerator.MoveNext()) { flag = true; enumerator.Current.Values.Add(b); } } if (!flag) { this.HandleUnexpectedArg(command1, args, index, "command or argument"); break; } } if (commandOption != null) { command1.ShowHint(); throw new CommandParsingException(command1, string.Format("Missing value for option '{0}'", (object)commandOption.LongName)); } return(command1.Invoke()); }