All checks were successful
Run the JSON parser tests / test (push) Has been skipped
32 lines
1.1 KiB
C#
32 lines
1.1 KiB
C#
using System.CommandLine;
|
|
using Jtr.Tools.CLI.Extensions;
|
|
|
|
namespace Jtr.Tools.CLI;
|
|
|
|
public partial class JsonPathCommand : Command
|
|
{
|
|
private static readonly Option<string> PathExpressionOption =
|
|
new(
|
|
aliases: ["--expr", "-e"],
|
|
description: "The path expression", getDefaultValue: () => "$"
|
|
) { IsRequired = true };
|
|
|
|
public JsonPathCommand() : base("path", "Evaluate a JSON path expression")
|
|
{
|
|
AddAlias("p");
|
|
this.AddInputOutputCommandOptions();
|
|
this.AddOption(PathExpressionOption);
|
|
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 = JsonPath.Interpreter.Evaluate(json, options.PathExpression);
|
|
|
|
var beautifiedOutput = JsonFormatter.Format(output, new() { Beautify = true });
|
|
|
|
await InputOutputHandler.HandleOutput(options.Output, options.OutputFile, beautifiedOutput);
|
|
}
|
|
} |