Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Draft] Basic cli #268

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,12 @@ if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.24)
GIT_TAG ${JSON_FETCH_VERSION}
FIND_PACKAGE_ARGS
)
list(APPEND fetch_packages nlohmann_json)
FetchContent_Declare(CLI11
GIT_REPOSITORY https://github.com/CLIUtils/CLI11
GIT_TAG v2.3.2
FIND_PACKAGE_ARGS
)
list(APPEND fetch_packages nlohmann_json CLI11)
else ()
# Try to get system installed version
find_package(nlohmann_json QUIET)
Expand All @@ -100,6 +105,15 @@ else ()
)
list(APPEND fetch_packages nlohmann_json)
endif ()
find_package(CLI11 QUIET)
if (NOT CLI11_FOUND)
# If failed fetch the desired version
FetchContent_Declare(CLI11
GIT_REPOSITORY https://github.com/CLIUtils/CLI11
GIT_TAG v2.3.2
)
list(APPEND fetch_packages nlohmann_json CLI11)
endif ()
endif ()

# Handle configure flags
Expand Down Expand Up @@ -140,6 +154,12 @@ set_target_properties(nlohmann_json_schema_validator PROPERTIES
OUTPUT_NAME nlohmann_json_validator
)

add_executable(nlohmann_json_schema_validator_cli)
set_target_properties(nlohmann_json_schema_validator_cli PROPERTIES
EXPORT_NAME cli
OUTPUT_NAME json-validator
)

# Main definitions in here
add_subdirectory(src)

Expand Down
10 changes: 8 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ target_sources(nlohmann_json_schema_validator PRIVATE
json-patch.cpp
string-format-check.cpp
)
target_sources(nlohmann_json_schema_validator_cli PRIVATE
cli.cpp)
configure_file(nlohmann/json-schema.hpp.in nlohmann/json-schema.hpp)

target_include_directories(nlohmann_json_schema_validator PUBLIC
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
)

set_target_properties(nlohmann_json_schema_validator PROPERTIES
PUBLIC_HEADER nlohmann/json-schema.hpp)
PUBLIC_HEADER ${CMAKE_CURRENT_BINARY_DIR}/nlohmann/json-schema.hpp)
target_link_libraries(nlohmann_json_schema_validator_cli PRIVATE
nlohmann_json_schema_validator CLI11::CLI11)

# TODO: Why would this need to be if guarded?
if (JSON_VALIDATOR_SHARED_LIBS)
Expand Down
67 changes: 67 additions & 0 deletions src/cli.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <CLI/App.hpp>
#include <CLI/Config.hpp>
#include <CLI/Formatter.hpp>
#include <fstream>

#include "nlohmann/json-schema.hpp"

using namespace nlohmann;
using namespace nlohmann::json_schema;

class main_cli : public CLI::App
{
std::string version;
std::ifstream schema_input;
std::filesystem::path object_path;
// TODO: Export this as a built-in loader
void loader(const json_uri &uri, json &sch)
{
std::string filename = object_path.parent_path().append(uri.path());
std::ifstream lf(filename);
if (!lf.good())
throw std::invalid_argument("could not open " + uri.url() + " tried with " + filename);
try {
lf >> sch;
} catch (const std::exception &e) {
throw e;
}
}

public:
json schema;
json object;
json_validator validator;
main_cli()
: CLI::App{"Json schema validator", "json-validator"},
validator{
[this](const json_uri &u, json &s) { this->loader(u, s); },
default_string_format_check},
version{nlohmann::json_schema::version}
{
set_version_flag("--version", version);
add_option("schema", schema_input, "JSON schema of the object")
->check(CLI::ExistingFile);
add_option("object", object_path, "JSON object to validate")
->check(CLI::ExistingFile);
}
void validate()
{
validator.set_root_schema(schema);
validator.validate(object);
}
};

int main(int argc, char *argv[])
{
main_cli app{};

try {
app.parse(argc, argv);
} catch (const CLI::ParseError &e) {
return app.exit(e);
}

app.validate();

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ class JSON_SCHEMA_VALIDATOR_API json_uri
namespace json_schema
{

constexpr std::string_view version = "@PROJECT_VERSION@";

extern json draft7_schema_builtin;

typedef std::function<void(const json_uri & /*id*/, json & /*value*/)> schema_loader;
Expand Down