4using System.Collections.Generic;
20 var writer =
new StringWriter();
21 var sut =
new Parser(with => with.HelpWriter = writer);
27 var text = writer.ToString();
28 Assert.True(text.Length > 0);
36 var writer =
new StringWriter();
37 var sut =
new Parser(with => with.HelpWriter = writer);
43 var text = writer.ToString();
44 text.Should().NotBeEmpty();
52 var writer =
new StringWriter();
53 var sut =
new Parser(with => with.HelpWriter = writer);
59 var text = writer.ToString();
60 text.Should().NotBeEmpty();
68 var expectedOptions =
new Simple_Options { StringValue =
"strvalue", IntSequence =
new[] { 1, 2, 3 } };
72 var result = sut.ParseArguments<
Simple_Options>(
new[] {
"--stringvalue=strvalue",
"-i1",
"2",
"3" });
80 [InlineData(
"file",
new[] {
"-o",
"file" })]
81 [InlineData(
"file",
new[] {
"-ofile" })]
82 [InlineData(
"hile",
new[] {
"-o",
"hile" })]
83 [InlineData(
"hile",
new[] {
"-ohile" })]
99 [InlineData(
new string[0], 0, 0)]
100 [InlineData(
new[] {
"-v" }, 1, 0)]
101 [InlineData(
new[] {
"-vv" }, 2, 0)]
102 [InlineData(
new[] {
"-v",
"-v" }, 2, 0)]
103 [InlineData(
new[] {
"-v",
"-v",
"-v" }, 3, 0)]
104 [InlineData(
new[] {
"-v",
"-vv" }, 3, 0)]
105 [InlineData(
new[] {
"-vv",
"-v" }, 3, 0)]
106 [InlineData(
new[] {
"-vvv" }, 3, 0)]
107 [InlineData(
new[] {
"-v",
"-s",
"-v",
"-v" }, 3, 1)]
108 [InlineData(
new[] {
"-v",
"-ss",
"-v",
"-v" }, 3, 2)]
109 [InlineData(
new[] {
"-v",
"-s",
"-sv",
"-v" }, 3, 2)]
110 [InlineData(
new[] {
"-vsvv" }, 3, 1)]
111 [InlineData(
new[] {
"-vssvv" }, 3, 2)]
112 [InlineData(
new[] {
"-vsvsv" }, 3, 2)]
117 var sut =
new Parser(with => with.AllowMultiInstance =
true);
149 StringValue =
"astring",
151 StringSequence =
new[] {
"--aaa",
"-b",
"--ccc" },
154 var sut =
new Parser(with => with.EnableDashDash =
true);
159 new[] {
"--stringvalue",
"astring",
"--",
"20",
"--aaa",
"-b",
"--ccc",
"30" });
169 var text =
"x1 x2 x3 -c x1";
170 var args = text.Split();
171 var parser =
new Parser(with =>
173 with.HelpWriter = Console.Out;
177 options.Compress.Should().BeEquivalentTo(
new[] {
"x1" });
178 options.InputDirs.Should().BeEquivalentTo(
new[] {
"x1",
"x2",
"x3" });
186 OptionSequence =
new[] {
"option1",
"option2",
"option3" },
187 ValueSequence =
new[] {
"value1",
"value2",
"value3" }
190 var sut =
new Parser(with => with.EnableDashDash =
true);
195 new[] {
"--option-seq",
"option1",
"option2",
"option3",
"--",
"value1",
"value2",
"value3" });
205 var expectedOptions =
new Add_Verb { Patch =
true, FileName =
"--strange-fn" };
206 var sut =
new Parser(with => with.EnableDashDash =
true);
209 var result = sut.ParseArguments(
210 new[] {
"add",
"-p",
"--",
"--strange-fn" },
217 ((
Parsed<object>)result).Value.Should().BeEquivalentTo(expectedOptions, o => o.RespectingRuntimeTypes());
225 var args =
new[] {
"-"};
247 "http://gsscoder.github.com/",
248 "http://yes-to-nooo.github.com/"
256 new[] {
"clone",
"-q",
"http://gsscoder.github.com/",
"http://yes-to-nooo.github.com/" },
263 ((
Parsed<object>)result).Value.Should().BeEquivalentTo(expectedOptions, o => o.RespectingRuntimeTypes());
268 [InlineData(
"blabla",
new[] {
"commit",
"-m",
"blabla" })]
269 [InlineData(
"blabla",
new[] {
"commit",
"-mblabla" })]
270 [InlineData(
"plapla",
new[] {
"commit",
"-m",
"plapla" })]
271 [InlineData(
"plapla",
new[] {
"commit",
"-mplapla" })]
275 var expectedOptions =
new Commit_Verb() { Message = message };
279 var result = sut.ParseArguments(
285 ((
Parsed<object>)result).Value.Should().BeEquivalentTo(expectedOptions, o => o.RespectingRuntimeTypes());
297 new[] {
"clone",
"-q",
"-q",
"http://gsscoder.github.com/",
"http://yes-to-nooo.github.com/" },
316 "http://gsscoder.github.com/",
317 "http://yes-to-nooo.github.com/"
325 new[] {
"clone",
"-q",
"http://gsscoder.github.com/",
"http://yes-to-nooo.github.com/" });
329 ((
Parsed<object>)result).Value.Should().BeEquivalentTo(expectedOptions, o => o.RespectingRuntimeTypes());
368 var help =
new StringWriter();
369 var sut =
new Parser(config => config.HelpWriter = help);
373 var result = help.ToString();
376 result.Length.Should().BeGreaterThan(0);
388 var result = sut.ParseArguments<
Simple_Options>(
new[] {
"--version" });
400 var help =
new StringWriter();
401 var sut =
new Parser(config => config.HelpWriter = help);
405 var result = help.ToString();
408 result.Length.Should().BeGreaterThan(0);
409 var lines = result.ToNotEmptyLines().TrimStringArray();
410 lines.Should().HaveCount(x => x == 1);
419 var help =
new StringWriter();
420 var sut =
new Parser(config => config.HelpWriter = help);
424 var result = help.ToString();
427 result.Length.Should().BeGreaterThan(0);
428 var lines = result.ToNotEmptyLines().TrimStringArray();
431 lines[2].Should().BeEquivalentTo(
"ERROR(S):");
432 lines[3].Should().BeEquivalentTo(
"No verb selected.");
433 lines[4].Should().BeEquivalentTo(
"add Add file contents to the index.");
434 lines[5].Should().BeEquivalentTo(
"commit Record changes to the repository.");
435 lines[6].Should().BeEquivalentTo(
"clone Clone a repository into a new directory.");
436 lines[7].Should().BeEquivalentTo(
"help Display more information on a specific command.");
437 lines[8].Should().BeEquivalentTo(
"version Display version information.");
445 var help =
new StringWriter();
446 var sut =
new Parser(config => config.HelpWriter = help);
450 var result = help.ToString();
453 result.Length.Should().BeGreaterThan(0);
454 var lines = result.ToNotEmptyLines().TrimStringArray();
457 lines[2].Should().BeEquivalentTo(
"add (Default Verb) Add file contents to the index.");
458 lines[3].Should().BeEquivalentTo(
"commit Record changes to the repository.");
459 lines[4].Should().BeEquivalentTo(
"clone Clone a repository into a new directory.");
460 lines[5].Should().BeEquivalentTo(
"help Display more information on a specific command.");
461 lines[6].Should().BeEquivalentTo(
"version Display version information.");
468 var help =
new StringWriter();
469 var sut =
new Parser(config => config.HelpWriter = help);
473 var result = help.ToString();
476 var lines = result.ToNotEmptyLines().TrimStringArray();
479 lines[2].Should().BeEquivalentTo(
"add Add file contents to the index.");
480 lines[3].Should().BeEquivalentTo(
"commit Record changes to the repository.");
481 lines[4].Should().BeEquivalentTo(
"clone Clone a repository into a new directory.");
482 lines[5].Should().BeEquivalentTo(
"help Display more information on a specific command.");
483 lines[6].Should().BeEquivalentTo(
"version Display version information.");
488 [InlineData(
"--version")]
489 [InlineData(
"version")]
493 var help =
new StringWriter();
494 var sut =
new Parser(config => config.HelpWriter = help);
498 var result = help.ToString();
501 result.Length.Should().BeGreaterThan(0);
502 var lines = result.ToNotEmptyLines().TrimStringArray();
503 lines.Should().HaveCount(x => x == 1);
512 var help =
new StringWriter();
513 var sut =
new Parser(config => config.HelpWriter = help);
517 var result = help.ToString();
520 var lines = result.ToNotEmptyLines().TrimStringArray();
523 lines[2].Should().BeEquivalentTo(
"ERROR(S):");
524 lines[3].Should().BeEquivalentTo(
"Option: 'weburl' is not compatible with: 'ftpurl'.");
525 lines[4].Should().BeEquivalentTo(
"Option: 'ftpurl' is not compatible with: 'weburl'.");
526 lines[5].Should().BeEquivalentTo(
"--weburl Required.");
527 lines[6].Should().BeEquivalentTo(
"--ftpurl Required.");
528 lines[7].Should().BeEquivalentTo(
"-a");
529 lines[8].Should().BeEquivalentTo(
"--help Display this help screen.");
530 lines[9].Should().BeEquivalentTo(
"--version Display version information.");
538 var help =
new StringWriter();
539 var sut =
new Parser(config => config.HelpWriter = help);
543 var result = help.ToString();
546 result.Length.Should().BeGreaterThan(0);
554 var help =
new StringWriter();
555 var sut =
new Parser(config =>
557 config.HelpWriter = help;
558 config.MaximumDisplayWidth = 80;
563 new[] {
"clone",
"--badoption=@bad?value" });
564 var result = help.ToString();
567 var lines = result.ToNotEmptyLines().TrimStringArray();
570 lines[2].Should().BeEquivalentTo(
"ERROR(S):");
571 lines[3].Should().BeEquivalentTo(
"Option 'badoption' is unknown.");
572 lines[4].Should().BeEquivalentTo(
"USAGE:");
573 lines[5].Should().BeEquivalentTo(
"Basic cloning:");
574 lines[6].Should().BeEquivalentTo(
"git clone https://github.com/gsscoder/csharpx");
575 lines[7].Should().BeEquivalentTo(
"Cloning quietly:");
576 lines[8].Should().BeEquivalentTo(
"git clone --quiet https://github.com/gsscoder/railwaysharp");
577 lines[9].Should().BeEquivalentTo(
"Cloning without hard links:");
578 lines[10].Should().BeEquivalentTo(
"git clone --no-hardlinks https://github.com/gsscoder/csharpx");
579 lines[11].Should().BeEquivalentTo(
"--no-hardlinks Optimize the cloning process from a repository on a local");
580 lines[12].Should().BeEquivalentTo(
"filesystem by copying files.");
581 lines[13].Should().BeEquivalentTo(
"-q, --quiet Suppress summary message.");
582 lines[14].Should().BeEquivalentTo(
"--help Display this help screen.");
583 lines[15].Should().BeEquivalentTo(
"--version Display version information.");
584 lines[16].Should().BeEquivalentTo(
"URLS (pos. 0) A list of url(s) to clone.");
593 var help =
new StringWriter();
594 var sut =
new Parser(config => config.HelpWriter = help);
598 var result = help.ToString();
601 var lines = result.ToNotEmptyLines().TrimStringArray();
604 lines[2].Should().BeEquivalentTo(
"ERROR(S):");
605 lines[3].Should().BeEquivalentTo(
"No verb selected.");
606 lines[4].Should().BeEquivalentTo(
"add Add file contents to the index.");
607 lines[5].Should().BeEquivalentTo(
"help Display more information on a specific command.");
608 lines[6].Should().BeEquivalentTo(
"version Display version information.");
617 var help =
new StringWriter();
618 var sut =
new Parser(config => config.HelpWriter = help);
622 var result = help.ToString();
625 var lines = result.ToNotEmptyLines().TrimStringArray();
628 lines[2].Should().BeEquivalentTo(
"-f, --force Allow adding otherwise ignored files.");
629 lines[3].Should().BeEquivalentTo(
"--help Display this help screen.");
630 lines[4].Should().BeEquivalentTo(
"--version Display version information.");
639 var expectedOptions =
new Secert_Verb { Force =
true, SecertOption =
null};
640 var help =
new StringWriter();
641 var sut =
new Parser(config => config.HelpWriter = help);
651 ((
Parsed<object>)result).Value.Should().BeEquivalentTo(expectedOptions, o => o.RespectingRuntimeTypes());
659 var expectedOptions =
new Secert_Verb { Force =
true, SecertOption =
"shhh" };
660 var help =
new StringWriter();
661 var sut =
new Parser(config => config.HelpWriter = help);
670 ((
Parsed<object>)result).Value.Should().BeEquivalentTo(expectedOptions, o => o.RespectingRuntimeTypes());
678 var help =
new StringWriter();
679 var sut =
new Parser(config =>
681 config.HelpWriter = help;
682 config.MaximumDisplayWidth = 80;
687 new[] {
"help",
"clone",
"extra-arg" });
688 var result = help.ToString();
691 var lines = result.ToNotEmptyLines().TrimStringArray();
694 lines[2].Should().BeEquivalentTo(
"--no-hardlinks Optimize the cloning process from a repository on a local");
695 lines[3].Should().BeEquivalentTo(
"filesystem by copying files.");
696 lines[4].Should().BeEquivalentTo(
"-q, --quiet Suppress summary message.");
697 lines[5].Should().BeEquivalentTo(
"--help Display this help screen.");
698 lines[6].Should().BeEquivalentTo(
"--version Display version information.");
699 lines[7].Should().BeEquivalentTo(
"value pos. 0");
711 var sut =
new Parser(config => config.IgnoreUnknownArguments =
true);
718 result.WithParsed(opts => opts.Should().BeEquivalentTo(expected));
730 var sut =
new Parser(config => config.IgnoreUnknownArguments =
true);
737 result.WithParsed(opts => opts.Should().BeEquivalentTo(expected));
746 var help =
new StringWriter();
747 var sut =
new Parser(config =>
749 config.HelpWriter = help;
750 config.MaximumDisplayWidth = 80;
755 new[] {
"clone",
"--bad-arg",
"--help" });
756 var result = help.ToString();
759 var lines = result.ToNotEmptyLines().TrimStringArray();
762 lines[2].Should().BeEquivalentTo(
"ERROR(S):");
763 lines[3].Should().BeEquivalentTo(
"Option 'bad-arg' is unknown.");
764 lines[4].Should().BeEquivalentTo(
"--no-hardlinks Optimize the cloning process from a repository on a local");
765 lines[5].Should().BeEquivalentTo(
"filesystem by copying files.");
766 lines[6].Should().BeEquivalentTo(
"-q, --quiet Suppress summary message.");
767 lines[7].Should().BeEquivalentTo(
"--help Display this help screen.");
768 lines[8].Should().BeEquivalentTo(
"--version Display version information.");
769 lines[9].Should().BeEquivalentTo(
"value pos. 0");
778 var expectedResult =
new[]
788 new[] {
"--weburl",
"value",
"--somethingelse",
"othervalue" });
798 var sameValues =
new[] {
"--stringvalue=test",
"--shortandlong=test" };
799 var sut =
new Parser(parserSettings => { parserSettings.IgnoreUnknownArguments =
true; });
802 result.MapResult(_ =>
true, _ =>
false).Should().BeTrue();
808 var sameValues =
new[] {
"--stringvalue=test1",
"--shortandlong=test2" };
809 var sut =
new Parser(parserSettings => { parserSettings.IgnoreUnknownArguments =
true; });
812 result.MapResult(_ =>
true, _ =>
false).Should().BeTrue();
819 yield
return new object[] {
new[] {
"--stringvalue=strdata0",
"--unknown=valid" },
new Simple_Options { StringValue =
"strdata0", IntSequence = Enumerable.Empty<
int>() } };
820 yield
return new object[] {
new[] {
"--stringvalue=strdata0",
"1234",
"--unknown",
"-i",
"1",
"2",
"3" },
new Simple_Options { StringValue =
"strdata0", LongValue = 1234L, IntSequence =
new[] { 1, 2, 3 } } };
821 yield
return new object[] {
new[] {
"--stringvalue=strdata0",
"-u" },
new Simple_Options { StringValue =
"strdata0", IntSequence = Enumerable.Empty<
int>() } };
829 yield
return new object[] {
new[] {
"commit",
"-up" },
new Commit_Verb { Patch =
true } };
830 yield
return new object[] {
new[] {
"commit",
"--amend",
"--unknown",
"valid" },
new Commit_Verb { Amend =
true } };
847 [Option(
'u',
"user", Default =
null)]
848 public string User {
get;
set; }
856 new[] {
"arg",
"-o",
"arg" })
857 .WithNotParsed(errors => {
throw new InvalidOperationException(
"Must be parsed."); })
860 Assert.Equal(
"arg", args.OptValue);
861 Assert.Equal(
"arg", args.PosValue);
870 new[] {
"test",
"arg",
"-o",
"arg" },
873 .WithNotParsed(errors => {
throw new InvalidOperationException(
"Must be parsed."); })
874 .WithParsed<Verb_With_Option_And_Value_Of_String_Type>(args =>
876 Assert.Equal(
"arg", args.OptValue);
877 Assert.Equal(
"arg", args.PosValue);
886 new[] {
"zero",
"one",
"two" })
887 .WithNotParsed(errors => {
throw new InvalidOperationException(
"Must be parsed."); })
890 Assert.Equal(
"zero", args.Arg0);
891 Assert.Equal(
"one", args.Arg1);
892 Assert.Equal(
"two", args.Arg2);
902 var help =
new StringWriter();
903 var sut =
new Parser(config => config.HelpWriter = help);
907 var result = help.ToString();
910 var lines = result.ToLines().TrimStringArray();
911 lines[6].Should().BeEquivalentTo(
"add Add file contents to the index.");
912 lines[8].Should().BeEquivalentTo(
"help Display more information on a specific command.");
913 lines[10].Should().BeEquivalentTo(
"version Display version information.");
923 .WithNotParsed(errors =>
throw new InvalidOperationException(
"Must be parsed."))
926 Assert.True(args.TestValueOne);
935 .WithNotParsed(errors =>
throw new InvalidOperationException(
"Must be parsed."))
938 Assert.True(args.TestValueOne);
948 .WithParsed(args =>
throw new InvalidOperationException(
"Should not be parsed."));
954 using (var sut =
new Parser(settings => settings.AllowMultiInstance =
true))
959 var stringVal =
"shortSeq1";
961 var result = sut.ParseArguments(
962 new[] {
"sequence",
"--long-seq", $
"{longVal1}",
"-s", stringVal,
"--long-seq", $
"{longVal2};{longVal3}" },
969 Assert.Equal(
new long[] { longVal1, longVal2, longVal3 }, verb.
LongSequence);
979 using (var sut =
new Parser(settings => settings.AllowMultiInstance =
false))
984 var stringVal =
"shortSeq1";
986 var result = sut.ParseArguments(
987 new[] {
"sequence",
"--long-seq", $
"{longVal1}",
"-s", stringVal,
"--long-seq", $
"{longVal2};{longVal3}" },
991 result.WithNotParsed(errors => Assert.All(errors, e =>
999 throw new Exception($
"{nameof(RepeatedOptionError)} expected");
1010 .WithNotParsed(errors =>
throw new InvalidOperationException(
"Must be parsed."))
1013 Assert.True(args.TestValue);
1023 var sut =
new Parser(config => config.HelpWriter =
null);
1026 sut.Settings.MaximumDisplayWidth.Should().BeGreaterThan(1);
Models an error generated when a user explicitly requests help.
Models an error generated when multiple default verbs are defined.
Models an error generated when a an option from another set is defined.
Models name information, used in CommandLine.Error instances.
It contains a sequence of CommandLine.Error.
It contains an instance of type T with parsed values.
Provides methods to parse command line arguments.
ParserResult< object > ParseArguments(IEnumerable< string > args, params Type[] types)
Parses a string array of command line arguments for verb commands scenario, constructing the proper i...
static Parser Default
Gets the singleton instance created with basic defaults.
Models an error generated when an option is repeated two or more times.
IEnumerable< long > LongSequence
IEnumerable< string > StringSequence
void Errors_of_type_MutuallyExclusiveSetError_are_properly_formatted()
void Parse_repeated_options_in_verbs_scenario_without_multi_instance()
void Parse_options_when_given_hidden_verb_with_hidden_option()
void Explicit_help_request_with_immutable_instance_generates_help_screen()
static IEnumerable< object[]> IgnoreUnknownArgumentsData
void Properly_formatted_help_screen_is_displayed_when_usage_is_defined_in_verb_scenario()
void Specific_verb_help_screen_should_be_displayed_regardless_other_argument()
void Parse_multiple_default_verbs()
void Explicit_help_request_with_immutable_instance_generates_help_requested_error()
void Explicit_version_request_generates_version_info_screen_in_verbs_scenario(string command)
void Properly_formatted_help_screen_is_displayed_when_there_is_a_hidden_verb_selected_usage_displays_with_hidden_option()
void Implicit_help_screen_in_verb_scenario()
void When_IgnoreUnknownArguments_is_set_valid_unknown_arguments_avoid_a_failure_parsing(string[] arguments, Simple_Options expected)
static void Arguments_with_the_same_values_when_unknown_arguments_are_ignored()
void When_HelpWriter_is_set_help_screen_is_generated_in_verbs_scenario_using_generic_overload()
void Parse_repeated_options_in_verbs_scenario_with_multi_instance()
void Explicit_help_request_with_specific_verb_generates_help_screen()
void Parse_options_with_short_name(string outputFile, string[] args)
void Parse_verbs_using_generic_overload()
void Parse_options_with_double_dash_in_verbs_scenario()
void Explicit_version_request_generates_version_info_screen()
void Parse_options_with_double_dash()
void Properly_formatted_help_screen_excludes_help_as_unknown_option()
void Parse_options_with_same_option_and_value_args()
void Parse_options_with_repeated_value_in_values_sequence_and_option()
static void Breaking_mutually_exclusive_set_constraint_with_both_set_name_with_gererates_Error()
void When_HelpWriter_is_set_help_screen_is_generated_in_verbs_scenario()
void When_HelpWriter_is_set_help_screen_is_generated()
void Parse_default_verb_implicit()
void Parse_verb_with_same_option_and_value_args()
static void Arguments_with_the_different_values_when_unknown_arguments_are_ignored()
void Parse_options_with_short_name_in_verbs_scenario(string message, string[] args)
void Parse_repeated_options_with_default_parser()
static IEnumerable< object[]> IgnoreUnknownArgumentsForVerbsData
void Parse_FlagCounter_options_with_short_name(string[] args, int verboseCount, int silentCount)
void Parse_default_verb_explicit()
void Parse_options_with_shuffled_index_values()
void Parse_default_verb_with_empty_name()
static void Null_default()
void Parse_to_immutable_instance()
void When_HelpWriter_is_null_it_should_not_fire_exception()
void Blank_lines_are_inserted_between_verbs()
void Help_screen_in_default_verb_scenario()
void When_IgnoreUnknownArguments_is_set_valid_unknown_arguments_avoid_a_failure_parsing_for_verbs(string[] arguments, Commit_Verb expected)
void Explicit_version_request_generates_version_requested_error()
void Properly_formatted_help_screen_is_displayed_when_there_is_a_hidden_verb()
void Parse_options_with_double_dash_and_option_sequence()
void Double_dash_help_dispalys_verbs_index_in_verbs_scenario()
void Parse_repeated_options_with_default_parser_in_verbs_scenario()
void Parse_options_when_given_hidden_verb()
void Parse_options_with_single_dash()
Models the copyright part of an help text. You can assign it where you assign any System....
override string ToString()
Returns the copyright as a System.String.
static CopyrightInfo Default
Gets the default copyright information. Retrieved from AssemblyCopyrightAttribute,...
Models the heading part of an help text. You can assign it where you assign any System....
static HeadingInfo Default
Gets the default heading instance. The title is retrieved from AssemblyTitleAttribute,...
override string ToString()
Returns the heading as a System.String.
Models an error generated when a user explicitly requests version.
ParserResultType
Discriminator enumeration of CommandLine.ParserResultType derivates.