Integrate ArgsParser as separate package. Setup json_cli app testbed.
This commit is contained in:
@@ -11,7 +11,7 @@ set(CMAKE_CXX_STANDARD 20)
|
||||
|
||||
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
|
||||
|
||||
#include(cmake/CPM.cmake)
|
||||
include(cmake/CPM.cmake)
|
||||
|
||||
file(GLOB_RECURSE HEADERS "include/*.hpp")
|
||||
|
||||
@@ -30,4 +30,19 @@ endif()
|
||||
|
||||
add_executable(json_cli main.cpp)
|
||||
|
||||
target_link_libraries(json_cli PUBLIC json)
|
||||
CPMAddPackage(
|
||||
NAME ArgsParser
|
||||
URL https://git.redacted.cc/josh/ArgsParser/archive/Prerelease-1.zip
|
||||
)
|
||||
|
||||
CPMAddPackage(
|
||||
NAME mcolor
|
||||
URL https://git.redacted.cc/maxine/mcolor/archive/Release-1.zip
|
||||
)
|
||||
|
||||
target_include_directories(json_cli PUBLIC
|
||||
${ArgsParser_SOURCE_DIR}/include
|
||||
${mcolor_SOURCE_DIR}/include
|
||||
)
|
||||
|
||||
target_link_libraries(json_cli PUBLIC json ArgsParser mcolor)
|
182
main.cpp
182
main.cpp
@@ -7,6 +7,10 @@
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#include <Colors.hpp>
|
||||
#include <ArgsParser.hpp>
|
||||
|
||||
#include "AnsiEscapeCodes.hpp"
|
||||
|
||||
/// Open a text file and return the contents.
|
||||
std::string read_file(const std::string& file_path)
|
||||
@@ -38,88 +42,6 @@ void parse_json_file(const std::string& file_path) {
|
||||
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;
|
||||
@@ -146,62 +68,59 @@ 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 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);
|
||||
int json_app(std::vector<std::string> params) {
|
||||
bool color_output = true;
|
||||
std::string input_content;
|
||||
bool read_from_stdin = true;
|
||||
|
||||
}
|
||||
};
|
||||
ArgsParser args(params);
|
||||
|
||||
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);
|
||||
//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"})) {
|
||||
if (args.has_flag({"--about", "--info", "--readme"})) {
|
||||
|
||||
show_readme(); return 0;
|
||||
}
|
||||
show_readme(); return 0;
|
||||
}
|
||||
|
||||
if (args.has_flag({"--help", "-h", "--h"})) {
|
||||
show_help(); 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({"--license", "-L"})) {
|
||||
show_license(); return 0;
|
||||
}
|
||||
|
||||
if (args.has_flag("--raw"))
|
||||
color_output = false;
|
||||
if (args.has_flag("--raw"))
|
||||
color_output = false;
|
||||
|
||||
if (args.has_flag_arg("--file"))
|
||||
{
|
||||
if (args.has_flag_arg("--file"))
|
||||
{
|
||||
|
||||
auto [success, file_name] = args.consume_flag_arg("--file");
|
||||
auto file_name = args.consume_flag_arg("--file").value();
|
||||
|
||||
input_content = read_file(file_name);
|
||||
read_from_stdin = false;
|
||||
}
|
||||
read_from_stdin = false;
|
||||
}
|
||||
|
||||
if (read_from_stdin)
|
||||
{
|
||||
@@ -216,7 +135,7 @@ int main(int argc, char* argv[]) {
|
||||
validate_json_string(input_content); return 0;
|
||||
}
|
||||
|
||||
std::vector<std::string> remaining = args.args;
|
||||
std::vector<std::string> remaining = args.get_remaining_args();
|
||||
|
||||
for (auto& e : remaining) {
|
||||
std::cout << e << " ";
|
||||
@@ -225,18 +144,39 @@ int main(int argc, char* argv[]) {
|
||||
|
||||
auto[val, err] = json::parse(input_content);
|
||||
|
||||
if (!err.empty())
|
||||
{
|
||||
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[]) {
|
||||
struct flag {
|
||||
std::string name;
|
||||
std::vector<std::string> aliases;
|
||||
|
||||
std::vector<std::string> Matches() {
|
||||
auto copy = aliases;
|
||||
copy.push_back(name);
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
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}"});
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user