internal ParserResult(T value, TypeInfo typeInfo) { Value = value ?? throw new ArgumentNullException(nameof(value)); this.tag = ParserResultType.Parsed; this.typeInfo = typeInfo ?? TypeInfo.Create(value.GetType()); Errors = new Error[0]; }
internal ParserResult(ParserResultType tag, T value, IEnumerable <Error> errors, Maybe <IEnumerable <Type> > verbTypes) { this.tag = tag; this.value = value; this.errors = errors; this.verbTypes = verbTypes; }
internal ParserResult(IEnumerable <Error> errors, TypeInfo typeInfo) { this.tag = ParserResultType.NotParsed; this.typeInfo = typeInfo ?? TypeInfo.Create(typeof(T)); Errors = errors ?? new Error[0]; Value = default; }
internal ParserResult(ParserResultType tag, T value, IEnumerable <Error> errors, Maybe <IEnumerable <Type> > verbTypes) { if (object.Equals(value, default(T))) { throw new ArgumentNullException("value"); } if (errors == null) { throw new ArgumentNullException("errors"); } this.tag = tag; this.value = value; this.errors = errors; this.verbTypes = verbTypes; }
public static CommandOptions ParseCommandLine(string[] args) { try { CommandOptions thisOptions = new CommandOptions(); var writer = new StringWriter(); var parser = new Parser(with => with.HelpWriter = writer); var options = parser.ParseArguments <CommandOptions>(args); ParserResultType tag = options.Tag; if (tag == ParserResultType.NotParsed) { thisOptions.ParserMessage = writer.ToString(); return(thisOptions); } options.WithParsed(opt => thisOptions = opt); // An arcane way to set thisOptions because the CommandOptions library if going for functional style if (!IodClientMethods.TaskTypes.Keys.Contains(thisOptions.TaskType)) { var help = CommandLine.Text.HelpText.AutoBuild(parser.ParseArguments <CommandOptions>(new[] { "--help" })); thisOptions.ParserMessage = $"Invalid task type -e {thisOptions.TaskType}\n{help}"; return(thisOptions); } if ((thisOptions.UserName == null || thisOptions.Password == null) && (thisOptions.UserName != null || thisOptions.Password != null)) { var help = CommandLine.Text.HelpText.AutoBuild(parser.ParseArguments <CommandOptions>(new[] { "--help" })); thisOptions.ParserMessage = $"UserName and Password must both be specified or neither specified\n{help}"; return(thisOptions); } return(thisOptions); } catch (Exception ex) { throw new ApplicationException("Error parsing command line arguments: ", ex); } }
public void Enforce_required_within_mutually_exclusive_set_only(string[] arguments, ParserResultType type, int expected) { // Exercize system var result = InstanceBuilder.Build( Maybe.Just<Func<FakeOptionsWithTwoRequiredAndSets>>(() => new FakeOptionsWithTwoRequiredAndSets()), arguments, StringComparer.Ordinal, CultureInfo.InvariantCulture); // Verify outcome if (type == ParserResultType.NotParsed) { ((NotParsed<FakeOptionsWithTwoRequiredAndSets>)result).Errors.Should().HaveCount(x => x == expected); } else if (type == ParserResultType.Parsed) { result.Should().BeOfType<Parsed<FakeOptionsWithTwoRequiredAndSets>>(); } }
public void Empty_set_options_allowed_with_mutually_exclusive_sets(string[] arguments, ParserResultType type, int expected) { // Exercize system var result = InstanceBuilder.Build( Maybe.Just<Func<Options_With_Named_And_Empty_Sets>>(() => new Options_With_Named_And_Empty_Sets()), arguments, StringComparer.Ordinal, CultureInfo.InvariantCulture, Enumerable.Empty<ErrorType>()); // Verify outcome if (type == ParserResultType.NotParsed) { ((NotParsed<Options_With_Named_And_Empty_Sets>)result).Errors.Should().HaveCount(x => x == expected); } else if (type == ParserResultType.Parsed) { result.Should().BeOfType<Parsed<Options_With_Named_And_Empty_Sets>>(); } }
public void Enforce_required_within_mutually_exclusive_set_only(string[] arguments, ParserResultType type, int expected) { // Exercize system var result = InvokeBuild <Options_With_Two_Option_Required_Set_To_True_And_Two_Sets>( arguments); // Verify outcome if (type == ParserResultType.NotParsed) { ((NotParsed <Options_With_Two_Option_Required_Set_To_True_And_Two_Sets>)result).Errors.Should().HaveCount(x => x == expected); } else if (type == ParserResultType.Parsed) { result.Should().BeOfType <Parsed <Options_With_Two_Option_Required_Set_To_True_And_Two_Sets> >(); } }
public void Empty_set_options_allowed_with_mutually_exclusive_sets(string[] arguments, ParserResultType type, int expected) { // Exercize system var result = InvokeBuild <Options_With_Named_And_Empty_Sets>( arguments); // Verify outcome if (type == ParserResultType.NotParsed) { ((NotParsed <Options_With_Named_And_Empty_Sets>)result).Errors.Should().HaveCount(x => x == expected); } else if (type == ParserResultType.Parsed) { result.Should().BeOfType <Parsed <Options_With_Named_And_Empty_Sets> >(); } }
public void Enforce_required_within_mutually_exclusive_set_only(string[] arguments, ParserResultType type, int expected) { // Exercize system var result = InstanceBuilder.Build( Maybe.Just <Func <Options_With_Two_Option_Required_Set_To_True_And_Two_Sets> >(() => new Options_With_Two_Option_Required_Set_To_True_And_Two_Sets()), arguments, StringComparer.Ordinal, CultureInfo.InvariantCulture, Enumerable.Empty <ErrorType>()); // Verify outcome if (type == ParserResultType.NotParsed) { ((NotParsed <Options_With_Two_Option_Required_Set_To_True_And_Two_Sets>)result).Errors.Should().HaveCount(x => x == expected); } else if (type == ParserResultType.Parsed) { result.Should().BeOfType <Parsed <Options_With_Two_Option_Required_Set_To_True_And_Two_Sets> >(); } }
/// <summary> /// /// </summary> /// <param name="tag"></param> internal ParserResult(ParserResultType tag, T value) { Tag = tag; Value = value; }
internal ParserResult(ParserResultType tag, T value, IEnumerable <Type> verbTypes) { this.tag = tag; this.value = value; this.verbTypes = verbTypes; }
public void Empty_set_options_allowed_with_mutually_exclusive_sets(string[] arguments, ParserResultType type, int expected) { // Exercize system var result = InstanceBuilder.Build( Maybe.Just <Func <FakeOptionsWithNamedAndEmptySets> >(() => new FakeOptionsWithNamedAndEmptySets()), arguments, StringComparer.Ordinal, CultureInfo.InvariantCulture); // Verify outcome if (type == ParserResultType.NotParsed) { ((NotParsed <FakeOptionsWithNamedAndEmptySets>)result).Errors.Should().HaveCount(x => x == expected); } else if (type == ParserResultType.Parsed) { result.Should().BeOfType <Parsed <FakeOptionsWithNamedAndEmptySets> >(); } }
internal ParserResult(ParserResultType tag, TypeInfo typeInfo) { this.tag = tag; this.typeInfo = typeInfo; }