private void UpdateResult(MemberInfo info, OptionResult result) { object value; // Multiple value types are handled differently than the rest if (result.Defintion.Type == OptValType.MultValue) { IList list; if (info is FieldInfo) { list = (IList)((FieldInfo)info).GetValue(_value); } else { list = (IList)((PropertyInfo)info).GetValue(_value, null); } // will need to be improved later, but this will ensure only the // options are stored in the list list.Clear(); foreach (string resultValue in result.Values) { list.Add(resultValue); } return; } switch (result.Defintion.Type) { case OptValType.Flag: value = result.IsDefined; break; case OptValType.IncrementalFlag: value = result.NumDefinitions; break; case OptValType.ValueOpt: case OptValType.ValueReq: default: value = result.Value; break; } if (info is FieldInfo) { ((FieldInfo)info).SetValue(_value, value); } else { ((PropertyInfo)info).SetValue(_value, value, null); } }
/// <summary> /// Parse an option /// </summary> /// <param name="args">All the arguments</param> /// <param name="optName">The char or string that the option was identified with</param> /// <param name="optDef">The option found</param> /// <param name="value">The value immediately following the option ([=:]value syntax)</param> /// <param name="index">The current index in the arguments which can be incremented /// by this function</param> /// <param name="dupOptHandleType">How to handle un-expected duplicate option /// definitions</param> /// <param name="unknownOptHandleType">How to handle options that were not /// defined</param> /// <param name="foundDefinitions">The option definitions already found</param> /// <param name="wasUnknown">If the option was unknown and the handle type was not /// error, this will be true so that the "option" can be added to the arguments</param> private void ParseOption(string[] args, object optName, OptionDefinition optDef, string value, ref int index, DupOptHandleType dupOptHandleType, UnknownOptHandleType unknownOptHandleType, ArrayList foundDefinitions, out bool wasUnknown) { OptionResult result; // make sure the option is found if (optDef == null) { switch (unknownOptHandleType) { case UnknownOptHandleType.Error: throw new ParseException( string.Format("Option {0} is not recoginzed", optName)); case UnknownOptHandleType.Warning: if (OptionWarning != null) { OptionWarning(this, new OptionWarningEventArgs( string.Format("Option {0} is not recoginzed", optName))); } break; } wasUnknown = true; return; } else { wasUnknown = false; } // make sure value is not given went it should not be given CheckIfValueless(optDef, value); result = _resultsHandler[optDef]; // make sure value is not duplicate when it should not be CheckIfDup(optDef, foundDefinitions, dupOptHandleType); foundDefinitions.Add(optDef); switch (optDef.Type) { case OptValType.Flag: if (result == null) { result = new OptionResult(optDef); } result.NumDefinitions = 1; result.Value = true; _resultsHandler[optDef] = result; break; case OptValType.IncrementalFlag: if (result == null) { result = new OptionResult(optDef); } result.NumDefinitions++; result.Value = result.NumDefinitions; _resultsHandler[optDef] = result; break; case OptValType.MultValue: value = GetValue(args, value, ref index); if (value == null) { throw new ParseException( string.Format("Option {0} requires a value", optDef.ID)); } if (result == null) { result = new OptionResult(optDef); } result.NumDefinitions++; result.AddValue(optDef.ConvertValue(value)); _resultsHandler[optDef] = result; break; case OptValType.ValueOpt: value = GetValue(args, value, ref index); if (result == null) { result = new OptionResult(optDef); } result.NumDefinitions = 1; result.Value = optDef.ConvertValue(value); _resultsHandler[optDef] = result; break; case OptValType.ValueReq: value = GetValue(args, value, ref index); if (value == null) { throw new ParseException( string.Format("Option {0} requires a value", optDef.ID)); } if (result == null) { result = new OptionResult(optDef); } result.NumDefinitions = 1; result.Value = optDef.ConvertValue(value); _resultsHandler[optDef] = result; break; } }
private void ParseEnvironment(ArrayList foundDefinitions, bool caseSensitive) { OptionDefinition opt; OptionResult result; foreach (string key in Environment.GetEnvironmentVariables().Keys) { string value; if (key.Length == 1) { opt = _resultsHandler.GetOptionDefinition(key[0], caseSensitive); } else { opt = _resultsHandler.GetOptionDefinition(key, caseSensitive); } if (opt != null) { // don't not overwrite the command-line values if (foundDefinitions.Contains(opt)) { continue; } value = Environment.GetEnvironmentVariable(key); result = _resultsHandler[opt]; if (result == null) { result = new OptionResult(opt); } switch (opt.Type) { case OptValType.Flag: if (value.ToLower() != bool.TrueString.ToLower()) { continue; } result.NumDefinitions = 1; _resultsHandler[opt] = result; break; case OptValType.IncrementalFlag: if (value.ToLower() != bool.TrueString.ToLower()) { continue; } result.NumDefinitions++; result.Value = result.NumDefinitions; _resultsHandler[opt] = result; break; case OptValType.MultValue: if (value == null) { continue; } try { result.AddValue(opt.ConvertValue(value)); } catch { continue; } result.NumDefinitions++; _resultsHandler[opt] = result; break; case OptValType.ValueOpt: if (value == null) { continue; } try { result.Value = opt.ConvertValue(value); } catch { continue; } result.NumDefinitions = 1; _resultsHandler[opt] = result; break; case OptValType.ValueReq: if (value == null) { continue; } try { result.Value = opt.ConvertValue(value); } catch { continue; } result.NumDefinitions = 1; _resultsHandler[opt] = result; break; } } } }
private void UpdateResult(MemberInfo info, OptionResult result) { object value; // Multiple value types are handled differently than the rest if (result.Defintion.Type == OptValType.MultValue) { IList list; if (info is FieldInfo) list = (IList)((FieldInfo)info).GetValue(_value); else list = (IList)((PropertyInfo)info).GetValue(_value, null); // will need to be improved later, but this will ensure only the // options are stored in the list list.Clear(); foreach (string resultValue in result.Values) list.Add(resultValue); return; } switch (result.Defintion.Type) { case OptValType.Flag: value = result.IsDefined; break; case OptValType.IncrementalFlag: value = result.NumDefinitions; break; case OptValType.ValueOpt: case OptValType.ValueReq: default: value = result.Value; break; } if (info is FieldInfo) ((FieldInfo)info).SetValue(_value, value); else ((PropertyInfo)info).SetValue(_value, value, null); }
private OptionResult GetResult(OptionDefinition def, MemberInfo info) { OptionResult result = new OptionResult(def); object val; if (info is FieldInfo) val = ((FieldInfo)info).GetValue(_value); else val = ((PropertyInfo)info).GetValue(_value, null); switch (result.Defintion.Type) { case OptValType.Flag: bool isSet; if (val is bool) isSet = (bool)val; else isSet = Convert.ToBoolean(val); result.NumDefinitions = 1; result.Value = isSet; break; case OptValType.IncrementalFlag: int numSet; numSet = Convert.ToInt32(val); result.NumDefinitions = numSet; result.Value = numSet; break; case OptValType.MultValue: foreach (object value in (ICollection)val) { result.NumDefinitions++; result.AddValue(value); } break; case OptValType.ValueOpt: case OptValType.ValueReq: if (val == null) { result.NumDefinitions = 0; result.Value = null; } else { result.NumDefinitions = 1; result.Value = val; } break; } return result; }
/// <summary> /// Parse an option /// </summary> /// <param name="args">All the arguments</param> /// <param name="optName">The char or string that the option was identified with</param> /// <param name="optDef">The option found</param> /// <param name="value">The value immediately following the option ([=:]value syntax)</param> /// <param name="index">The current index in the arguments which can be incremented /// by this function</param> /// <param name="dupOptHandleType">How to handle un-expected duplicate option /// definitions</param> /// <param name="unknownOptHandleType">How to handle options that were not /// defined</param> /// <param name="foundDefinitions">The option definitions already found</param> /// <param name="wasUnknown">If the option was unknown and the handle type was not /// error, this will be true so that the "option" can be added to the arguments</param> private void ParseOption(string[] args, object optName, OptionDefinition optDef, string value, ref int index, DupOptHandleType dupOptHandleType, UnknownOptHandleType unknownOptHandleType, ArrayList foundDefinitions, out bool wasUnknown) { OptionResult result; // make sure the option is found if (optDef == null) { switch (unknownOptHandleType) { case UnknownOptHandleType.Error: throw new ParseException( string.Format("Option {0} is not recoginzed", optName)); case UnknownOptHandleType.Warning: if (OptionWarning != null) OptionWarning(this, new OptionWarningEventArgs( string.Format("Option {0} is not recoginzed", optName))); break; } wasUnknown = true; return; } else wasUnknown = false; // make sure value is not given went it should not be given CheckIfValueless(optDef, value); result = _resultsHandler[optDef]; // make sure value is not duplicate when it should not be CheckIfDup(optDef, foundDefinitions, dupOptHandleType); foundDefinitions.Add(optDef); switch (optDef.Type) { case OptValType.Flag: if (result == null) result = new OptionResult(optDef); result.NumDefinitions = 1; result.Value = true; _resultsHandler[optDef] = result; break; case OptValType.IncrementalFlag: if (result == null) result = new OptionResult(optDef); result.NumDefinitions++; result.Value = result.NumDefinitions; _resultsHandler[optDef] = result; break; case OptValType.MultValue: value = GetValue(args, value, ref index); if (value == null) throw new ParseException( string.Format("Option {0} requires a value", optDef.ID)); if (result == null) result = new OptionResult(optDef); result.NumDefinitions++; result.AddValue(optDef.ConvertValue(value)); _resultsHandler[optDef] = result; break; case OptValType.ValueOpt: value = GetValue(args, value, ref index); if (result == null) result = new OptionResult(optDef); result.NumDefinitions = 1; result.Value = optDef.ConvertValue(value); _resultsHandler[optDef] = result; break; case OptValType.ValueReq: value = GetValue(args, value, ref index); if (value == null) throw new ParseException( string.Format("Option {0} requires a value", optDef.ID)); if (result == null) result = new OptionResult(optDef); result.NumDefinitions = 1; result.Value = optDef.ConvertValue(value); _resultsHandler[optDef] = result; break; } }
private void ParseEnvironment(ArrayList foundDefinitions, bool caseSensitive) { OptionDefinition opt; OptionResult result; foreach (string key in Environment.GetEnvironmentVariables().Keys) { string value; if (key.Length == 1) opt = _resultsHandler.GetOptionDefinition(key[0], caseSensitive); else opt = _resultsHandler.GetOptionDefinition(key, caseSensitive); if (opt != null) { // don't not overwrite the command-line values if (foundDefinitions.Contains(opt)) continue; value = Environment.GetEnvironmentVariable(key); result = _resultsHandler[opt]; if (result == null) result = new OptionResult(opt); switch (opt.Type) { case OptValType.Flag: if (value.ToLower() != bool.TrueString.ToLower()) continue; result.NumDefinitions = 1; _resultsHandler[opt] = result; break; case OptValType.IncrementalFlag: if (value.ToLower() != bool.TrueString.ToLower()) continue; result.NumDefinitions++; result.Value = result.NumDefinitions; _resultsHandler[opt] = result; break; case OptValType.MultValue: if (value == null) continue; try { result.AddValue(opt.ConvertValue(value)); } catch { continue; } result.NumDefinitions++; _resultsHandler[opt] = result; break; case OptValType.ValueOpt: if (value == null) continue; try { result.Value = opt.ConvertValue(value); } catch { continue; } result.NumDefinitions = 1; _resultsHandler[opt] = result; break; case OptValType.ValueReq: if (value == null) continue; try { result.Value = opt.ConvertValue(value); } catch { continue; } result.NumDefinitions = 1; _resultsHandler[opt] = result; break; } } } }
/// <summary> /// Add a result /// </summary> /// <param name="key">Option definition</param> /// <param name="value">Result value</param> public void Add(OptionDefinition key, OptionResult value) { base.Dictionary.Add(key, value); }
private OptionResult GetResult(OptionDefinition def, MemberInfo info) { OptionResult result = new OptionResult(def); object val; if (info is FieldInfo) { val = ((FieldInfo)info).GetValue(_value); } else { val = ((PropertyInfo)info).GetValue(_value, null); } switch (result.Defintion.Type) { case OptValType.Flag: bool isSet; if (val is bool) { isSet = (bool)val; } else { isSet = Convert.ToBoolean(val); } result.NumDefinitions = 1; result.Value = isSet; break; case OptValType.IncrementalFlag: int numSet; numSet = Convert.ToInt32(val); result.NumDefinitions = numSet; result.Value = numSet; break; case OptValType.MultValue: foreach (object value in (ICollection)val) { result.NumDefinitions++; result.AddValue(value); } break; case OptValType.ValueOpt: case OptValType.ValueReq: if (val == null) { result.NumDefinitions = 0; result.Value = null; } else { result.NumDefinitions = 1; result.Value = val; } break; } return(result); }