public void ApplyTo(ConsoleCommand command) { command.HasOption("e=", "Endpoint url used for requests.", v => Endpoint = v); Credentials = new ApiCredentialParameters(); Credentials.ApplyTo(command); }
public void AddXmlRpcLogin(ConsoleCommand command) { command.HasOption("rpc=", "Url of wordpress XML-RPC endpoint (example: http://joeblogstest.alexjamesbrown.com/xmlrpc.php)", v => XmlRpcUrl = v); command.HasOption("u=", "Username", v => Username = v); command.HasOption("p=", "Password", v => Password = v); }
public void ApplyTo(ConsoleCommand command) { FindAndLoadCredentialsIfAvailable(); if (string.IsNullOrEmpty(Username)) { ApplyWithoutCheckingFile(command); } else { command.HasOption("u=", string.Format("Username (default is {0})", Username), v => Username = v); command.HasOption("s=", string.Format("Secret key (default is for {0})", Username), v => Secret = v); } }
static int Main(string[] args) { #if x64 if (!Environment.Is64BitProcess) throw new Exception("This runner is expected to run 64bit"); #endif var commands = new ConsoleCommand[] { new RunAloneCommand(), new RunSatelliteCommand(), }; return ConsoleCommandDispatcher.DispatchCommand(commands, args, Console.Out); }
private static bool CommandMatchesArgument(ConsoleCommand command, string arg) { if (String.IsNullOrEmpty(arg)) { return(false); } if (arg.Equals(command.Command, StringComparison.OrdinalIgnoreCase)) { return(true); } else if (command.Aliases != null && command.Aliases.Count > 0) { foreach (string alias in command.Aliases) { if (arg.Equals(alias, StringComparison.OrdinalIgnoreCase)) { return(true); } } } return(false); }
public static int DispatchCommand(IEnumerable <ConsoleCommand> commands, string[] arguments, TextWriter consoleOut, bool skipExeInExpectedUsage = false) { ConsoleCommand selectedCommand = null; TextWriter console = consoleOut; IEnumerable <ConsoleCommand> consoleCommands = commands as ConsoleCommand[] ?? commands.ToArray(); foreach (var command in consoleCommands) { ValidateConsoleCommand(command); } try { List <string> remainingArguments; if (consoleCommands.Count() == 1) { selectedCommand = consoleCommands.First(); if (arguments.Any() && (arguments.First().ToLower() == selectedCommand.Command.ToLower())) { remainingArguments = selectedCommand.GetActualOptions().Parse(arguments.Skip(1)); } else { remainingArguments = selectedCommand.GetActualOptions().Parse(arguments); } } else { if (!arguments.Any()) { throw new ConsoleHelpAsException("No arguments specified."); } if (arguments[0].Equals("help", StringComparison.InvariantCultureIgnoreCase)) { selectedCommand = GetMatchingCommand(consoleCommands, arguments.Skip(1).FirstOrDefault()); if (selectedCommand == null) { ConsoleHelp.ShowSummaryOfCommands(consoleCommands, console); } else { ConsoleHelp.ShowCommandHelp(selectedCommand, console, skipExeInExpectedUsage); } return(-1); } selectedCommand = GetMatchingCommand(consoleCommands, arguments.First()); if (selectedCommand == null) { throw new ConsoleHelpAsException("Command name not recognized."); } remainingArguments = selectedCommand.GetActualOptions().Parse(arguments.Skip(1)); } selectedCommand.CheckRequiredArguments(); CheckRemainingArguments(remainingArguments, selectedCommand.RemainingArgumentsCount); var preResult = selectedCommand.OverrideAfterHandlingArgumentsBeforeRun(remainingArguments.ToArray()); if (preResult.HasValue) { return(preResult.Value); } ConsoleHelp.ShowParsedCommand(selectedCommand, console); return(selectedCommand.Run(remainingArguments.ToArray())); } catch (ConsoleHelpAsException e) { return(DealWithException(e, console, skipExeInExpectedUsage, selectedCommand, consoleCommands)); } catch (NDesk.Options.OptionException e) { return(DealWithException(e, console, skipExeInExpectedUsage, selectedCommand, consoleCommands)); } }
public static int DispatchCommand(ConsoleCommand command, string[] arguments, TextWriter consoleOut) { return(DispatchCommand(new [] { command }, arguments, consoleOut)); }
private static int DealWithException(Exception e, TextWriter console, bool skipExeInExpectedUsage, ConsoleCommand selectedCommand, IEnumerable <ConsoleCommand> commands) { if (selectedCommand != null) { console.WriteLine(); console.WriteLine(e.Message); ConsoleHelp.ShowCommandHelp(selectedCommand, console, skipExeInExpectedUsage); } else { ConsoleHelp.ShowSummaryOfCommands(commands, console); } return(-1); }
public void ApplyWithoutCheckingFile(ConsoleCommand command) { command.HasRequiredOption("u=", "Username", v => Username = v); command.HasRequiredOption("s=", "Secret key", v => Secret = v); }
public static int DispatchCommand(IEnumerable <ConsoleCommand> commands, string[] arguments, TextWriter consoleOut) { ConsoleCommand selectedCommand = null; TextWriter console = consoleOut; try { List <string> remainingArguments; if (commands.Count() == 1) { selectedCommand = commands.First(); CheckCommandProperty(selectedCommand); if (arguments.Count() > 0 && arguments.First().ToLower() == selectedCommand.Command.ToLower()) { remainingArguments = selectedCommand.Options.Parse(arguments.Skip(1)); } else { remainingArguments = selectedCommand.Options.Parse(arguments); } } else { if (arguments.Count() < 1) { throw new ConsoleHelpAsException("No arguments specified."); } foreach (var possibleCommand in commands) { CheckCommandProperty(possibleCommand); if (arguments.First().ToLower() == possibleCommand.Command.ToLower()) { selectedCommand = possibleCommand; break; } } if (selectedCommand == null) { throw new ConsoleHelpAsException("Command name not recognized."); } remainingArguments = selectedCommand.Options.Parse(arguments.Skip(1)); } CheckRequiredArguments(selectedCommand); CheckRemainingArguments(remainingArguments, selectedCommand.RemainingArgumentsCount); ConsoleHelp.ShowParsedCommand(selectedCommand, console); return(selectedCommand.Run(remainingArguments.ToArray())); } catch (Exception e) { if (!ConsoleHelpAsException.WriterErrorMessage(e, console)) { throw; } console.WriteLine(); if (selectedCommand != null) { if (e is ConsoleHelpAsException || e is NDesk.Options.OptionException) { ConsoleHelp.ShowCommandHelp(selectedCommand, console); } } else { ConsoleHelp.ShowSummaryOfCommands(commands, console); } return(-1); } }
private static void ValidateConsoleCommand(ConsoleCommand command) { if (string.IsNullOrEmpty(command.Command)) { throw new InvalidOperationException(String.Format( "Command {0} did not call IsCommand in its constructor to indicate its name and description.", command.GetType().Name)); } }
public static int DispatchCommand(ConsoleCommand command, string[] arguments, TextWriter consoleOut) { return DispatchCommand(new [] {command}, arguments, consoleOut); }
private static int DealWithException(Exception e, TextWriter console, bool skipExeInExpectedUsage, ConsoleCommand selectedCommand, IEnumerable<ConsoleCommand> commands) { if (selectedCommand != null) { console.WriteLine(); console.WriteLine(e.Message); ConsoleHelp.ShowCommandHelp(selectedCommand, console, skipExeInExpectedUsage); } else { ConsoleHelp.ShowSummaryOfCommands(commands, console); } return -1; }
private static (ConsoleCommand, string[], int?) GetSelectedCommand(IEnumerable <ConsoleCommand> commands, string[] arguments, TextWriter consoleOut, bool skipExeInExpectedUsage = false) { ConsoleCommand selectedCommand = null; List <string> remainingArguments; TextWriter console = consoleOut; foreach (var command in commands) { ValidateConsoleCommand(command); } try { if (commands.Count() == 1) { selectedCommand = commands.First(); if (arguments.Count() > 0 && CommandMatchesArgument(selectedCommand, arguments.First())) { remainingArguments = selectedCommand.GetActualOptions().Parse(arguments.Skip(1)); } else { remainingArguments = selectedCommand.GetActualOptions().Parse(arguments); } } else { if (arguments.Count() < 1) { throw new ConsoleHelpAsException("No arguments specified."); } if (arguments[0].Equals("help", StringComparison.InvariantCultureIgnoreCase)) { selectedCommand = GetMatchingCommand(commands, arguments.Skip(1).FirstOrDefault()); if (selectedCommand == null) { ConsoleHelp.ShowSummaryOfCommands(commands, console); } else { ConsoleHelp.ShowCommandHelp(selectedCommand, console, skipExeInExpectedUsage); } return(null, null, -1); } selectedCommand = GetMatchingCommand(commands, arguments.First()); if (selectedCommand == null) { throw new ConsoleHelpAsException("Command name not recognized."); } remainingArguments = selectedCommand.GetActualOptions().Parse(arguments.Skip(1)); } selectedCommand.CheckRequiredArguments(); CheckRemainingArguments(remainingArguments, selectedCommand.RemainingArgumentsCountMin, selectedCommand.RemainingArgumentsCountMax); var preResult = selectedCommand.OverrideAfterHandlingArgumentsBeforeRun(remainingArguments.ToArray()); if (preResult.HasValue) { return(null, null, preResult); } ConsoleHelp.ShowParsedCommand(selectedCommand, console); return(selectedCommand, remainingArguments.ToArray(), null); } catch (ConsoleHelpAsException e) { return(null, null, DealWithException(e, console, skipExeInExpectedUsage, selectedCommand, commands)); } catch (Mono.Options.OptionException e) { return(null, null, DealWithException(e, console, skipExeInExpectedUsage, selectedCommand, commands)); } }
public static Task <int> DispatchCommandAsync(ConsoleCommand command, string[] arguments, TextWriter consoleOut) { return(DispatchCommandAsync(new[] { command }, arguments, consoleOut)); }