Skip to content

Name and aliases separation #2073

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

Merged
merged 7 commits into from
Mar 7, 2023

Conversation

adamsitnik
Copy link
Member

@adamsitnik adamsitnik commented Mar 3, 2023

It's a second attempt (the first one was #2067). For motivation please go to #2053 (comment)

Changes:

  • Separation of Name and Aliases concepts. Name needs to be always provided in an explicit way and can not be empty or changed later. We don't remove prefixes from Name anymore (as is). Name is also not added to Aliases anymore.
  • Simplification of the Symbol constructors. Each symbol type provides now only a single public ctor. The ctor requires to provide only the mandatory data (name), everything else needs to be provided by using the properties.
  • Removal of SetDefaultValue, SetDefaultValueFactory methods, they have been repalced with a single property.
  • Removal of IdentifierSymbol type. The aliases are now represented as a single property. It makes it very easy to implement HiddenAliases feature. Once this PR gets merged I am going to create an issue for that (or find an existing one and add description)
  • Rename of Option.ArgumentHelpName to just HelpName (the fact that we are using an Argument is an implementation detail)

Breaking change: users who were using Option(string name, string description) ctor, are now going to hit a silent error, as compiler won't warn them that description has became an alias now.
We are soon going to rename Option to CliOpton and expect the end users to not hit the silent error (we are currently not releasing any packages to nuget.org).
Those who are going to hit this issue are most likely going to get an exception at runtime, as description typically contains whitespaces and aliases can't.

fixes #1895
fixes #2038
fixes #2051
fixes #2053

unblocks #2052 (required data will be always present)

/// Initializes a new instance of the Argument class.
/// </summary>
protected Argument()
private protected Argument(string name) : base(name, canContainWhitespaces: true)
Copy link
Member Author

Choose a reason for hiding this comment

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

since arguments name is not used for parsing, I've decided to keep allowing the whitespaces. I don't have a strong opinion about that.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree.

}
else if (!argument.IsBoolean() && argument.CompletionSources.Count > 0)
Copy link
Member Author

Choose a reason for hiding this comment

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

this change fixes #2038

First of all we check HelpName, if it's not provided and there are some CompletionSources defined for given Argument, then we are getting the completions.

Choose a reason for hiding this comment

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

It now adds <> around HelpName unlike before.

Copy link

@KalleOlaviNiemitalo KalleOlaviNiemitalo Mar 8, 2023

Choose a reason for hiding this comment

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

Huh, no, it did add <> already in 2.0 beta 4.

new Option<string>("--three", "option three")
new Option<string>("--one") { Description = "option one" },
new Option<string>("--two") { Description = "option two" },
new Option<string>("--three") { Description = "option three" },
Copy link
Contributor

Choose a reason for hiding this comment

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

The change to the params constructor for aliases results in some extra verbosity here and I wonder if it's the right tradeoff, given that people using Option set a description much more often than aliases.

Copy link
Member Author

Choose a reason for hiding this comment

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

We have at least three ways of setting the aliases:

  • what we had so far, which required the user to allocate an array in an explicit way
new Option<bool>("--help", new [] { "-h", "-?" }, "description");
  • using params, which looks way nicer, but requires the description to be set via a property. TBH I am not sure if it's a bad thing, but it's very subjective.
new Option<bool>("--help", "-h", "-?")
{
    Description = "description"
};
  • using collection initalizer syntax. IMO it looks great but I would expect very few C# devs to know about this duck typing.
new Option<bool>("--help", "description")
{
    Aliases = { "-h", "-?" }
};

@jonsequitur if you believe it's worth it, I can remove the params ctor and revert this part of the changes. My personal preference would be to merge it in the new form and eventually revisit it after API design review.

Copy link
Contributor

Choose a reason for hiding this comment

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

My hesitation is that the more common case is now more verbose, and it will be a very large source-breaking change. The reasoning makes sense though. I'd be curious to hear more opinions.

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'd be curious to hear more opinions.

@KathleenDollard @Keboo @KalleOlaviNiemitalo what is your opinion on that?

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 am going to merge the PR now, but please don't hesitate to share your feedback.

Copy link
Member

Choose a reason for hiding this comment

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

Another option you have is to treat the aliases as peers to one another when supplying them, but have the convention that the first in the array is the primary and the rest are aliases.

new Option<bool>(new[] { "--help", "-h", "-?" }, "description");

Or move the aliases to the end of the parameter list:

new Option<bool>("--help", "description", new[] { "-h", "-?" });

I'm not a fan of the params for the aliases regardless though. With new[] { } being as succinct as it is (compared to when C# was young), I think it's harder to justify params for something like this. And I concur with @jonsequitur's position that the more common case is now more verbose.

Copy link
Member

Choose a reason for hiding this comment

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

In my first example above, I meant to explicitly state that there'd still be the single-string overload too. Calling that out in case it wasn't inferred.

Option(string name, string description);
Option(string[] nameAndAliases, string description);

isDefault: true);
var option = new Option<int>("-x")
{
DefaultValueFactory = _ => 123
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd love to come up with a better name for this.

@adamsitnik adamsitnik merged commit 7d3cd43 into dotnet:main Mar 7, 2023
@adamsitnik adamsitnik deleted the nameAndAliasesSeparation branch March 7, 2023 18:25
YuliiaKovalova pushed a commit to dotnet/templating that referenced this pull request Mar 9, 2023
* Update dependencies from https://github.com/dotnet/command-line-api build 20230307.2

System.CommandLine
 From Version 2.0.0-beta4.23152.1 -> To Version 2.0.0-beta4.23157.2

* Adjust the code following breaking change in Option ctor by dotnet/command-line-api#2073

---------

Co-authored-by: dotnet-maestro[bot] <dotnet-maestro[bot]@users.noreply.github.com>
Co-authored-by: Gang Wang <[email protected]>
YuliiaKovalova pushed a commit to dotnet/templating that referenced this pull request Mar 9, 2023
* Update dependencies from https://github.com/dotnet/command-line-api build 20230307.2

System.CommandLine
 From Version 2.0.0-beta4.23152.1 -> To Version 2.0.0-beta4.23157.2

* Adjust the code following breaking change in Option ctor by dotnet/command-line-api#2073

---------

Co-authored-by: dotnet-maestro[bot] <dotnet-maestro[bot]@users.noreply.github.com>
Co-authored-by: Gang Wang <[email protected]>
YuliiaKovalova pushed a commit to dotnet/templating that referenced this pull request Mar 9, 2023
* Update dependencies from https://github.com/dotnet/command-line-api build 20230307.2

System.CommandLine
 From Version 2.0.0-beta4.23152.1 -> To Version 2.0.0-beta4.23157.2

* Adjust the code following breaking change in Option ctor by dotnet/command-line-api#2073

---------

Co-authored-by: dotnet-maestro[bot] <dotnet-maestro[bot]@users.noreply.github.com>
Co-authored-by: Gang Wang <[email protected]>
YuliiaKovalova added a commit to dotnet/templating that referenced this pull request Mar 9, 2023
…6224)

* Update dependencies from https://github.com/dotnet/command-line-api build 20230307.2

System.CommandLine
 From Version 2.0.0-beta4.23152.1 -> To Version 2.0.0-beta4.23157.2

* [main] Update dependencies from dotnet/command-line-api (#6222)

* Update dependencies from https://github.com/dotnet/command-line-api build 20230307.2

System.CommandLine
 From Version 2.0.0-beta4.23152.1 -> To Version 2.0.0-beta4.23157.2

* Adjust the code following breaking change in Option ctor by dotnet/command-line-api#2073

---------

Co-authored-by: dotnet-maestro[bot] <dotnet-maestro[bot]@users.noreply.github.com>
Co-authored-by: Gang Wang <[email protected]>

* adjust to changes in command line

---------

Co-authored-by: dotnet-maestro[bot] <dotnet-maestro[bot]@users.noreply.github.com>
Co-authored-by: dotnet-maestro[bot] <42748379+dotnet-maestro[bot]@users.noreply.github.com>
Co-authored-by: Gang Wang <[email protected]>
Co-authored-by: YuliiaKovalova <[email protected]>
YuliiaKovalova pushed a commit to dotnet/templating that referenced this pull request Mar 9, 2023
…ne-api (#6225)

* Update dependencies from https://github.com/dotnet/command-line-api build 20230307.2

System.CommandLine
 From Version 2.0.0-beta4.23152.1 -> To Version 2.0.0-beta4.23157.2

* [main] Update dependencies from dotnet/command-line-api (#6222)

* Update dependencies from https://github.com/dotnet/command-line-api build 20230307.2

System.CommandLine
 From Version 2.0.0-beta4.23152.1 -> To Version 2.0.0-beta4.23157.2

* Adjust the code following breaking change in Option ctor by dotnet/command-line-api#2073

---------

Co-authored-by: dotnet-maestro[bot] <dotnet-maestro[bot]@users.noreply.github.com>
Co-authored-by: Gang Wang <[email protected]>

---------

Co-authored-by: dotnet-maestro[bot] <dotnet-maestro[bot]@users.noreply.github.com>
Co-authored-by: dotnet-maestro[bot] <42748379+dotnet-maestro[bot]@users.noreply.github.com>
Co-authored-by: Gang Wang <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants