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"); }
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(); }
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"); }
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"); }
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(); }
public void Help_WithHelpOfVerbCalled_GeneratedHelpOutputForVerb() { const string expected = @"Usage: clipr verb1 [ -n|--name N ] [ -h|--help ] Optional Arguments: -h, --help Display this help document. -n, --name "; var sw = new StringWriter(); var opt = new OptionsWithVerbs(); var parser = new CliParser <OptionsWithVerbs>(opt, new ParserOptions { OutputWriter = sw }); try { parser.Parse("verb1 --help".Split()); } catch (ParserExit e) { } var actual = sw.ToString(); Assert.AreEqual(expected, actual); }
public void Help_WithVerbs_AddsVerbDescriptionsToHelpOutput() { const string expected = @"Usage: clipr [ -h|--help ] [ --version ] <command> Optional Arguments: -h, --help Display this help document. --version Displays the version of the current executable. Commands: verb1 A verb for names verb2 A verb for ages"; var opt = new OptionsWithVerbs(); var parser = new CliParser <OptionsWithVerbs>(opt); var gen = new AutomaticHelpGenerator <OptionsWithVerbs>(); var help = gen.GetHelp(parser.Config); Assert.AreEqual(expected, help); }