internal static OptionResult CreateImplicit( IOption option, CommandResult parent) { var result = new OptionResult(option, option.Token()); result.IsImplicit = true; if (option.Argument.HasDefaultValue) { var value = option.Argument.GetDefaultValue(); switch (value) { case string arg: result.TryTakeToken(new Token(arg, TokenType.Argument)); break; default: result.Result = ArgumentParseResult.Success(value); break; } } return(result); }
public void Custom_types_and_conversion_logic_can_be_specified() { var argument = new Argument <MyCustomType>(parsed => { var custom = new MyCustomType(); foreach (var a in parsed.Arguments) { custom.Add(a); } return(ArgumentParseResult.Success(custom)); }) { Arity = ArgumentArity.ZeroOrMore }; var parser = new Parser( new Command("custom", "", argument: argument)); var result = parser.Parse("custom one two three"); var customType = result.CommandResult.GetValueOrDefault <MyCustomType>(); customType .Values .Should() .BeEquivalentTo("one", "two", "three"); }
private bool parseArgument_Type(IDMCommandContext context, ArgumentContainer argOut, out ArgumentParseResult failedParse) { if (context.Arguments.First.ToLower() == "delete") // Type is to delete the value { argOut.mode = CommandMode.delete; if (argOut.BotVar.IsDefined) { failedParse = null; return(true); } else { failedParse = new ArgumentParseResult(Arguments[0], $"Couldn't locate a config variable named `{argOut.BotVarId}`!"); return(false); } } else { argOut.mode = CommandMode.set; } if (!Enum.TryParse(context.Arguments.First, true, out argOut.assignType)) { failedParse = new ArgumentParseResult(Arguments[2]); return(false); } if (argOut.assignType == BotVarType.Undefined || argOut.assignType == BotVarType.Deleted) { failedParse = new ArgumentParseResult(Arguments[2]); return(false); } failedParse = null; return(true); }
public bool Build(out EmbedBuilder embed, out string messageContent, out string error) { ArgumentParseResult parseResult = EmbedHelper.TryParseEmbedFromJSONObject(JSON, out embed, out messageContent); error = parseResult.Message; return parseResult.Success; }
public void Argument_defaults_arity_to_One_for_non_IEnumerable_types() { var argument = new Argument <int>(s => ArgumentParseResult.Success(1)); argument.Arity.Should().BeEquivalentTo(ArgumentArity.ExactlyOne); }
private bool parseArgument_Value(IDMCommandContext context, ArgumentContainer argOut, out ArgumentParseResult failedParse) { argOut.value = context.Arguments.First; switch (argOut.assignType) { case BotVarType.UInt64: if (ulong.TryParse(argOut.value, out ulong uint64Val)) { argOut.BotVar = new BotVar(argOut.BotVarId, uint64Val); } else { failedParse = new ArgumentParseResult(Arguments[3]); return(false); } break; case BotVarType.Int64: if (long.TryParse(argOut.value, out long int64Val)) { argOut.BotVar = new BotVar(argOut.BotVarId, int64Val); } else { failedParse = new ArgumentParseResult(Arguments[3]); return(false); } break; case BotVarType.Float64: if (double.TryParse(argOut.value, out double float64Val)) { argOut.BotVar = new BotVar(argOut.BotVarId, float64Val); } else { failedParse = new ArgumentParseResult(Arguments[3]); return(false); } break; case BotVarType.String: argOut.BotVar = new BotVar(argOut.BotVarId, argOut.value); break; case BotVarType.Bool: if (bool.TryParse(argOut.value, out bool boolVal)) { argOut.BotVar = new BotVar(argOut.BotVarId, boolVal); } else { failedParse = new ArgumentParseResult(Arguments[3]); return(false); } break; case BotVarType.Generic: string json_str = context.RemoveArgumentsFront(3); if (JSONContainer.TryParse(json_str, out JSONContainer json, out string error)) { argOut.BotVar = new BotVar(argOut.BotVarId, json); } else { failedParse = new ArgumentParseResult(Arguments[3], error); return(false); } break;