45 lines
1.6 KiB
C#
45 lines
1.6 KiB
C#
using System.CommandLine;
|
|
using DevDisciples.Json.Tools.CLI.Extensions;
|
|
|
|
namespace DevDisciples.Json.Tools.CLI;
|
|
|
|
public partial class Json2CSharpCommand : Command
|
|
{
|
|
private static readonly Option<string> RootClassNameOption =
|
|
new(
|
|
aliases: ["--root-class", "--root", "-r"],
|
|
description: "The name of the root class",
|
|
getDefaultValue: () => Json2CSharpTranslator.Context.DefaultRootClassName
|
|
);
|
|
|
|
private static readonly Option<string> NamespaceOption = new(
|
|
aliases: ["--namespace", "--ns", "-n",],
|
|
description: "The namespace to use",
|
|
getDefaultValue: () => Json2CSharpTranslator.Context.DefaultNamespace
|
|
);
|
|
|
|
public Json2CSharpCommand() : base("2csharp", "Transform a JSON object into a C# class")
|
|
{
|
|
AddAlias("2cs");
|
|
AddAlias("2c#");
|
|
|
|
this.AddIOCommandOptions();
|
|
AddOption(RootClassNameOption);
|
|
AddOption(NamespaceOption);
|
|
|
|
this.SetHandler(ExecuteAsync, new CommandOptionsBinder());
|
|
}
|
|
|
|
private static async Task ExecuteAsync(CommandOptions options)
|
|
{
|
|
var json = await IOHandler.HandleInput(options.Input, options.InputArgument, options.InputFile);
|
|
|
|
var output = Json2CSharpTranslator.Translate(json, new()
|
|
{
|
|
RootClassName = options.RootClassName ?? Json2CSharpTranslator.Context.DefaultRootClassName,
|
|
Namespace = options.Namespace ?? Json2CSharpTranslator.Context.DefaultNamespace
|
|
});
|
|
|
|
await IOHandler.HandleOutput(options.Output, options.OutputFile, output);
|
|
}
|
|
} |