Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

adamsitnik
Copy link
Member

This is very similar to #1987, but since adding a new Symbol to Command requires setting a parent I had to implement a custom IList<T> that sets the parent on every add.

/// Represents all of the options for the command, including global options that have been applied to any of the command's ancestors.
/// </summary>
public IReadOnlyList<Option> Options => _options is not null ? _options : Array.Empty<Option>();
public IList<Option> Options => _options ??= new (this);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
public IList<Option> Options => _options ??= new (this);
public IList<Option> Options => _options ??= new(this);

/// <summary>
/// a wrapper of List<typeparamref name="T"/> that sets parent for every added element
/// </summary>
internal sealed class ChildList<T> : IList<T> where T : Symbol
Copy link
Member

Choose a reason for hiding this comment

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

Did you consider this?

Suggested change
internal sealed class ChildList<T> : IList<T> where T : Symbol
internal sealed class ChildList : IList<Symbol>

Copy link
Member Author

Choose a reason for hiding this comment

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

It would be impossible to enforce the right types without perf overhead. Example:

Option<bool> option = new ("--some);
RootCommand root = new ();
// in the line below Arguments would be a list of Symbols, which would allow for adding not just arguments but also options and commands
root.Arguments.Add(option); // no compilation error, at runtime we would later find out that it's not an Argument

/// <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)
Copy link
Member

Choose a reason for hiding this comment

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

Is weird that now that you are removing AddOption we keep AddGlobalOption. If we follow the pattern this should be named AddGlobal, no? But that sounds weird.

Not asking for a change, just wanted to point that out.

Copy link
Member Author

Choose a reason for hiding this comment

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

That is on my TODO list, we just need to find a good name for "global" options. In the near future it might look like this:

Option<bool> help = new ("--help);
help.IsCommon = true;
command.Options.Add(help);

# Conflicts:
#	src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt
@adamsitnik
Copy link
Member Author

I believe I've answered all the questions, merging.

@adamsitnik adamsitnik merged commit 704e640 into dotnet:main Dec 15, 2022
@adamsitnik adamsitnik deleted the commandLists branch December 15, 2022 14:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants