Skip to content

dotnet CLI: Add --cli-schema option for CLI structure JSON #49118

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

MiYanni
Copy link
Member

@MiYanni MiYanni commented May 22, 2025

Resolves: #46345

Summary

TODO

…utput the structure of that command in JSON. This still has work to be done to refine the output.
@MiYanni MiYanni requested a review from baronfel May 22, 2025 23:37
Comment on lines 92 to 112
writer.WriteStartObject(nameof(command.Arguments).ToCamelCase());
foreach (var argument in command.Arguments.OrderBy(a => a.Name))
{
// TODO: Check these
writer.WriteStartObject(argument.Name);

writer.WriteString(nameof(argument.Description).ToCamelCase(), argument.Description);
writer.WriteBoolean(nameof(argument.Hidden).ToCamelCase(), argument.Hidden);
writer.WriteBoolean("hasValidators", argument.GetHasValidators() ?? false);
writer.WriteString(nameof(argument.HelpName).ToCamelCase(), argument.HelpName);
writer.WriteString(nameof(argument.ValueType).ToCamelCase(), argument.ValueType.FullName);
writer.WriteBoolean(nameof(argument.HasDefaultValue).ToCamelCase(), argument.HasDefaultValue);

writer.WriteStartObject(nameof(argument.Arity).ToCamelCase());
writer.WriteNumber(nameof(argument.Arity.MinimumNumberOfValues).ToCamelCase(), argument.Arity.MinimumNumberOfValues);
writer.WriteNumber(nameof(argument.Arity.MaximumNumberOfValues).ToCamelCase(), argument.Arity.MaximumNumberOfValues);
writer.WriteEndObject();

writer.WriteEndObject();
}
writer.WriteEndObject();
Copy link
Member Author

Choose a reason for hiding this comment

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

TODO: Local functions might make sense for each of these sub-sections.

Comment on lines +8 to +10
#pragma warning disable IDE0065 // Misplaced using directive
using Command = System.CommandLine.Command;
#pragma warning restore IDE0065 // Misplaced using directive
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 is bizarre but necessary since declaring the using above the namespace means that anything in the namespace scope will override it. Meaning, System.DotNet.Cli.Utils.Command will be used even if an alias is declared. So, you put the alias inside the namespace to have it be used properly.

Copy link
Member Author

Choose a reason for hiding this comment

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

TODO: We should just change that Command class to not be called Command since CliCommand from S.CL was changed back to Command, which causes situations like this to happen.

Comment on lines 143 to 147
if (parseResult.GetValue(Parser.CliSchemaOption))
{
CommandLineInfo.PrintCliSchema(parseResult.CommandResult.Command);
return 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 is now the highest priority option since every other option is irrelevant if this one is present.

MiYanni added 2 commits May 23, 2025 16:32
…ded name for root command. Cleaned up type output. Added default value output. Removed ordering for arguments. Added relaxed JSON encoding.
… in-person discussion. Added version to the root. Shared json serialization options. Created strings for the --cli-schema description.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create a way to get a structured representation of a given .NET CLI command
1 participant