Skip to content

Commit ddb3a0f

Browse files
committed
Only display usages when no default value has been specified.
Fixes #60
1 parent a8cae91 commit ddb3a0f

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

CommandLineParser.Tests/Usage/UsagePrinterTests.cs

+32
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,38 @@ namespace MatthiWare.CommandLine.Tests.Usage
1212
{
1313
public class UsagePrinterTests
1414
{
15+
#region Issue_60
16+
17+
private class Options_Issue60
18+
{
19+
[Name("c", "check")]
20+
[Description("Reports the amount of duplicates without changing anything")]
21+
[DefaultValue(true)] // Note defaults to true and not required
22+
public bool OnlyCheck { get; set; }
23+
}
24+
25+
[Theory]
26+
// https://github.com/MatthiWare/CommandLineParser.Core/issues/60
27+
[InlineData(new string[] { }, false)]
28+
[InlineData(new string[] { "-c" }, false)]
29+
[InlineData(new string[] { "-c", "true" }, false)]
30+
[InlineData(new string[] { "-c", "false" }, false)]
31+
public void AllOptionsHaveDefaultValueShouldNotPrintUsages(string[] args, bool called)
32+
{
33+
var printerMock = new Mock<IUsagePrinter>();
34+
35+
var parser = new CommandLineParser<Options_Issue60>
36+
{
37+
Printer = printerMock.Object
38+
};
39+
40+
parser.Parse(args);
41+
42+
printerMock.Verify(mock => mock.PrintUsage(), called ? Times.Once() : Times.Never());
43+
}
44+
45+
#endregion
46+
1547
private class UsagePrinterGetsCalledOptions
1648
{
1749
[Name("o"), Required]

CommandLineParser/CommandLineParser`TOption.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ private void AutoPrintUsageAndErrors(ParseResult<TOption> result, bool noArgsSup
306306
{
307307
if (!ParserOptions.AutoPrintUsageAndErrors) return;
308308

309-
if (noArgsSupplied)
309+
if (noArgsSupplied && (Options.Any(opt => !opt.HasDefault) || Commands.Any(cmd => cmd.IsRequired)))
310310
PrintHelp();
311311
else if (result.HelpRequested)
312312
Printer.PrintUsage(result.HelpRequestedFor);

0 commit comments

Comments
 (0)