diff --git a/src/CommandLine/Parser.cs b/src/CommandLine/Parser.cs index 4301aa52..9944cf97 100644 --- a/src/CommandLine/Parser.cs +++ b/src/CommandLine/Parser.cs @@ -201,16 +201,18 @@ private static ParserResult MakeParserResult(ParserResult parserResult, { return DisplayHelp( parserResult, + settings.AutoVersion, + settings.AutoHelp, settings.HelpWriter, settings.MaximumDisplayWidth); } - private static ParserResult DisplayHelp(ParserResult parserResult, TextWriter helpWriter, int maxDisplayWidth) + private static ParserResult DisplayHelp(ParserResult parserResult, bool autoVersion, bool autoHelp, TextWriter helpWriter, int maxDisplayWidth) { parserResult.WithNotParsed( errors => Maybe.Merge(errors.ToMaybe(), helpWriter.ToMaybe()) - .Do((_, writer) => writer.Write(HelpText.AutoBuild(parserResult, maxDisplayWidth))) + .Do((_, writer) => writer.Write(HelpText.AutoBuild(parserResult, maxDisplayWidth, autoVersion, autoHelp))) ); return parserResult; diff --git a/src/CommandLine/Text/HelpText.cs b/src/CommandLine/Text/HelpText.cs index f5e9a7b9..3c798687 100644 --- a/src/CommandLine/Text/HelpText.cs +++ b/src/CommandLine/Text/HelpText.cs @@ -314,6 +314,8 @@ public SentenceBuilder SentenceBuilder /// The containing the instance that collected command line arguments parsed with class. /// A delegate used to customize the text block of reporting parsing errors text block. /// A delegate used to customize model used to render text block of usage examples. + /// A value to indicate whether shall be set or not. + /// A value to indicate whether shall be set or not. /// If true the output style is consistent with verb commands (no dashes), otherwise it outputs options. /// The maximum width of the display. /// The parameter is not ontly a metter of formatting, it controls whether to handle verbs or options. @@ -322,7 +324,9 @@ public static HelpText AutoBuild( Func onError, Func onExample, bool verbsIndex = false, - int maxDisplayWidth = DefaultMaximumLength) + int maxDisplayWidth = DefaultMaximumLength, + bool autoVersion = true, + bool autoHelp = true) { var auto = new HelpText { @@ -330,7 +334,9 @@ public static HelpText AutoBuild( Copyright = CopyrightInfo.Empty, AdditionalNewLineAfterOption = true, AddDashesToOption = !verbsIndex, - MaximumDisplayWidth = maxDisplayWidth + MaximumDisplayWidth = maxDisplayWidth, + AutoVersion = autoVersion, + AutoHelp = autoHelp, }; try @@ -394,14 +400,16 @@ public static HelpText AutoBuild( /// /// The containing the instance that collected command line arguments parsed with class. /// The maximum width of the display. + /// A value to indicate whether shall be set or not. + /// A value to indicate whether shall be set or not. /// /// An instance of class. /// /// This feature is meant to be invoked automatically by the parser, setting the HelpWriter property /// of . - public static HelpText AutoBuild(ParserResult parserResult, int maxDisplayWidth = DefaultMaximumLength) + public static HelpText AutoBuild(ParserResult parserResult, int maxDisplayWidth = DefaultMaximumLength, bool autoVersion = true, bool autoHelp = true) { - return AutoBuild(parserResult, h => h, maxDisplayWidth); + return AutoBuild(parserResult, h => h, maxDisplayWidth, autoVersion, autoHelp); } /// @@ -411,12 +419,14 @@ public static HelpText AutoBuild(ParserResult parserResult, int maxDisplay /// The containing the instance that collected command line arguments parsed with class. /// A delegate used to customize the text block of reporting parsing errors text block. /// The maximum width of the display. + /// A value to indicate whether shall be set or not. + /// A value to indicate whether shall be set or not. /// /// An instance of class. /// /// This feature is meant to be invoked automatically by the parser, setting the HelpWriter property /// of . - public static HelpText AutoBuild(ParserResult parserResult, Func onError, int maxDisplayWidth = DefaultMaximumLength) + public static HelpText AutoBuild(ParserResult parserResult, Func onError, int maxDisplayWidth = DefaultMaximumLength, bool autoVersion = true, bool autoHelp = true) { if (parserResult.Tag != ParserResultType.NotParsed) throw new ArgumentException("Excepting NotParsed type.", "parserResult"); @@ -431,7 +441,7 @@ public static HelpText AutoBuild(ParserResult parserResult, Func e, maxDisplayWidth: maxDisplayWidth); + }, e => e, maxDisplayWidth: maxDisplayWidth, autoVersion: autoVersion, autoHelp: autoHelp); var err = errors.OfType().Single(); var pr = new NotParsed(TypeInfo.Create(err.Type), new Error[] { err }); @@ -440,12 +450,12 @@ public static HelpText AutoBuild(ParserResult parserResult, Func e, maxDisplayWidth: maxDisplayWidth) + }, e => e, maxDisplayWidth: maxDisplayWidth, autoVersion: autoVersion, autoHelp: autoHelp) : AutoBuild(parserResult, current => { onError?.Invoke(current); return DefaultParsingErrorsHandler(parserResult, current); - }, e => e, true, maxDisplayWidth); + }, e => e, true, maxDisplayWidth, autoVersion: autoVersion, autoHelp: autoHelp); } /// diff --git a/tests/CommandLine.Tests/Unit/Issue821Tests.cs b/tests/CommandLine.Tests/Unit/Issue821Tests.cs new file mode 100644 index 00000000..e650188d --- /dev/null +++ b/tests/CommandLine.Tests/Unit/Issue821Tests.cs @@ -0,0 +1,123 @@ +using System; +using System.IO; +using System.Linq; +using System.Runtime.Serialization.Formatters; +using System.Text; +using CommandLine.Tests.Fakes; +using CommandLine.Text; +using FluentAssertions; +using Xunit; +using Xunit.Abstractions; + +//Issue #591 +//When options class is only having explicit interface declarations, it should be detected as mutable. + +namespace CommandLine.Tests.Unit +{ + public class Issue821Tests + { + [Verb("do")] + private class EmptyDoOptions{ + }; + + [Verb("nothing")] + private class EmptyNothingOptions{ + }; + + [Fact] + public void not_parsed_should_produce_version_if_set_to_true() + { + StringBuilder output = new StringBuilder(); + ParserResult result; + using (StringWriter writer = new StringWriter(output)) + { + void ConfigureParser(ParserSettings settings) + { + settings.HelpWriter = writer; + settings.AutoVersion = true; + } + + using (Parser parser = new Parser(ConfigureParser)) + { + result = parser.ParseArguments(Array.Empty()); + } + } + + result.Should().BeOfType>(); + string outputContent = output.ToString(); + outputContent.Should().Contain("version", "Version is set to true and must be contained"); + } + + [Fact] + public void not_parsed_should_not_produce_version_if_set_to_false() + { + StringBuilder output = new StringBuilder(); + ParserResult result; + using (StringWriter writer = new StringWriter(output)) + { + void ConfigureParser(ParserSettings settings) + { + settings.HelpWriter = writer; + settings.AutoVersion = false; + } + + using (Parser parser = new Parser(ConfigureParser)) + { + result = parser.ParseArguments(Array.Empty()); + } + } + + result.Should().BeOfType>(); + string outputContent = output.ToString(); + outputContent.Should().NotContain("version", "Version is set to false and must not be contained"); + } + + [Fact] + public void not_parsed_should_produce_help_if_set_to_true() + { + StringBuilder output = new StringBuilder(); + ParserResult result; + using (StringWriter writer = new StringWriter(output)) + { + void ConfigureParser(ParserSettings settings) + { + settings.HelpWriter = writer; + settings.AutoHelp = true; + } + + using (Parser parser = new Parser(ConfigureParser)) + { + result = parser.ParseArguments(Array.Empty()); + } + } + + result.Should().BeOfType>(); + string outputContent = output.ToString(); + outputContent.Should().Contain("help", "Help is set to true and must be contained"); + } + + [Fact] + public void not_parsed_should_not_produce_help_if_set_to_false() + { + StringBuilder output = new StringBuilder(); + ParserResult result; + using (StringWriter writer = new StringWriter(output)) + { + void ConfigureParser(ParserSettings settings) + { + settings.HelpWriter = writer; + settings.AutoHelp = false; + } + + using (Parser parser = new Parser(ConfigureParser)) + { + result = parser.ParseArguments(Array.Empty()); + } + } + + result.Should().BeOfType>(); + string outputContent = output.ToString(); + outputContent.Should().NotContain("help", "Help is set to false and must not be contained"); + } + } +}