Files
json/main.cpp

243 lines
5.4 KiB
C++

/// A command-line tool for JSON parsing and file manipulation.
#include <json.hpp>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
/// 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);
std::streamsize file_size;
file.seekg(0, std::ios::end);
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;
}
namespace argue {
class ArgsParser {
public:
ArgsParser(int argc, char** argv) {
args = std::vector<std::string>(argv, argv+argc);
}
explicit ArgsParser(const std::vector<std::string>& value): args(value) {}
bool has_flag(const std::string& option) {
return std::ranges::find(args, option) != args.end();
}
bool has_flag(const std::initializer_list<std::string>& flags) {
for (auto& flag : flags)
if (has_flag(flag))
return true;
return false;
}
/// Searches the arguments for all given flag tokens, and removes all occurrences of it.
/// @return true if the flag was found in the arguments.
bool consume_flag(const std::string& option) {
if (has_flag(option)) {
args.erase(std::remove(args.begin(), args.end(), option), args.end());
return true;
}
return false;
}
/// Searches the arguments for all given flag tokens, and removes them.
/// @return True if the flag was found in the arguments.
bool consume_flag(const std::initializer_list<std::string>& flags) {
bool had_any_flag = false;
for (auto& flag : flags) {
if (has_flag(flags)) {
had_any_flag = true;
args.erase(std::remove(args.begin(), args.end(), flag), args.end());
}
}
return had_any_flag;
}
bool has_flag_arg(const std::string& option) {
if (has_flag(option)) {
auto it = std::ranges::find(args, option);
return it!=args.end() && ++it!=args.end();
}
}
std::string get_flag_arg(const std::string& option) {
auto it = std::ranges::find(args, option);
++it;
return *it;
}
std::tuple<bool, std::string> consume_flag_arg(const std::string& flag) {
}
std::tuple<bool, std::string> consume_flag_arg(const std::initializer_list<std::string>& flags) {
}
std::vector<std::string> args;
protected:
private:
};
}
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 << "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;
}
/// Read options and their args, remove from master args list.
int main(int argc, char* argv[]) {
struct flag {
std::string name;
std::vector<std::string> aliases;
std::vector<std::string> Matches() {
auto copy = aliases;
copy.push_back(name);
}
};
bool color_output = true;
std::string input_content;
bool read_from_stdin = true;
argue::ArgsParser args(argc,argv);
//std::vector<std::string> 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_arg("--file"))
{
auto [success, file_name] = args.consume_flag_arg("--file");
input_content = read_file(file_name);
read_from_stdin = false;
}
if (read_from_stdin)
{
std::string 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<std::string> remaining = args.args;
for (auto& e : remaining) {
std::cout << e << " ";
}
std::cout << std::endl;
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;
}