Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename Option.IsGlobal to AppliesToSelfAndChildren, make it public #2060

Merged
merged 4 commits into from
Feb 18, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ System.CommandLine
public System.Boolean TreatUnmatchedTokensAsErrors { get; set; }
public System.Collections.Generic.List<System.Action<System.CommandLine.Parsing.CommandResult>> Validators { get; }
public System.Void Add(Symbol symbol)
public System.Void AddGlobalOption(Option option)
public System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem> GetCompletions(System.CommandLine.Completions.CompletionContext context)
public System.Collections.Generic.IEnumerator<Symbol> GetEnumerator()
public static class CommandExtensions
Expand Down Expand Up @@ -184,6 +183,7 @@ System.CommandLine
public System.String VersionOptionDescription()
public abstract class Option : IdentifierSymbol, System.CommandLine.Binding.IValueDescriptor
public System.Boolean AllowMultipleArgumentsPerToken { get; set; }
public System.Boolean AppliesToSelfAndChildren { get; set; }
public System.String ArgumentHelpName { get; set; }
public ArgumentArity Arity { get; set; }
public System.Collections.Generic.List<System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem>>> CompletionSources { get; }
Expand Down
12 changes: 6 additions & 6 deletions src/System.CommandLine.Tests/CommandLineConfigurationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,13 @@ public void ThrowIfInvalid_throws_if_sibling_command_and_option_aliases_collide_
[Fact]
public void ThrowIfInvalid_throws_if_there_are_duplicate_sibling_global_option_aliases_on_the_root_command()
{
var option1 = new Option<string>("--dupe");
var option2 = new Option<string>("-y");
var option1 = new Option<string>("--dupe") { AppliesToSelfAndChildren = true };
var option2 = new Option<string>("-y") { AppliesToSelfAndChildren = true };
option2.AddAlias("--dupe");

var command = new RootCommand();
command.AddGlobalOption(option1);
command.AddGlobalOption(option2);
command.Options.Add(option1);
command.Options.Add(option2);

var config = new CommandLineConfiguration(command);

Expand All @@ -200,7 +200,7 @@ public void ThrowIfInvalid_does_not_throw_if_global_option_alias_is_the_same_as_
new Option<string>("--dupe")
}
};
rootCommand.AddGlobalOption(new Option<string>("--dupe"));
rootCommand.Options.Add(new Option<string>("--dupe") { AppliesToSelfAndChildren = true });

var config = new CommandLineConfiguration(rootCommand);

Expand All @@ -219,7 +219,7 @@ public void ThrowIfInvalid_does_not_throw_if_global_option_alias_is_the_same_as_
new Command("--dupe")
}
};
rootCommand.AddGlobalOption(new Option<string>("--dupe"));
rootCommand.Options.Add(new Option<string>("--dupe") { AppliesToSelfAndChildren = true });

var config = new CommandLineConfiguration(rootCommand);

Expand Down
7 changes: 4 additions & 3 deletions src/System.CommandLine.Tests/CommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,9 @@ public void When_Name_is_set_to_its_current_value_then_it_is_not_removed_from_al
[Fact]
public void AddGlobalOption_updates_Options_property()
{
var option = new Option<string>("-x");
var option = new Option<string>("-x") { AppliesToSelfAndChildren = true };
var command = new Command("mycommand");
command.AddGlobalOption(option);
command.Options.Add(option);

command.Options
.Should()
Expand All @@ -303,7 +303,8 @@ public void When_Options_is_referenced_before_a_global_option_is_added_then_addi
.Should()
.BeEmpty();

command.AddGlobalOption(option);
option.AppliesToSelfAndChildren = true;
command.Options.Add(option);

command.Options
.Should()
Expand Down
2 changes: 1 addition & 1 deletion src/System.CommandLine.Tests/CompletionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void Command_GetCompletions_returns_available_option_aliases_for_global_o
subcommand1
};

rootCommand.AddGlobalOption(new Option<string>("--three", "option three"));
rootCommand.Options.Add(new Option<string>("--three", "option three") { AppliesToSelfAndChildren = true });

var completions = subcommand2.GetCompletions(CompletionContext.Empty);

Expand Down
27 changes: 15 additions & 12 deletions src/System.CommandLine.Tests/GlobalOptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ public void Global_options_appear_in_options_list()
{
var root = new Command("parent");

var option = new Option<int>("--global");
var option = new Option<int>("--global") { AppliesToSelfAndChildren = true };

root.AddGlobalOption(option);
root.Options.Add(option);

var child = new Command("child");

Expand All @@ -32,9 +32,10 @@ public void When_a_required_global_option_is_omitted_it_results_in_an_error()
command.SetHandler(() => { });
var requiredOption = new Option<bool>("--i-must-be-set")
{
IsRequired = true
IsRequired = true,
AppliesToSelfAndChildren = true
};
rootCommand.AddGlobalOption(requiredOption);
rootCommand.Options.Add(requiredOption);

var result = rootCommand.Parse("child");

Expand All @@ -50,9 +51,10 @@ public void When_a_required_global_option_has_multiple_aliases_the_error_message
var rootCommand = new RootCommand();
var requiredOption = new Option<bool>(new[] { "-i", "--i-must-be-set" })
{
IsRequired = true
IsRequired = true,
AppliesToSelfAndChildren = true
};
rootCommand.AddGlobalOption(requiredOption);
rootCommand.Options.Add(requiredOption);

var result = rootCommand.Parse("");

Expand All @@ -70,9 +72,10 @@ public void When_a_required_global_option_is_present_on_child_of_command_it_was_
command.SetHandler(() => { });
var requiredOption = new Option<bool>("--i-must-be-set")
{
IsRequired = true
IsRequired = true,
AppliesToSelfAndChildren = true
};
rootCommand.AddGlobalOption(requiredOption);
rootCommand.Options.Add(requiredOption);

var result = rootCommand.Parse("child --i-must-be-set");

Expand All @@ -84,9 +87,9 @@ public void Subcommands_added_after_a_global_option_is_added_to_parent_will_reco
{
var root = new Command("parent");

var option = new Option<int>("--global");
var option = new Option<int>("--global") { AppliesToSelfAndChildren = true };

root.AddGlobalOption(option);
root.Options.Add(option);

var child = new Command("child");

Expand All @@ -106,9 +109,9 @@ public void Subcommands_with_global_option_should_propagate_option_to_children()

root.Subcommands.Add(firstChild);

var option = new Option<int>("--global");
var option = new Option<int>("--global") { AppliesToSelfAndChildren = true };

firstChild.AddGlobalOption(option);
firstChild.Options.Add(option);

var secondChild = new Command("second");

Expand Down
4 changes: 2 additions & 2 deletions src/System.CommandLine.Tests/ParseResultTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ public void ParseResult_GetCompletions_returns_global_options_of_given_command_o
{
leafCommand
};
midCommand1.AddGlobalOption(new Option<string>("--three1", "option three 1"));
midCommand1.Options.Add(new Option<string>("--three1", "option three 1") { AppliesToSelfAndChildren = true });

var midCommand2 = new Command("midCommand2")
{
leafCommand
};
midCommand2.AddGlobalOption(new Option<string>("--three2", "option three 2"));
midCommand2.Options.Add(new Option<string>("--three2", "option three 2") { AppliesToSelfAndChildren = true });

var rootCommand = new Command("root")
{
Expand Down
12 changes: 8 additions & 4 deletions src/System.CommandLine.Tests/ParsingValidationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ public void All_custom_validators_are_called(string commandLine)
[InlineData("subcommand --file \"Foo\"")]
public void Validators_on_global_options_are_executed_when_invoking_a_subcommand(string commandLine)
{
var option = new Option<FileInfo>("--file");
var option = new Option<FileInfo>("--file") { AppliesToSelfAndChildren = true };
option.Validators.Add(r =>
{
r.AddError("Invoked validator");
Expand All @@ -469,7 +469,7 @@ public void Validators_on_global_options_are_executed_when_invoking_a_subcommand
{
subCommand
};
rootCommand.AddGlobalOption(option);
rootCommand.Options.Add(option);

var result = rootCommand.Parse(commandLine);

Expand All @@ -495,7 +495,11 @@ public async Task A_custom_validator_added_to_a_global_option_is_checked(string
{
var handlerWasCalled = false;

var globalOption = new Option<int>("--value");
var globalOption = new Option<int>("--value")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to rename this local since we are removing the global concept?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about it, but I was unable to come up with a better name. appliesToSelfAndChildrenOption? inheritableOption?

{
AppliesToSelfAndChildren = true
};

globalOption.Validators.Add(r => r.AddError("oops!"));

var grandchildCommand = new Command("grandchild");
Expand All @@ -509,7 +513,7 @@ public async Task A_custom_validator_added_to_a_global_option_is_checked(string
childCommand
};

rootCommand.AddGlobalOption(globalOption);
rootCommand.Options.Add(globalOption);

rootCommand.SetHandler((int i) => handlerWasCalled = true, globalOption);
childCommand.SetHandler((int i) => handlerWasCalled = true, globalOption);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ internal static CommandLineBuilder UseHelp(
if (builder.HelpOption is null)
{
builder.HelpOption = helpOption;
builder.Command.AddGlobalOption(helpOption);
builder.Command.Options.Add(helpOption);
builder.MaxHelpWidth = maxWidth;
}
return builder;
Expand Down
14 changes: 1 addition & 13 deletions src/System.CommandLine/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,6 @@ public IEnumerable<Symbol> Children

internal bool HasValidators => _validators is not null && _validators.Count > 0;

/// <summary>
/// Adds a global <see cref="Option"/> to the command.
/// </summary>
/// <param name="option">The global option to add to the command.</param>
/// <remarks>Global options are applied to the command and recursively to subcommands. They do not apply to
/// parent commands.</remarks>
public void AddGlobalOption(Option option)
{
option.IsGlobal = true;
Options.Add(option);
}

/// <summary>
/// Adds a <see cref="Symbol"/> to the command.
/// </summary>
Expand Down Expand Up @@ -202,7 +190,7 @@ public override IEnumerable<CompletionItem> GetCompletions(CompletionContext con
{
var option = parentCommand.Options[i];

if (option.IsGlobal)
if (option.AppliesToSelfAndChildren)
{
AddCompletionsFor(option);
}
Expand Down
2 changes: 1 addition & 1 deletion src/System.CommandLine/Help/HelpBuilder.Default.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public static Action<HelpContext> OptionsSection() =>
foreach (var option in parentCommand.Options)
{
// global help aliases may be duplicated, we just ignore them
if (option.IsGlobal && !option.IsHidden && uniqueOptions.Add(option))
if (option.AppliesToSelfAndChildren && !option.IsHidden && uniqueOptions.Add(option))
{
options.Add(ctx.HelpBuilder.GetTwoColumnRow(option, ctx));
}
Expand Down
2 changes: 1 addition & 1 deletion src/System.CommandLine/Help/HelpBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ IEnumerable<string> GetUsageParts()
{
if (!displayOptionTitle)
{
displayOptionTitle = parentCommand.HasOptions && parentCommand.Options.Any(x => x.IsGlobal && !x.IsHidden);
displayOptionTitle = parentCommand.HasOptions && parentCommand.Options.Any(x => x.AppliesToSelfAndChildren && !x.IsHidden);
}

yield return parentCommand.Name;
Expand Down
1 change: 1 addition & 0 deletions src/System.CommandLine/Help/HelpOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public HelpOption(string[] aliases, Func<LocalizationResources> getLocalizationR
: base(aliases, null, new Argument<bool> { Arity = ArgumentArity.Zero })
{
_localizationResources = getLocalizationResources;
AppliesToSelfAndChildren = true;
}

public HelpOption(Func<LocalizationResources> getLocalizationResources) : this(new[]
Expand Down
6 changes: 3 additions & 3 deletions src/System.CommandLine/Option.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ public ArgumentArity Arity
}

/// <summary>
/// Global options are applied to the command and recursively to subcommands.
/// They do not apply to parent commands.
/// When set to true, this option will be applied to the command and recursively to subcommands.
/// It will not apply to parent commands.
/// </summary>
internal bool IsGlobal { get; set; }
public bool AppliesToSelfAndChildren { get; set; }

/// <summary>
/// Validators that will be called when the option is matched by the parser.
Expand Down
2 changes: 1 addition & 1 deletion src/System.CommandLine/Parsing/CommandResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private void ValidateOptions(bool completeValidation)
{
var option = options[i];

if (!completeValidation && !(option.IsGlobal || option.Argument.HasDefaultValue || (option is HelpOption or VersionOption)))
if (!completeValidation && !(option.AppliesToSelfAndChildren || option.Argument.HasDefaultValue || (option is HelpOption or VersionOption)))
{
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions src/System.CommandLine/Parsing/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ private static Dictionary<string, Token> ValidTokens(this Command command)
Option option = options[childIndex];
foreach (string childAlias in option.Aliases)
{
if (!option.IsGlobal || !tokens.ContainsKey(childAlias))
if (!option.AppliesToSelfAndChildren || !tokens.ContainsKey(childAlias))
{
tokens.Add(childAlias, new Token(childAlias, TokenType.Option, option, Token.ImplicitPosition));
}
Expand All @@ -476,7 +476,7 @@ private static Dictionary<string, Token> ValidTokens(this Command command)
for (var i = 0; i < parentCommand.Options.Count; i++)
{
Option option = parentCommand.Options[i];
if (option.IsGlobal)
if (option.AppliesToSelfAndChildren)
{
foreach (var childAlias in option.Aliases)
{
Expand Down