Exemplo n.º 1
0
        public void Parse_verbs_using_instance()
        {
            string invokedVerb = null;
            object invokedVerbInstance = null;

            var proof = new Random().Next(int.MaxValue);
            var options = new OptionsWithVerbs();
            options.CommitVerb.Should().NotBeNull();
            options.CommitVerb.CreationProof = proof;

            var parser = new CommandLine.Parser();
            var result = parser.ParseArguments(new string[] { "commit", "--amend" }, options,
                (verb, subOptions) =>
                {
                    invokedVerb = verb;
                    invokedVerbInstance = subOptions;
                });

            result.Should().BeTrue();

            invokedVerb.Should().Be("commit");
            invokedVerbInstance.Should().BeOfType<CommitSubOptions>();

            // Check if the instance is the one provider by us (not by the parser)
            options.CommitVerb.CreationProof.Should().Be(proof);
            options.CommitVerb.Amend.Should().BeTrue();
        }
Exemplo n.º 2
0
        public void Parse_verbs_create_instance()
        {
            string invokedVerb = null;
            object invokedVerbInstance = null;

            var options = new OptionsWithVerbs();
            options.AddVerb.Should().BeNull();

            var parser = new CommandLine.Parser();
            var result = parser.ParseArguments(new string[] {"add", "-p", "untracked.bin"} , options,
                (verb, subOptions) =>
                {
                    invokedVerb = verb;
                    invokedVerbInstance = subOptions;
                });

            result.Should().BeTrue();

            invokedVerb.Should().Be("add");
            invokedVerbInstance.Should().BeOfType<AddSubOptions>();

            // Parser has built instance for us
            options.AddVerb.Should().NotBeNull();
            options.AddVerb.CreationProof.Should().NotHaveValue();
            options.AddVerb.Patch.Should().BeTrue();
            options.AddVerb.FileName[0].Should().Be("untracked.bin");
        }
Exemplo n.º 3
0
        public void Failed_verb_parsing_prints_particular_help_screen()
        {
            string invokedVerb = null;
            object invokedVerbInstance = null;

            var options = new OptionsWithVerbs();
            var testWriter = new StringWriter();

            var parser = new CommandLine.Parser(with => with.HelpWriter = testWriter);
            var result = parser.ParseArguments(new string[] {"clone", "--no_hardlinks"}, options,
                (verb, subOptions) =>
                {
                    invokedVerb = verb;
                    invokedVerbInstance = subOptions;
                });

            result.Should().BeFalse();

            invokedVerb.Should().Be("clone");
            invokedVerbInstance.Should().BeNull();

            var helpText = testWriter.ToString();
            helpText.Should().Be("help for: clone");
        }
Exemplo n.º 4
0
        public void Failed_parsing_prints_help_index()
        {
            string invokedVerb = null;
            object invokedVerbInstance = null;

            var options = new OptionsWithVerbs();
            var testWriter = new StringWriter();

            var parser = new CommandLine.Parser(with => with.HelpWriter = testWriter);
            var result = parser.ParseArguments(new string[] {}, options,
                (verb, subOptions) =>
                {
                    invokedVerb = verb;
                    invokedVerbInstance = subOptions;
                });

            result.Should().BeFalse();

            invokedVerb.Should().BeEmpty();
            invokedVerbInstance.Should().BeNull();

            var helpText = testWriter.ToString();
            helpText.Should().Be("verbs help index");
        }
Exemplo n.º 5
0
        public void Parse_strict_bad_input_fails_and_exits_with_verbs_when_get_usage_is_defined()
        {
            string invokedVerb = null;
            object invokedVerbInstance = null;

            var options = new OptionsWithVerbs();
            var testWriter = new StringWriter();

            ReflectionHelper.AssemblyFromWhichToPullInformation = Assembly.GetExecutingAssembly();
            var parser = new CommandLine.Parser(with => with.HelpWriter = testWriter);
            var result = parser.ParseArgumentsStrict(new string[] { "bad", "input" }, options,
                (verb, subOptions) =>
                {
                    invokedVerb = verb;
                    invokedVerbInstance = subOptions;
                },
                () => Console.WriteLine("fake fail"));

            result.Should().BeFalse();

            var helpText = testWriter.ToString();
            Console.WriteLine(helpText);
            var lines = helpText.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
            // Did we really produced all help?
            lines.Should().HaveCount(n => n == 1);
            // Verify just significant output
            lines[0].Trim().Should().Be("verbs help index");

            invokedVerb.Should().Be("bad");
            invokedVerbInstance.Should().BeNull();
        }