/// A command-line tool for JSON parsing and file manipulation. #include #include #include #include #include #include #include #include #include #include "AnsiEscapeCodes.hpp" /// Open a text file and return the contents. std::string read_file(const std::string& file_path) { std::ifstream file(file_path, std::ios::binary); if (!file) throw std::runtime_error("We couldn't find the file: " + file_path); file.seekg(0, std::ios::end); std::streamsize file_size = file.tellg(); file.seekg(0, std::ios::beg); std::string file_content(file_size, '\0'); file.read(&file_content[0], file_size); file.close(); return file_content; } void parse_json_file(const std::string& file_path) { auto file_content = read_file(file_path); auto [text, parse_error] = json::parse(file_content); if (!parse_error.empty()) std::cerr << "Error while parsing json: " << parse_error << std::endl; std::cout << json::deparse(text) << std::endl; } void show_help() { std::cout << "Usage:" << std::endl; std::cout << ">json [--options] content" << std::endl; std::cout << "Options: " << std::endl; std::cout << "--help --about --license " << std::endl; } void show_license() { std::cout << "" << std::endl; } void show_version() { } /// Parses a string of JSON and outputs any syntax errors, if present. void validate_json_string(const std::string& input) { } void echo_json_formatted(const std::string& input) { } void show_readme() { std::cout << Colors::Oranges::Coral.ToEscapeCode(false); std::cout << "A cli tool for parsing json files." << std::endl; std::cout << "Developed & maintained by josh@redacted.cc" << std::endl; std::cout << "This program is included with the Redacted Software JSON Library for C++." << std::endl; std::cout << "https://git.redacted.cc/josh/json" << std::endl; std::cout << "Run `json --help` for help information." << std::endl; std::cout << "Run `json --license` for license information." << std::endl; std::cout << mcolor::AnsiEscapeCodes::ResetAll; } /// void print_all_args(int argc, char* argv[]) { } /// Read options and their args, remove from master args list. int json_app(std::vector params) { bool color_output = true; std::string input_content; bool read_from_stdin = true; bool verbose = false; ArgsParser args(params); //std::vector args(argv, argv+argc); // Terminal Options - Parsed first, close the program w/o further action. if (args.has_flag({"--about", "--info", "--readme"})) { show_readme(); return 0; } if (args.has_flag({"--help", "-h", "--h"})) { show_help(); return 0; } if (args.has_flag({"--license", "-L"})) { show_license(); return 0; } if (args.has_flag("--raw")) color_output = false; if (args.has_flag("--colorful")) color_output = true; if (args.has_flag("--quiet")) verbose = false; if (args.has_flag("--verbose")) verbose = true; if (args.has_flag("--file")) { if (args.has_flag_arg("--file")) { auto file_name = args.consume_flag_arg("--file").value(); if (!std::filesystem::exists(file_name)) { std::cout << "Error: File " << file_name << "not found!"; } input_content = read_file(file_name); read_from_stdin = false; } } if (read_from_stdin) { std::string line; std::getline(std::cin, line); // TODO: Add mode for merge? input_content = line; //while(std::getline(std::cin, line) && !line.empty()) { // input_content += line; //} } if (args.has_flag("--validate")) { validate_json_string(input_content); return 0; } std::vector remaining = args.get_remaining_args(); for (auto& e : remaining) { std::cout << e << " "; } std::cout << std::endl; if (input_content.empty()) { if (!verbose) std::cout << "No input provided!" << std::endl;; return 0; } auto query_tokens = args.get_remaining_args(); if (!query_tokens.empty()) { } auto[val, err] = json::parse(input_content); if (!err.empty()) { std::cerr << "Error reading JSON: " << err << std::endl; return -1; } std::string roundtripped = json::deparse(val); std::cout << roundtripped << std::endl; return 0; } int main(int argc, char* argv[]) { ArgsParser pre_parser(argc, argv); if (pre_parser.has_flag("--test-all")) { int readme = json_app({"--readme"}); int help = json_app({"--help"}); int license = json_app({"--license"}); int version = json_app({"--version"}); int result_0 = json_app({"--file", "nonexistant.json"}); int result_1 = json_app({"--validate", "{\"test\":42069}"}); return 0; } else return json_app(pre_parser.get_remaining_args()); }