Skip to content

Commit c079d2b

Browse files
committed
exclude --help and --version options from binding
1 parent 9097545 commit c079d2b

File tree

4 files changed

+9
-8
lines changed

4 files changed

+9
-8
lines changed

src/System.CommandLine/Builder/CommandLineBuilderExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,8 @@ public static CommandLineBuilder UseVersionOption(
515515
return true;
516516
});
517517

518+
versionOption.DisallowBinding = true;
519+
518520
command.AddOption(versionOption);
519521

520522
builder.AddMiddleware(async (context, next) =>

src/System.CommandLine/Help/HelpOption.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ public HelpOption() : base(new[]
1212
"--help",
1313
"-?",
1414
"/?"
15-
}, Resources.Instance.HelpOptionDescription() )
15+
}, Resources.Instance.HelpOptionDescription())
1616
{
17+
DisallowBinding = true;
1718
}
1819

1920
internal override Argument Argument
@@ -22,11 +23,6 @@ internal override Argument Argument
2223
set { }
2324
}
2425

25-
protected bool Equals(HelpOption other)
26-
{
27-
return other != null;
28-
}
29-
3026
public override bool Equals(object obj)
3127
{
3228
return obj is HelpOption;

src/System.CommandLine/Option.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ public bool ArgumentIsHidden
122122

123123
private IEnumerable<Argument> Arguments => Children.OfType<Argument>();
124124

125+
internal bool DisallowBinding { get; set; }
126+
125127
public override string Name
126128
{
127129
get => base.Name;

src/System.CommandLine/Parsing/CommandResultExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ internal static bool TryGetValueForOption(
3737

3838
for (var i = 0; i < options.Count; i++)
3939
{
40-
var option = options[i];
40+
var option = (Option) options[i];
4141

42-
if (valueDescriptor.ValueName.IsMatch(option))
42+
if (!option.DisallowBinding &&
43+
valueDescriptor.ValueName.IsMatch(option))
4344
{
4445
var optionResult = commandResult.FindResultFor(option);
4546

0 commit comments

Comments
 (0)