/// <summary> /// Parses a string array of command line arguments for verb commands scenario, constructing the proper instance from the array of types supplied by <paramref name="types"/>. /// Grammar rules are defined decorating public properties with appropriate attributes. /// The <see cref="CommandLine.VerbAttribute"/> must be applied to types in the array. /// </summary> /// <param name="args">A <see cref="System.String"/> array of command line arguments, normally supplied by application entry point.</param> /// <param name="types">A <see cref="System.Type"/> array used to supply verb alternatives.</param> /// <returns>A <see cref="CommandLine.ParserResult{T}"/> containing the appropriate instance with parsed values as a <see cref="System.Object"/> /// and a sequence of <see cref="CommandLine.Error"/>.</returns> /// <exception cref="System.ArgumentNullException">Thrown if one or more arguments are null.</exception> /// <exception cref="System.ArgumentOutOfRangeException">Thrown if <paramref name="types"/> array is empty.</exception> /// <remarks>All types must expose a parameterless constructor. It's stronly recommended to use a generic overload.</remarks> public ParserResult <object> ParseArguments(IEnumerable <string> args, params Type[] types) { if (args == null) { throw new ArgumentNullException("args"); } if (types == null) { throw new ArgumentNullException("types"); } if (types.Length == 0) { throw new ArgumentOutOfRangeException("types"); } return(MakeParserResult( () => InstanceChooser.Choose( (arguments, optionSpecs) => Tokenize(arguments, optionSpecs, settings), types, args, settings.NameComparer, settings.ParsingCulture, HandleUnknownArguments(settings.IgnoreUnknownArguments)), settings)); }
//construction and initialization public static ILoad Create(string path) { InstanceChooser myChooser = new InstanceChooser(); LoaderBase myLoader = myChooser.Choose(path); if (myLoader != null) { myLoader.Load(path); //virtual method call moved out of constructor. } return(myLoader); }
private static ParserResult <object> InvokeChoose( IEnumerable <Type> types, IEnumerable <string> arguments) { return(InstanceChooser.Choose( (args, optionSpecs) => Tokenizer.ConfigureTokenizer(StringComparer.Ordinal, false, false)(args, optionSpecs), types, arguments, StringComparer.Ordinal, CultureInfo.InvariantCulture, Enumerable.Empty <ErrorType>())); }
public void Explicit_help_request_for_an_invalid_verb_generates_HelpVerbRequestedError_with_Matched_set_to_false() { // Fixture setup var expectedErrors = new[] { new HelpVerbRequestedError(null, null, false) }; // Exercize system var result = InstanceChooser.Choose( new[] { typeof(AddOptions), typeof(CommitOptions), typeof(CloneOptions) }, new[] { "help", "earthunderalienattack" }, StringComparer.Ordinal, CultureInfo.InvariantCulture); // Verify outcome //Assert.IsType<NullInstance>(result.Value); Assert.True(expectedErrors.SequenceEqual(((NotParsed <object>)result).Errors)); // Teardown }
public void Parse_empty_array_returns_NullInstance() { // Fixture setup var expectedErrors = new[] { new NoVerbSelectedError() }; // Exercize system var result = InstanceChooser.Choose( new[] { typeof(AddOptions), typeof(CommitOptions), typeof(CloneOptions) }, new string[] { }, StringComparer.Ordinal, CultureInfo.InvariantCulture); // Verify outcome //Assert.IsType<NullInstance>(result.Value); Assert.True(expectedErrors.SequenceEqual(((NotParsed <object>)result).Errors)); // Teardown }
public void Parse_existing_verb_returns_verb_immutable_instance() { // Fixture setup var expected = new ImmutableAddOptions(true, default(bool), "dummy.bin"); // Exercize system var result = InstanceChooser.Choose( new[] { typeof(ImmutableAddOptions), typeof(ImmutableCommitOptions), typeof(ImmutableCloneOptions) }, new[] { "add", "--patch", "dummy.bin" }, StringComparer.Ordinal, CultureInfo.InvariantCulture); // Verify outcome Assert.IsType <ImmutableAddOptions>(((Parsed <object>)result).Value); expected.ShouldBeEquivalentTo(((Parsed <object>)result).Value); // Teardown }
public void Explicit_help_request_for_a_valid_verb_generates_HelpVerbRequestedError_with_appropriate_data() { // Fixture setup var expectedErrors = new[] { new HelpVerbRequestedError("commit", typeof(CommitOptions), true) }; // Exercize system var result = InstanceChooser.Choose( new[] { typeof(AddOptions), typeof(CommitOptions), typeof(CloneOptions) }, new[] { "help", "commit" }, StringComparer.Ordinal, CultureInfo.InvariantCulture); // Verify outcome Assert.IsType <NullInstance>(result.Value); Assert.True(expectedErrors.SequenceEqual(((NotParsed <object>)result).Errors)); // Teardown }
public void Explicit_help_request_generates_HelpVerbRequestedError() { // Fixture setup var expectedErrors = new[] { new HelpVerbRequestedError(null, null, false) }; // Exercize system var result = InstanceChooser.Choose( new[] { typeof(AddOptions), typeof(CommitOptions), typeof(CloneOptions) }, new[] { "help" }, StringComparer.Ordinal, CultureInfo.InvariantCulture); // Verify outcome Assert.IsType <NullInstance>(result.Value); Assert.True(expectedErrors.SequenceEqual(result.Errors)); // Teardown }
public void Explicit_help_request_for_an_invalid_verb_generates_HelpVerbRequestedError_with_Matched_set_to_false() { // Fixture setup var expectedErrors = new[] { new HelpVerbRequestedError(null, null, false) }; // Exercize system var result = InstanceChooser.Choose( new[] { typeof(Add_Verb), typeof(Commit_Verb), typeof(Clone_Verb) }, new[] { "help", "earthunderalienattack" }, StringComparer.Ordinal, CultureInfo.InvariantCulture, Enumerable.Empty <ErrorType>()); // Verify outcome ((NotParsed <object>)result).Errors.ShouldBeEquivalentTo(expectedErrors); // Teardown }
public void Explicit_help_request_for_a_valid_verb_generates_HelpVerbRequestedError_with_appropriate_data() { // Fixture setup var expectedErrors = new[] { new HelpVerbRequestedError("commit", typeof(Commit_Verb), true) }; // Exercize system var result = InstanceChooser.Choose( new[] { typeof(Add_Verb), typeof(Commit_Verb), typeof(Clone_Verb) }, new[] { "help", "commit" }, StringComparer.Ordinal, CultureInfo.InvariantCulture, Enumerable.Empty <ErrorType>()); // Verify outcome ((NotParsed <object>)result).Errors.ShouldBeEquivalentTo(expectedErrors); // Teardown }
public void Parse_empty_array_returns_NullInstance() { // Fixture setup var expectedErrors = new[] { new NoVerbSelectedError() }; // Exercize system var result = InstanceChooser.Choose( new[] { typeof(Add_Verb), typeof(Commit_Verb), typeof(Clone_Verb) }, new string[] { }, StringComparer.Ordinal, CultureInfo.InvariantCulture, Enumerable.Empty <ErrorType>()); // Verify outcome ((NotParsed <object>)result).Errors.ShouldBeEquivalentTo(expectedErrors); // Teardown }
public void Parse_sequence_verb_with_separator_returns_verb_instance(string[] arguments, string[] expectedString) { // Fixture setup var expected = new SequenceOptions { LongSequence = new long[] { }, StringSequence = expectedString }; // Exercize system var result = InstanceChooser.Choose( new[] { typeof(AddOptions), typeof(CommitOptions), typeof(CloneOptions), typeof(SequenceOptions) }, arguments, StringComparer.Ordinal, CultureInfo.InvariantCulture); // Verify outcome Assert.IsType <SequenceOptions>(((Parsed <object>)result).Value); expected.ShouldBeEquivalentTo(((Parsed <object>)result).Value); // Teardown }
public void Parse_existing_verb_returns_verb_instance() { // Fixture setup var expected = new AddOptions { Patch = true, FileName = "dummy.bin" }; // Exercize system var result = InstanceChooser.Choose( new[] { typeof(AddOptions), typeof(CommitOptions), typeof(CloneOptions) }, new[] { "add", "--patch", "dummy.bin" }, StringComparer.Ordinal, CultureInfo.InvariantCulture); // Verify outcome Assert.IsType <AddOptions>(result.Value); expected.ShouldBeEquivalentTo(result.Value); // Teardown }
public void Parse_sequence_verb_returns_verb_instance() { // Fixture setup var expected = new SequenceOptions { LongSequence = new long[] { }, StringSequence = new[] { "aa", "b" } }; // Exercize system var result = InstanceChooser.Choose( new[] { typeof(AddOptions), typeof(CommitOptions), typeof(CloneOptions), typeof(SequenceOptions) }, new[] { "sequence", "-s", "aa", "b" }, StringComparer.Ordinal, CultureInfo.InvariantCulture); // Verify outcome Assert.IsType <SequenceOptions>(result.Value); expected.ShouldBeEquivalentTo(result.Value); // Teardown }
public void Parse_existing_verb_returns_verb_instance() { // Fixture setup var expected = new Add_Verb { Patch = true, FileName = "dummy.bin" }; // Exercize system var result = InstanceChooser.Choose( new[] { typeof(Add_Verb), typeof(Commit_Verb), typeof(Clone_Verb) }, new[] { "add", "--patch", "dummy.bin" }, StringComparer.Ordinal, CultureInfo.InvariantCulture, Enumerable.Empty <ErrorType>()); // Verify outcome Assert.IsType <Add_Verb>(((Parsed <object>)result).Value); expected.ShouldBeEquivalentTo(((Parsed <object>)result).Value); // Teardown }