-
Notifications
You must be signed in to change notification settings - Fork 401
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
Conversation
src/System.CommandLine/Argument.cs
Outdated
/// Initializes a new instance of the Argument class. | ||
/// </summary> | ||
protected Argument() | ||
private protected Argument(string name) : base(name, canContainWhitespaces: true) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
return $"<{firstColumn}>"; |
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" }, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
Co-authored-by: Jon Sequeira <[email protected]>
* 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]>
* 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]>
* 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]>
…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]>
…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]>
It's a second attempt (the first one was #2067). For motivation please go to #2053 (comment)
Changes:
Name
andAliases
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 toAliases
anymore.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.SetDefaultValue
,SetDefaultValueFactory
methods, they have been repalced with a single property.IdentifierSymbol
type. The aliases are now represented as a single property. It makes it very easy to implementHiddenAliases
feature. Once this PR gets merged I am going to create an issue for that (or find an existing one and add description)Option.ArgumentHelpName
to justHelpName
(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 thatdescription
has became analias
now.We are soon going to rename
Option
toCliOpton
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)