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); }
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); }
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); }