/// <summary> Provides the standard type cohersion between types </summary> protected Object ChangeType(Object value, Type type, bool required, Object defaultValue) { if (value == null) { InterpreterException.Assert(required == false, "The value for {0} is required.", this.DisplayName); value = defaultValue; } InterpreterException.Assert(value != null || !type.IsValueType, "Can not set value of type {0} to null in {1}.", type, this.DisplayName); if (value != null) { try { if (type.IsEnum && value is string) { value = Enum.Parse(type, value as string, true); } else if (!type.IsAssignableFrom(value.GetType())) { value = Convert.ChangeType(value, type); } } catch (FormatException f) { throw new InterpreterException(f.Message, f); } } return(value); }
public virtual void Run(ICommandInterpreter interpreter, string[] arguments) { ArgumentList args = new ArgumentList(arguments); if (args.Count == 1 && args.Contains("?")) { Help(); return; } //translate ordinal referenced names for (int i = 0; i < _arguments.Length && args.Unnamed.Count > 0; i++) { args.Add(_arguments[i].DisplayName, args.Unnamed[0]); args.Unnamed.RemoveAt(0); } List <object> invokeArgs = new List <object>(); foreach (Argument arg in _arguments) { object argValue = arg.GetArgumentValue(interpreter, args, arguments); invokeArgs.Add(argValue); } //make sure we actually used all arguments. List <string> names = new List <string>(args.Keys); InterpreterException.Assert(names.Count == 0, "Unknown argument(s): {0}", String.Join(", ", names.ToArray())); InterpreterException.Assert(args.Unnamed.Count == 0, "Too many arguments supplied."); Invoke(Method, Target, invokeArgs.ToArray()); }
/// <summary> Manually adds an option </summary> public void AddOption(IOption option) { foreach (string key in option.AllNames) { InterpreterException.Assert(false == _options.ContainsKey(key), "Option {0} already exists.", key); _options.Add(key, option); } }
public object Get(string property) { IOption opt; InterpreterException.Assert(_options.TryGetValue(property, out opt), "The option {0} was not found.", property); object value = opt.Value; Console.Out.WriteLine("{0}", value); return(value); }
/// <summary> /// Expands '$(OptionName)' within the input string to the named option's value. /// </summary> public string ExpandOptions(string input) { // replaces $(OptionName) with value of OptionName return(StringUtils.Transform(input, _optionName, delegate(Match m) { string optionName = m.Groups["Name"].Value; InterpreterException.Assert(_options.ContainsKey(optionName), "Unknown option specified: {0}", optionName); return String.Format("{0}", _options[optionName].Value); } ).Replace("$$", "$")); }
/// <summary> Manually adds a command </summary> public void AddCommand(ICommand command) { foreach (string key in command.AllNames) { if (String.IsNullOrEmpty(key)) { continue; } InterpreterException.Assert(false == _commands.ContainsKey(key), "Command {0} already exists.", key); _commands.Add(key, command); } }
/// <summary> Provides the standard type cohersion between types </summary> protected Object ChangeType(Object value, Type type, bool required, Object defaultValue) { if (value == null) { InterpreterException.Assert(required == false, "The value for {0} is required.", this.DisplayName); value = defaultValue; } InterpreterException.Assert(value != null || !type.IsValueType, "Can not set value of type {0} to null in {1}.", type, this.DisplayName); if (value != null) { if (!type.IsAssignableFrom(value.GetType())) { value = Convert.ChangeType(value, type); } } return(value); }
/// <summary> Provides the standard type cohersion between types </summary> protected Object ChangeType(Object value, Type type, bool required, Object defaultValue) { if (value == null) { InterpreterException.Assert(required == false, "The value for {0} is required.", this.DisplayName); value = defaultValue; } InterpreterException.Assert(value != null || !type.IsValueType || Nullable.GetUnderlyingType(type) != null, "Can not set value of type {0} to null in {1}.", type, this.DisplayName); if (value != null) { try { var targetType = !type.IsValueType ? type : (Nullable.GetUnderlyingType(type) ?? type); if (targetType.IsEnum && value is string) { value = Enum.Parse(targetType, value as string, true); } if (targetType.IsEnum && Enum.IsDefined(targetType, value)) { value = Enum.ToObject(targetType, value); } else if (!targetType.IsAssignableFrom(value.GetType())) { value = Convert.ChangeType(value, targetType); } } catch (FormatException fe) { throw new InterpreterException(fe.Message, fe); } catch (ArgumentException ae) { throw new InterpreterException(ae.Message, ae); } } return(value); }
/// <summary> /// The last link in the command chain /// </summary> internal void ProcessCommand(string[] arguments) { if (Check.NotNull(arguments).Length == 0) { Help(null); return; } string commandName = arguments[0]; ICommand command; InterpreterException.Assert(_commands.TryGetValue(commandName, out command), "Invalid command name: {0}", commandName); List <string> args = new List <string>(); for (int i = 1; i < arguments.Length; i++) { args.Add(ExpandOptions(arguments[i])); } command.Run(this, args.ToArray()); }
public void Set([DefaultValue(null)] string property, [DefaultValue(null)] object value, [DefaultValue(false), Description("Read from std::in lines formatted as NAME=VALUE")] bool readInput) { if (readInput) { string line; while (null != (line = Console.In.ReadLine())) { Set(line, null, false); } return; } if (property == null) { foreach (IOption opt in Options) { Console.WriteLine("{0}={1}", opt.DisplayName, opt.Value); } return; } else if (value == null && property.IndexOf('=') < 0) { Get(property); return; } else if (value == null) { string[] args = property.Split(new char[] { '=' }, 2); property = args[0].TrimEnd(); value = args[1].TrimStart(); } IOption option; InterpreterException.Assert(_options.TryGetValue(property, out option), "The option {0} was not found.", property); option.Value = value; }
public DisplayInfoBase(object target, ICustomAttributeProvider mi) { _target = target; _member = mi; if (mi is MethodInfo) { _name = ((MethodInfo)mi).Name; } else if (mi is ParameterInfo) { _name = ((ParameterInfo)mi).Name; } else if (mi is PropertyInfo) { _name = ((PropertyInfo)mi).Name; } InterpreterException.Assert(_name != null, "Unknown type " + mi.ToString()); _reflectedType = target == null ? null : target is Type ? (Type)target : target.GetType(); _description = _member.ToString(); _category = _target.GetType().Name; _visible = true; foreach (DisplayNameAttribute a in _member.GetCustomAttributes(typeof(DisplayNameAttribute), true)) { _name = a.DisplayName; } foreach (DescriptionAttribute a in _member.GetCustomAttributes(typeof(DescriptionAttribute), true)) { _description = String.Format("{0}", a.Description); } foreach (CategoryAttribute a in _member.GetCustomAttributes(typeof(CategoryAttribute), true)) { _category = String.Format("{0}", a.Category); } foreach (BrowsableAttribute a in _member.GetCustomAttributes(typeof(BrowsableAttribute), true)) { _visible = a.Browsable; } List <string> names = new List <string>(); foreach (DisplayInfoAttribute a in mi.GetCustomAttributes(typeof(DisplayInfoAttribute), true)) { if (!String.IsNullOrEmpty(a.DisplayName)) { _name = a.DisplayName; } names.AddRange(a.AliasNames); if (!String.IsNullOrEmpty(a.Description)) { _description = a.Description; } if (!String.IsNullOrEmpty(a.Category)) { _category = a.Category; } _visible &= a.Visible; } names.Insert(0, _name); foreach (AliasNameAttribute a in _member.GetCustomAttributes(typeof(AliasNameAttribute), true)) { names.Add(a.Name); } _allNames = names.ToArray(); try { _attributes = mi.GetCustomAttributes(true); } catch { _attributes = new object[0]; } }