示例#1
0
        public static CanCauseError <TValue> TryGetValue <TKey, TValue>(this IDictionary <TKey, TValue> dict, TKey key)
        {
            TValue value;

            return(dict.TryGetValue(key, out value)
                ? value
                : CanCauseError <TValue> .Error("No value with key {0} found.", key));
        }
示例#2
0
 public static CanCauseError <T> Where <T>(this CanCauseError <T> error, Func <T, bool> predicate)
 {
     if (error.CausedError || predicate(error.Result))
     {
         return(error);
     }
     else
     {
         return(CanCauseError <T> .Error("Value failed predicate"));
     }
 }
示例#3
0
 public static CanCauseError ExceptionToError(this Action f)
 {
     try
     {
         f();
         return(CanCauseError.NoError);
     }
     catch (Exception e)
     {
         return(CanCauseError.Error(e.Message));
     }
 }
示例#4
0
 public static CanCauseError <T> ExceptionToError <T>(this Func <T> f)
 {
     try
     {
         var result = f();
         return(CanCauseError <T> .NoError(result));
     }
     catch (Exception e)
     {
         return(CanCauseError <T> .Error(e.Message));
     }
 }
示例#5
0
 public static CanCauseError <C, Error> SelectMany <A, B, C, Error>(
     this CanCauseError <A, Error> a, Func <A, CanCauseError <B, Error> > func, Func <A, B, C> select)
 {
     if (a.CausedError)
     {
         return(CanCauseError <C, Error> .Error(a.ErrorState));
     }
     else
     {
         var b = func(a.Result);
         return(b.CausedError ? CanCauseError <C, Error> .Error(b.ErrorState) :
                select(a.Result, b.Result));
     }
 }
        public CanCauseError HandleArgs(IEnumerable <string> args)
        {
            int parameterNumber = 0;

            foreach (var arg in args)
            {
                if (arg[0] == '-')
                {
                    var split    = arg.Split(new[] { ':' }, 2);
                    var flagName = split[0].TrimStart('-');
                    var param    = split.Length == 2 ? split[1] : null;

                    Action <string> flagAction;
                    if (flags.TryGetValue(flagName, out flagAction))
                    {
                        flagAction(param);
                    }
                    else
                    {
                        return(CanCauseError.Error("No flag named {0} exists.", flagName));
                    }
                }
                else
                {
                    if (parameterNumber < parameters.Length)
                    {
                        parameters[parameterNumber](arg);
                    }
                    else
                    {
                        return(CanCauseError.Error("Too many parameters."));
                    }
                }
            }
            return(CanCauseError.NoError);
        }