/// <summary> /// Class contructor. /// <see cref="ProgramOptionsBase"/> for additional information passed to ArgumentParser. /// </summary> /// <param name="programOptions">Instance of program options derived from ProgramOptionsBase.</param> /// <param name="output">TextWriter to output debug messages in debug mode.</param> internal ArgumentParser(ProgramOptionsBase programOptions, TextWriter output = null) { this.programOptions = programOptions; this.output = output; if (output == null) { this.output = System.Console.Out; } }
private bool SetEnumValue(string textValue, ProgramOptionsBase options) { try { var value = Enum.Parse(fieldInfo.FieldType, textValue, true); fieldInfo.SetValue(options, value); } catch { return false; } IsPresent = true; return true; }
public void SetValueToDefault(ProgramOptionsBase programOptions) { if (optionAttribute.GetType() != typeof(OptionWithParameterAttribute)) { throw new RequiredParameterMissingException(Name); } /* user has to set DefaultValue in correct type */ fieldInfo.SetValue(programOptions, ((OptionWithOptionableParameterAttribute)optionAttribute).DefaultValue); IsPresent = true; }
/// <summary> /// /// </summary> /// <param name="textValue"></param> /// <param name="options"></param> /// <returns></returns> public bool SetValue(string textValue, ProgramOptionsBase options) { Type fieldType = fieldInfo.FieldType; if (fieldType == typeof(string)) { fieldInfo.SetValue(options, textValue); } else if (fieldInfo.FieldType.IsEnum) { return SetEnumValue(textValue, options); } else { object value = null; try { value = TypeDescriptor.GetConverter(fieldType).ConvertFromString(textValue); } catch (NotSupportedException) { return false; } if (value is IComparable) { BoundsCheck((IComparable)value); } fieldInfo.SetValue(options, value); } IsPresent = true; return true; }