Exemplo n.º 1
0
        private static void ApplyOption(ref RunContext ctx, IOptionToken token)
        {
            Option?opt = null;

            if (token is ShortOptionToken shortOpt)
            {
                opt = ctx.Command.Options.SingleOrDefault(o => o.ShortName == shortOpt.Content);
            }
            else if (token is LongOptionToken longOpt)
            {
                opt = ctx.Command.Options.SingleOrDefault(o => o.LongName == longOpt.Content);
            }

            if (opt == null)
            {
                throw new RunException($"Unknown option at position {token.Position}");
            }

            if (!TryTakeValue(ref ctx, opt.ValueType, out var value))
            {
                if (opt.ValueType == typeof(bool))
                {
                    value = true;
                }
                else
                {
                    throw new RunException($"Expected value at position {ctx.Tokens.PeekOrDefault()?.Position}");
                }
            }

            ValueSetter.SetValue(ctx.OptionsObj, opt.MemberExpression, value);
        }
Exemplo n.º 2
0
        private static object TakeArray <T>(RunContext ctx)
        {
            var list = new List <T>();

            while (ctx.Tokens.PeekOrDefault() is StringToken strToken)
            {
                ctx.Tokens.Dequeue();

                if (!ValueSetter.TryParseValue(strToken.Content, typeof(T), out var obj))
                {
                    throw new RunException($"Cannot parse value into {typeof(T).Name} at position {strToken.Position}");
                }

                list.Add((T)obj);
            }

            return(list.ToArray());
        }
Exemplo n.º 3
0
        private static bool TryTakeValue(ref RunContext ctx, Type valueType, [MaybeNullWhen(false)] out object value)
        {
            if (valueType.IsArray)
            {
                var del = (TakeArrayDelegate)typeof(YaclipApp).GetMethod(nameof(TakeArray), BindingFlags.NonPublic | BindingFlags.Static).MakeGenericMethod(valueType.GetElementType()).CreateDelegate(typeof(TakeArrayDelegate));
                value = del(ctx);
                return(true);
            }

            if (!(ctx.Tokens.PeekOrDefault() is StringToken strToken))
            {
                value = null;
                return(false);
            }

            ctx.Tokens.Dequeue();
            return(ValueSetter.TryParseValue(strToken.Content, valueType, out value));
        }
Exemplo n.º 4
0
        private static void ApplyArgument(ref RunContext ctx)
        {
            if (ctx.SetArgumentsCount == ctx.Command.Arguments.Length)
            {
                throw new RunException("Argument found when no more were necessary");
            }

            var arg = ctx.Command.Arguments[ctx.SetArgumentsCount];

            ctx.SetArgumentsCount++;

            if (!TryTakeValue(ref ctx, arg.Type, out var value))
            {
                throw new RunException($"Cannot parse value at position {ctx.Tokens.PeekOrDefault()?.Position}");
            }

            ValueSetter.SetValue(ctx.OptionsObj, arg.MemberExpression, value);
        }
Exemplo n.º 5
0
        private static void ApplyOption(ref RunContext ctx, IOptionToken token)
        {
            Option?opt = null;

            if (token is ShortOptionToken shortOpt)
            {
                opt = ctx.Command.Options.SingleOrDefault(o => o.ShortName == shortOpt.Content);
            }
            else if (token is LongOptionToken longOpt)
            {
                opt = ctx.Command.Options.SingleOrDefault(o => o.LongName == longOpt.Content);
            }

            if (opt == null)
            {
                throw new RunException($"Unknown option at position {token.Position}");
            }

            var  propertyType = opt.ValueType;
            bool isList       = false;

            if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(IList <>))
            {
                propertyType = propertyType.GetGenericArguments()[0];
                isList       = true;
            }

            if (!TryTakeValue(ref ctx, propertyType, out var value))
            {
                if (opt.ValueType == typeof(bool))
                {
                    value = true;
                }
                else
                {
                    throw new RunException($"Expected value at position {ctx.Tokens.PeekOrDefault()?.Position}");
                }
            }

            ValueSetter.SetValue(ctx.OptionsObj, opt.MemberExpression, value, isList);
        }