Skip to content

Move directives to CliRootCommand - #2224

Merged
jonsequitur merged 4 commits into
dotnet:mainfrom
jonsequitur:move-directives-to-CliRootCommand
Jun 15, 2023
Merged

jonsequitur merged 4 commits into
dotnet:mainfrom
jonsequitur:move-directives-to-CliRootCommand

Conversation

@jonsequitur

Copy link
Copy Markdown
Contributor

No description provided.

@jonsequitur
jonsequitur merged commit 6b9b8c8 into dotnet:main Jun 15, 2023

@adamsitnik adamsitnik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Overall the fix looks very good, thank you @jonsequitur !

I am curious why you have decided to move the directives from config to root command now. I am not against it, but it's going to have non-trivial impact on the future of RootCommand (we won't be able to delete it). Are you planning to move more things from the config to the root command?

CliDirective configurationDirective = parseResult.Configuration.Directives.Single(d => d.Name == "config");
hostBuilder.ConfigureHostConfiguration(config => { config.AddCommandLineDirectives(parseResult, configurationDirective); });
if (parseResult.Configuration.RootCommand is CliRootCommand root &&
root.Directives.SingleOrDefault(d => d.Name == HostingDirectiveName) is { } directive)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

subjective: I find is { } directive very unintuitive

_children = new();
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: whitespaces

Comment on lines +30 to +35
internal bool HasDirectives =>
RootCommand switch
{
CliRootCommand root => root.Directives.Count > 0,
_ => false
};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

subjective: IMO a switch statement for two possibilities is not worth it

internal bool HasDirectives => RootCommand is CliRootCommand root && root.Directives.Count > 0;

? (CliDirective)directiveToken.Symbol!
: null;
CliDirective? directive;
if (knownTokens.TryGetValue($"[{directiveName}]", out var directiveToken))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

So the actual parse error fix was to wrap the directive name in []?

May I ask why have you decided to move CliDirective from config to root command then? I am not against it, I am just curious whether overall you got to a conclusion that it's where it belongs or whether I can't see some other aspect of the actual fix.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This change isn't ideal but it's a step toward reducing the impact of a larger bug (#2223). It can't be fully fixed without incorporating directives into the symbol model, which is why moving them to CliRootCommand at this point was helpful. It's a design decision we discussed a few times for the sake of consistency. Tactically, this was a good time to do it since it related to this bug.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It can't be fully fixed without incorporating directives into the symbol model

Thanks, now I know all I needed! 👍

Dictionary<string, CliToken> tokens = new(StringComparer.Ordinal);

if (directives is not null)
if (command is CliRootCommand { Directives: IList<CliDirective> directives })

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

again subjective on style: what do we benefit from using this syntax here?

if (command is CliRootCommand { Directives: IList<CliDirective> directives })
{
    for (int i = 0; i < directives.Count; i++)
if (command is CliRootCommand rootCommand)
{
    for (int i = 0; i < rootCommand.Directives.Count; i++)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This avoids an intermediate assignment of a CliRootCommand variable when all we need from it is the directives:

if (command is CliRootCommand { Directives: IList<CliDirective> directives })

I don't know if this makes any difference from a performance perspective but I find it (subjectively, again) to be less cluttered.

@jonsequitur

Copy link
Copy Markdown
Contributor Author

Are you planning to move more things from the config to the root command?

The sense from a few design discussions we've had about this is that since directives are only valid at the root, and since they're symbols, it's consistent and intuitive for them to be children of CliRootCommand rather than CliConfiguration.

The other changes we've discussed making to CliConfiguration involve factoring out invocation-related members to create a cleaner separation between parser and invocation concepts, and to leave CliConfiguration focused specifically on parsing concerns that are cross-cutting and not specific to any symbols, e.g. token replacement / response files, POSIX bundling behaviors, and so on.

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