private void SetSwitchValues(CommandContext context) { if (context.Switches != null && context.Switches.Any()) { foreach (var commandSwitch in context.Switches) { SetSwitchValue(commandSwitch); } } }
private void Invoke(CommandContext context) { CheckMethodForSwitches(context.CommandDescriptor.MethodInfo, context.Switches); var arguments = (context.Arguments ?? Enumerable.Empty<string>()).ToArray(); object[] invokeParameters = GetInvokeParametersForMethod(context.CommandDescriptor.MethodInfo, arguments); if (invokeParameters == null) { throw new InvalidOperationException(T("Command arguments \"{0}\" don't match command definition", string.Join(" ", arguments)).ToString()); } this.Context = context; var result = context.CommandDescriptor.MethodInfo.Invoke(this, invokeParameters); if (result is string) context.Output.Write(result); }
public void Execute(CommandContext context) { SetSwitchValues(context); Invoke(context); }
private void SetSwitchValues(CommandContext context) { if (context.Switches != null && context.Switches.Any()) { foreach (var commandSwitch in context.Switches.Keys) { PropertyInfo propertyInfo = GetType().GetProperty(commandSwitch); if (propertyInfo == null) { throw new InvalidOperationException(T("Switch : ") + commandSwitch + T(" was not found")); } if (propertyInfo.GetCustomAttributes(typeof(OrchardSwitchAttribute), false).Length == 0) { throw new InvalidOperationException(T("A property of the name ") + commandSwitch + T(" exists but is not decorated with the OrchardSwitch attribute")); } string stringValue = context.Switches[commandSwitch]; if (propertyInfo.PropertyType.IsAssignableFrom(typeof(bool))) { bool boolValue; Boolean.TryParse(stringValue, out boolValue); propertyInfo.SetValue(this, boolValue, null); } else if (propertyInfo.PropertyType.IsAssignableFrom(typeof(int))) { int intValue; Int32.TryParse(stringValue, out intValue); propertyInfo.SetValue(this, intValue, null); } else if (propertyInfo.PropertyType.IsAssignableFrom(typeof(string))) { propertyInfo.SetValue(this, stringValue, null); } else { throw new InvalidOperationException(T("No property named ") + commandSwitch + T(" found of type bool, int or string")); } } } }