jtr/DevDisciples.Json.Tools.CLI/JsonBeautifyCommand.cs

31 lines
1.0 KiB
C#

using System.CommandLine;
using DevDisciples.Json.Tools.CLI.Extensions;
namespace DevDisciples.Json.Tools.CLI;
public partial class JsonBeautifyCommand : Command
{
private static readonly Option<int> IndentSizeOption =
new(aliases: ["--indent", "--is"], description: "The indent size", getDefaultValue: () => 2);
public JsonBeautifyCommand() : base("beautify", "Beautify JSON")
{
AddAlias("b");
this.AddInputOutputCommandOptions();
AddOption(IndentSizeOption);
this.SetHandler(ExecuteAsync, new CommandOptionsBinder());
}
private static async Task ExecuteAsync(CommandOptions options)
{
var json = await InputOutputHandler.HandleInput(options.Input, options.InputArgument, options.InputFile);
var output = JsonFormatter.Format(json, new()
{
Beautify = true,
IndentSize = options.IndentSize
});
await InputOutputHandler.HandleOutput(options.Output, options.OutputFile, output);
}
}