jtr/DevDisciples.Json.Tools.CLI/JsonPrettifyCommand.cs
2024-09-15 17:23:27 +02:00

32 lines
1.0 KiB
C#

using System.CommandLine;
using DevDisciples.Json.Tools.CLI.Extensions;
namespace DevDisciples.Json.Tools.CLI;
public partial class JsonPrettifyCommand : Command
{
private static readonly Option<int> IndentSizeOption =
new(aliases: ["--indent", "--is"], description: "The indent size", getDefaultValue: () => 2);
public JsonPrettifyCommand() : base("prettify", "Prettify JSON")
{
AddAlias("p");
this.AddIOCommandOptions();
AddOption(IndentSizeOption);
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 = JsonFormatter.Format(json, new()
{
Beautify = true,
IndentSize = options.IndentSize
});
await IOHandler.HandleOutput(options.Output, options.OutputFile, output);
}
}