public CmdLineArgDefinitionBuilder Arg(string name) { if (string.IsNullOrWhiteSpace(name)) throw new ArgumentException(); if (_args.Any(x => x.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase))) throw new InvalidOperationException(); if (_args.Any(x => x.IsOptional || x.IsParams)) throw new InvalidOperationException(); var definition = new CmdLineArgDefinition(name); _args.Add(definition); return new CmdLineArgDefinitionBuilder(definition); }
public void ShouldThrowOnFailingArgValue() { var definition = new CmdLineArgDefinition("arg") { Validator = values => values.Single() == "pass" ? null : "fail" }; Should.Throw<ParserException>(() => definition.Validate(new[] { "foo" })) .Message.Split(new[] { Environment.NewLine }, StringSplitOptions.None) .ShouldBe(new[] { "Invalid arg 'arg':", "fail" }); }
private static void ParseArg(string s, CmdLineArgDefinition argDefinition, ParserContext context, Queue<string> queue) { var strings = argDefinition.IsParams ? new[] { s }.Concat(TakeWhileNotPrefixed(queue, context.Prefixes)).ToArray() : new[] { s }; var validationError = argDefinition.Validator(strings); if (!string.IsNullOrEmpty(validationError)) { var message = new StringBuilder() .AppendFormat("Invalid arg {0}:", argDefinition.DisplayName ?? argDefinition.Name) .AppendLine() .AppendLine(validationError) .ToString(); throw new ParserException(message); } strings.Each(x => context.Input.Args.Add(argDefinition.Name, x)); }