Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions CommandLineParser.Tests/Usage/UsagePrinterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,38 @@ namespace MatthiWare.CommandLine.Tests.Usage
{
public class UsagePrinterTests
{
#region Issue_60

private class Options_Issue60
{
[Name("c", "check")]
[Description("Reports the amount of duplicates without changing anything")]
[DefaultValue(true)] // Note defaults to true and not required
public bool OnlyCheck { get; set; }
}

[Theory]
// https://github.com/MatthiWare/CommandLineParser.Core/issues/60
[InlineData(new string[] { }, false)]
[InlineData(new string[] { "-c" }, false)]
[InlineData(new string[] { "-c", "true" }, false)]
[InlineData(new string[] { "-c", "false" }, false)]
public void AllOptionsHaveDefaultValueShouldNotPrintUsages(string[] args, bool called)
{
var printerMock = new Mock<IUsagePrinter>();

var parser = new CommandLineParser<Options_Issue60>
{
Printer = printerMock.Object
};

parser.Parse(args);

printerMock.Verify(mock => mock.PrintUsage(), called ? Times.Once() : Times.Never());
}

#endregion

private class UsagePrinterGetsCalledOptions
{
[Name("o"), Required]
Expand Down
2 changes: 1 addition & 1 deletion CommandLineParser/CommandLineParser`TOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ private void AutoPrintUsageAndErrors(ParseResult<TOption> result, bool noArgsSup
{
if (!ParserOptions.AutoPrintUsageAndErrors) return;

if (noArgsSupplied)
if (noArgsSupplied && (Options.Any(opt => !opt.HasDefault) || Commands.Any(cmd => cmd.IsRequired)))
PrintHelp();
else if (result.HelpRequested)
Printer.PrintUsage(result.HelpRequestedFor);
Expand Down