public void Dispatch(string input) { // Skip blank lines. if (string.IsNullOrWhiteSpace(input)) return; // Split into tokens by whitespace. var tokens = input.Split(); // Command name is first token. ICommand command; if (!commandRegistry.TryGetCommand(tokens[0], out command)) { output.WriteLine(Purpose.Error, "Unrecognized command \"{0}\".", tokens[0]); return; } // Feed args to command. var context = new LoadParamsContext(tokens, 1, tokens.Length - 1); command.LoadParams(context); if (context.Failed) { output.WriteLine( Purpose.Error, "Invalid args \"{0}\" for command \"{1}\"", input, command.Usage); using (output.Indent()) { foreach (var error in context.Errors) output.WriteLine(Purpose.Error, "- {0}", error); } return; } // Execute. messageBoard.Send(command); }
protected static void OptionalResourceName(LoadParamsContext context, int index, out string resourceName) { resourceName = null; if (context.TokenCount <= index) return; if (string.IsNullOrWhiteSpace(context[index])) context.ReportError("Resource name cannot be empty."); else resourceName = context[index]; }
protected static void OptionalProcessName(LoadParamsContext context, int index, out string processName, bool singleCharNamesOnly = true) { processName = null; if (context.TokenCount <= index) return; if (string.IsNullOrWhiteSpace(context[index])) context.ReportError("Process name cannot be empty."); else if (singleCharNamesOnly && new StringInfo(context[index]).LengthInTextElements > 1) context.ReportError("Process name can be at most 1 character."); else processName = context[index]; }
protected static void RequiredCount(LoadParamsContext context, int index, out int count) { if (context.TokenCount <= index) count = 1; // Default value if not provided instead of error. else if (!int.TryParse(context[index], NumberStyles.None, CultureInfo.InvariantCulture, out count)) context.ReportError("Failed to parse count as an integer value."); else if (count < 1) context.ReportError("Count cannot be less than 1."); }
public abstract void LoadParams(LoadParamsContext context);
public override void LoadParams(LoadParamsContext context) { }
public override void LoadParams(LoadParamsContext context) { OptionalResourceName(context, 0, out resourceName); }
public override void LoadParams(LoadParamsContext context) { OptionalProcessName(context, 0, out processName, false); }
public override void LoadParams(LoadParamsContext context) { RequiredResourceName(context, 0, out resourceName); RequiredCount(context, 1, out count); }
public override void LoadParams(LoadParamsContext context) { RequiredProcessName(context, 0, out processName); }
protected static void RequiredResourceName(LoadParamsContext context, int index, out string resourceName) { resourceName = null; if (context.TokenCount <= index) context.ReportError("No argument provided for resource name."); else if (string.IsNullOrWhiteSpace(context[index])) context.ReportError("Resource name cannot be empty."); else resourceName = context[index]; }
protected static void RequiredPriority(LoadParamsContext context, int index, out int priority) { priority = 0; if (context.TokenCount <= index) context.ReportError("No argument provided for priority."); else if (!int.TryParse(context[index], NumberStyles.None, CultureInfo.InvariantCulture, out priority)) context.ReportError("Failed to parse priority as an integer value."); else if (priority != 1 && priority != 2) context.ReportError("Invalid priority, must be 1 or 2."); }