public bool TryGetValueForOption(IValueDescriptor valueDescriptor, out object value) { var children = Children .Where(o => valueDescriptor.Name.IsMatch(o.Symbol)) .ToArray(); SymbolResult symbolResult = null; if (children.Length > 1) { throw new ArgumentException($"Ambiguous match while trying to bind parameter {valueDescriptor.Name} among: {string.Join(",", children.Select(o => o.Name))}"); } if (children.Length == 1) { symbolResult = children[0]; } if (symbolResult is OptionResult && symbolResult.GetValueAs(valueDescriptor.Type) is SuccessfulArgumentResult successful) { value = successful.Value; return(true); } else { value = null; return(false); } }
public static T GetValueOrDefault <T>(this SymbolResult symbolResult) { ArgumentResult result = symbolResult.GetValueAs(typeof(T)); switch (result) { case SuccessfulArgumentResult successful: return((T)successful.Value); case FailedArgumentResult failed: throw new InvalidOperationException(failed.ErrorMessage); case NoArgumentResult _: default: return(default); } }