Change command-line parsing (again...)

- Added formatted table with `--style` option
- Added `warning` log level
- Fixed bugs and cleaned up API
- Removed some extra debugging output
This commit is contained in:
David Allen 2023-07-01 21:32:24 -06:00
parent 0238dfd510
commit b36d55ceee
15 changed files with 685 additions and 255 deletions

View file

@ -17,6 +17,11 @@ namespace gdpm::package{
}
namespace gdpm::config{
enum class print_style{
list = 0,
table = 1,
};
struct context{
string username;
string password;
@ -25,24 +30,28 @@ namespace gdpm::config{
string packages_dir;
string tmp_dir;
string_map remote_sources;
size_t jobs = 1;
size_t timeout = 3000;
size_t max_results = 200;
int jobs = 1;
int timeout = 3000;
bool enable_sync = true;
bool enable_cache = true;
bool skip_prompt = false;
bool enable_file_logging;
bool clean_temporary;
int verbose;
int verbose = log::INFO;
print_style style = print_style::list;
package::info info;
rest_api::request_params api_params;
rest_api::request_params rest_api_params;
};
string to_json(const context& config, bool pretty_print = false);
error load(std::filesystem::path path, context& config);
error save(std::filesystem::path path, const context& config);
error handle_config(config::context& config, const args_t& args, const var_opts& opts);
context make_context(const string& username = GDPM_CONFIG_USERNAME, const string& password = GDPM_CONFIG_PASSWORD, const string& path = GDPM_CONFIG_PATH, const string& token = GDPM_CONFIG_TOKEN, const string& godot_version = GDPM_CONFIG_GODOT_VERSION, const string& packages_dir = GDPM_CONFIG_LOCAL_PACKAGES_DIR, const string& tmp_dir = GDPM_CONFIG_LOCAL_TMP_DIR, const string_map& remote_sources = {GDPM_CONFIG_REMOTE_SOURCES}, size_t threads = GDPM_CONFIG_THREADS, size_t timeout = 0, bool enable_sync = GDPM_CONFIG_ENABLE_SYNC, bool enable_file_logging = GDPM_CONFIG_ENABLE_FILE_LOGGING, int verbose = GDPM_CONFIG_VERBOSE);
error set_property(config::context& config, const string& property, const any& value);
template <typename T = any>
T& get_property(const config::context& config, const string& property);
context make_context(const string& username = GDPM_CONFIG_USERNAME, const string& password = GDPM_CONFIG_PASSWORD, const string& path = GDPM_CONFIG_PATH, const string& token = GDPM_CONFIG_TOKEN, const string& godot_version = GDPM_CONFIG_GODOT_VERSION, const string& packages_dir = GDPM_CONFIG_LOCAL_PACKAGES_DIR, const string& tmp_dir = GDPM_CONFIG_LOCAL_TMP_DIR, const string_map& remote_sources = {GDPM_CONFIG_REMOTE_SOURCES}, int jobs = GDPM_CONFIG_THREADS, int timeout = 0, bool enable_sync = GDPM_CONFIG_ENABLE_SYNC, bool enable_file_logging = GDPM_CONFIG_ENABLE_FILE_LOGGING, int verbose = GDPM_CONFIG_VERBOSE);
error validate(const rapidjson::Document& doc);
void print_json(const context& config);
void print_properties(const context& config, const string_list& properties);

View file

@ -13,6 +13,8 @@ namespace gdpm::constants::error{
NONE = 0,
UNKNOWN,
UNKNOWN_COMMAND,
UNKNOWN_ARGUMENT,
ARGPARSE_ERROR,
NOT_FOUND,
NOT_DEFINED,
NOT_IMPLEMENTED,
@ -102,6 +104,11 @@ namespace gdpm{
#endif
}
static constexpr gdpm::error error_rc(const gdpm::error& e){
error(e);
return e;
}
static void error(const char *p, const gdpm::error& e){
println("{}{}{}", p, prefix.contents, e.get_message());
}

View file

@ -77,6 +77,7 @@ namespace gdpm::log
inline constexpr const char* get_info_prefix() { return "[INFO {}] "; }
inline constexpr const char* get_error_prefix() { return "[ERROR {}] "; }
inline constexpr const char* get_debug_prefix() { return "[DEBUG {}] "; }
inline constexpr const char* get_warning_prefix() { return "[WARN {}] "; }
static void vlog(fmt::string_view format, fmt::format_args args){
fmt::vprint(format, args);
@ -142,6 +143,21 @@ namespace gdpm::log
#endif
}
template <typename S, typename...Args>
static constexpr void warn(const S& format, Args&&...args){
if(log::level < to_int(log::WARNING))
return;
#if GDPM_LOG_LEVEL > WARN
set_prefix_if(std::format(get_warning_prefix(), utils::timestamp()), true);
set_suffix_if("\n");
vlog(
fmt::format(GDPM_COLOR_LOG_WARNING "{}{}{}" GDPM_COLOR_LOG_RESET, prefix.contents, format, suffix),
fmt::make_format_args(args...)
);
#endif
}
template <typename S, typename...Args>
static constexpr void print(const S& format, Args&&...args){
vlog(

View file

@ -116,6 +116,7 @@ namespace gdpm::package {
GDPM_DLL_EXPORT void print_list(const rapidjson::Document& json);
GDPM_DLL_EXPORT void print_list(const info_list& packages);
GDPM_DLL_EXPORT void print_table(const info_list& packages);
GDPM_DLL_EXPORT result_t<info_list> get_package_info(const opts_t& opts);
GDPM_DLL_EXPORT result_t<title_list> get_package_titles(const info_list& packages);
GDPM_DLL_EXPORT void clean_temporary(const config::context& config, const title_list& package_titles);

View file

@ -1,11 +1,13 @@
#pragma once
#include "clipp.h"
#include <tuple>
#include <functional>
#include <type_traits>
#include <string>
#include <variant>
#include <future>
#include <any>
namespace gdpm{
class error;
@ -47,6 +49,7 @@ namespace gdpm{
using string_list = std::vector<string>;
using string_map = std::unordered_map<string, string>;
using string_pair = std::pair<string, string>;
using any = std::any;
using var = std::variant<int, float, bool, string, string_list, string_map, size_t>;
template <typename T = var>
using _args_t = std::vector<T>;