Exemple #1
0
        MapValues(
            IEnumerable <SpecificationProperty> propertyTuples,
            IEnumerable <KeyValuePair <string, IEnumerable <string> > > options,
            Func <IEnumerable <string>, System.Type, bool, Maybe <object> > converter,
            StringComparer comparer)
        {
            var sequencesAndErrors = propertyTuples
                                     .Select(pt =>
                                             options.SingleOrDefault(
                                                 s =>
                                                 s.Key.MatchName(((OptionSpecification)pt.Specification).ShortName, ((OptionSpecification)pt.Specification).LongName, comparer))
                                             .ToMaybe()
                                             .Return(sequence =>
                                                     converter(sequence.Value, pt.Property.PropertyType, pt.Specification.ConversionType.IsScalar())
                                                     .Return(converted =>
                                                             Tuple.Create(
                                                                 pt.WithValue(Maybe.Just(converted)),
                                                                 Maybe.Nothing <Error>()),
                                                             Tuple.Create <SpecificationProperty, Maybe <Error> >(
                                                                 pt,
                                                                 Maybe.Just <Error>(new BadFormatConversionError(NameInfo.FromOptionSpecification((OptionSpecification)pt.Specification))))),
                                                     Tuple.Create(pt, Maybe.Nothing <Error>()))
                                             );

            return(StatePair.Create(
                       sequencesAndErrors.Select(se => se.Item1),
                       sequencesAndErrors.Select(se => se.Item2).OfType <Just <Error> >().Select(se => se.Value)));
        }
Exemple #2
0
        public bool Equals(StatePair <T> other)
        {
            if (other == null)
            {
                return(false);
            }

            return(this.Value.Equals(other.Value) && this.Errors.SequenceEqual(other.Errors));
        }
Exemple #3
0
        public static StatePair <IEnumerable <SpecificationProperty> > MapValues(
            IEnumerable <SpecificationProperty> specProps,
            IEnumerable <string> values,
            Func <IEnumerable <string>, System.Type, bool, Maybe <object> > converter)
        {
            var propAndErrors = MapValuesImpl(specProps, values, converter);

            return(StatePair.Create(
                       propAndErrors.Select(pe => pe.Item1),
                       propAndErrors.Select(pe => pe.Item2).OfType <Just <Error> >().Select(e => e.Value)
                       ));
        }
        public static StatePair <IEnumerable <Token> > Tokenize(
            IEnumerable <string> arguments,
            Func <string, bool> nameLookup)
        {
            if (arguments == null)
            {
                throw new ArgumentNullException("arguments");
            }

            var            errors  = new List <Error>();
            Action <Error> onError = e => errors.Add(e);

            var tokens = from arg in arguments
                         from token in !arg.StartsWith("-", StringComparison.Ordinal)
                               ? new Token[] { Token.Value(arg) }
                               : arg.StartsWith("--", StringComparison.Ordinal)
                                     ? TokenizeLongName(arg, onError)
                                     : TokenizeShortName(arg, nameLookup)
            select token;

            var unkTokens = from t in tokens where t.IsName() && !nameLookup(t.Text) select t;

            return(StatePair.Create(tokens.Except(unkTokens), errors.Concat(from t in unkTokens select new UnknownOptionError(t.Text))));
        }