Making edits and integrating ArgsParser
This commit is contained in:
@@ -32,7 +32,7 @@ add_executable(json_cli main.cpp)
|
|||||||
|
|
||||||
CPMAddPackage(
|
CPMAddPackage(
|
||||||
NAME ArgsParser
|
NAME ArgsParser
|
||||||
URL https://git.redacted.cc/josh/ArgsParser/archive/Prerelease-1.zip
|
URL https://git.redacted.cc/josh/ArgsParser/archive/Prerelease-3.zip
|
||||||
)
|
)
|
||||||
|
|
||||||
CPMAddPackage(
|
CPMAddPackage(
|
||||||
|
88
main.cpp
88
main.cpp
@@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
#include <Colors.hpp>
|
#include <Colors.hpp>
|
||||||
#include <ArgsParser.hpp>
|
#include <ArgsParser.hpp>
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
#include "AnsiEscapeCodes.hpp"
|
#include "AnsiEscapeCodes.hpp"
|
||||||
|
|
||||||
@@ -19,9 +20,8 @@ std::string read_file(const std::string& file_path)
|
|||||||
if (!file)
|
if (!file)
|
||||||
throw std::runtime_error("We couldn't find the file: " + file_path);
|
throw std::runtime_error("We couldn't find the file: " + file_path);
|
||||||
|
|
||||||
std::streamsize file_size;
|
|
||||||
file.seekg(0, std::ios::end);
|
file.seekg(0, std::ios::end);
|
||||||
file_size = file.tellg();
|
std::streamsize file_size = file.tellg();
|
||||||
file.seekg(0, std::ios::beg);
|
file.seekg(0, std::ios::beg);
|
||||||
|
|
||||||
std::string file_content(file_size, '\0');
|
std::string file_content(file_size, '\0');
|
||||||
@@ -44,7 +44,7 @@ void parse_json_file(const std::string& file_path) {
|
|||||||
|
|
||||||
void show_help() {
|
void show_help() {
|
||||||
std::cout << "Usage:" << std::endl;
|
std::cout << "Usage:" << std::endl;
|
||||||
std::cout << ">json <--options> content" << std::endl;
|
std::cout << ">json [--options] content" << std::endl;
|
||||||
std::cout << "Options: " << std::endl;
|
std::cout << "Options: " << std::endl;
|
||||||
std::cout << "--help --about --license " << std::endl;
|
std::cout << "--help --about --license " << std::endl;
|
||||||
}
|
}
|
||||||
@@ -90,6 +90,7 @@ int json_app(std::vector<std::string> params) {
|
|||||||
bool color_output = true;
|
bool color_output = true;
|
||||||
std::string input_content;
|
std::string input_content;
|
||||||
bool read_from_stdin = true;
|
bool read_from_stdin = true;
|
||||||
|
bool verbose = false;
|
||||||
|
|
||||||
ArgsParser args(params);
|
ArgsParser args(params);
|
||||||
|
|
||||||
@@ -110,24 +111,43 @@ int json_app(std::vector<std::string> params) {
|
|||||||
show_license(); return 0;
|
show_license(); return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (args.has_flag("--raw"))
|
if (args.has_flag("--raw")) color_output = false;
|
||||||
color_output = false;
|
|
||||||
|
|
||||||
if (args.has_flag_arg("--file"))
|
if (args.has_flag("--colorful")) color_output = true;
|
||||||
{
|
|
||||||
|
|
||||||
auto file_name = args.consume_flag_arg("--file").value();
|
if (args.has_flag("--quiet")) verbose = false;
|
||||||
|
|
||||||
input_content = read_file(file_name);
|
if (args.has_flag("--verbose")) verbose = true;
|
||||||
read_from_stdin = false;
|
|
||||||
|
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)
|
if (read_from_stdin)
|
||||||
{
|
{
|
||||||
std::string line;
|
std::string line;
|
||||||
while(std::getline(std::cin, line) && !line.empty()) {
|
|
||||||
input_content += 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"))
|
if (args.has_flag("--validate"))
|
||||||
@@ -142,6 +162,20 @@ int json_app(std::vector<std::string> params) {
|
|||||||
}
|
}
|
||||||
std::cout << std::endl;
|
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);
|
auto[val, err] = json::parse(input_content);
|
||||||
|
|
||||||
if (!err.empty()) {
|
if (!err.empty()) {
|
||||||
@@ -156,27 +190,21 @@ int json_app(std::vector<std::string> params) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
struct flag {
|
ArgsParser pre_parser(argc, argv);
|
||||||
std::string name;
|
if (pre_parser.has_flag("--test-all")) {
|
||||||
std::vector<std::string> aliases;
|
int readme = json_app({"--readme"});
|
||||||
|
|
||||||
std::vector<std::string> Matches() {
|
int help = json_app({"--help"});
|
||||||
auto copy = aliases;
|
|
||||||
copy.push_back(name);
|
|
||||||
|
|
||||||
}
|
int license = json_app({"--license"});
|
||||||
};
|
|
||||||
|
|
||||||
int readme = json_app({"--readme"});
|
int version = json_app({"--version"});
|
||||||
|
|
||||||
int help = json_app({"--help"});
|
int result_0 = json_app({"--file", "nonexistant.json"});
|
||||||
|
|
||||||
int license = json_app({"--license"});
|
int result_1 = json_app({"--validate", "{\"test\":42069}"});
|
||||||
|
|
||||||
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());
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user