示例#1
0
        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 isUnix = Environment.OSVersion.Platform == PlatformID.Unix ||
                         Environment.OSVersion.Platform == PlatformID.MacOSX;

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

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

            return(StatePair.Create(tokens.Where(x => !unkTokens.Contains(x)), errors.Concat(from t in unkTokens select new UnknownOptionError(t.Text))));
        }
示例#2
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)));
        }
示例#3
0
        public bool Equals(StatePair <T> other)
        {
            if (other == null)
            {
                return(false);
            }

            return(this.Value.Equals(other.Value) && this.Errors.SequenceEqual(other.Errors));
        }
示例#4
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)
                       ));
        }
示例#5
0
        public static StatePair <IEnumerable <Token> > ExplodeOptionList(
            StatePair <IEnumerable <Token> > tokens,
            Func <string, Maybe <char> > optionSequenceWithSeparatorLookup)
        {
            var replaces = tokens.Value.Select((t, i) =>
                                               optionSequenceWithSeparatorLookup(t.Text)
                                               .Return(sep => Tuple.Create(i + 1, sep),
                                                       Tuple.Create(-1, '\0'))).SkipWhile(x => x.Item1 < 0);

            var exploded = tokens.Value.Select((t, i) =>
                                               replaces.FirstOrDefault(x => x.Item1 == i).ToMaybe()
                                               .Return(r => t.Text.Split(r.Item2).Select(Token.Value),
                                                       Enumerable.Empty <Token>().Concat(new[] { t })));

            var flattened = exploded.SelectMany(x => x);

            return(StatePair.Create(flattened, tokens.Errors));
        }
示例#6
0
        public static StatePair <IEnumerable <Token> > Tokenize(
            IEnumerable <string> arguments,
            Func <string, bool> nameLookup)
        {
            var            errors  = new List <Error>();
            Action <Error> onError = errors.Add;

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

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

            return(StatePair.Create(tokens.Where(x => !unkTokens.Contains(x)), errors.Concat(from t in unkTokens select new UnknownOptionError(t.Text))));
        }
        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))));
        }