jtr/Jtr.Tools.CLI/JsonBeautifyCommand.cs
mdnapo 96c203f842
All checks were successful
Run the JSON parser tests / test (push) Has been skipped
Moved UI to separate project, added Dockerfile & renamed project
2024-10-19 12:04:40 +02:00

31 lines
1023 B
C#

using System.CommandLine;
using Jtr.Tools.CLI.Extensions;
namespace Jtr.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);
}
}