mirror of
https://github.com/oxen-io/oxen-core.git
synced 2023-12-14 02:22:56 +01:00
Simplify arg printing; move <iostream> out of header
This commit is contained in:
parent
a91774f2fc
commit
cec9d5db1c
|
@ -34,6 +34,7 @@
|
|||
#ifdef HAVE_READLINE
|
||||
# include "epee/readline_buffer.h"
|
||||
#endif
|
||||
#include <iostream>
|
||||
|
||||
namespace command_line
|
||||
{
|
||||
|
@ -104,5 +105,23 @@ void clear_screen()
|
|||
#endif
|
||||
}
|
||||
|
||||
bool handle_error_helper(const boost::program_options::options_description& desc, std::function<bool()> parser) {
|
||||
try
|
||||
{
|
||||
return parser();
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
std::cerr << "Failed to parse arguments: " << e.what() << std::endl;
|
||||
std::cerr << desc << std::endl;
|
||||
return false;
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
std::cerr << "Failed to parse arguments: unknown exception" << std::endl;
|
||||
std::cerr << desc << std::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -34,13 +34,13 @@
|
|||
#include <sstream>
|
||||
#include <array>
|
||||
#include <type_traits>
|
||||
#include <iostream>
|
||||
|
||||
#include <boost/program_options/parsers.hpp>
|
||||
#include <boost/program_options/options_description.hpp>
|
||||
#include <boost/program_options/variables_map.hpp>
|
||||
#include "common/string_util.h"
|
||||
#include "common/i18n.h"
|
||||
#include "common/format.h"
|
||||
#include "logging/oxen_logger.h"
|
||||
|
||||
namespace command_line
|
||||
|
@ -140,24 +140,12 @@ namespace command_line
|
|||
|
||||
namespace {
|
||||
template <typename T>
|
||||
struct arg_stringify {
|
||||
const T& v;
|
||||
arg_stringify(const T& val) : v{val} {}
|
||||
};
|
||||
template <typename T>
|
||||
std::ostream& operator<<(std::ostream& o, const arg_stringify<T>& a) {
|
||||
return o << a.v;
|
||||
std::string arg_stringify(const T& a) {
|
||||
return "{}"_format(a);
|
||||
}
|
||||
template <typename T>
|
||||
std::ostream& operator<<(std::ostream& o, const arg_stringify<std::vector<T>>& a) {
|
||||
o << '{';
|
||||
bool first = true;
|
||||
for (auto& x : a.v) {
|
||||
if (first) first = false;
|
||||
else o << ",";
|
||||
o << x;
|
||||
}
|
||||
return o << '}';
|
||||
std::string arg_stringify(const std::vector<T>& v) {
|
||||
return "{{{}}}"_format(fmt::join(v, ","));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -166,11 +154,11 @@ namespace command_line
|
|||
{
|
||||
auto semantic = boost::program_options::value<T>();
|
||||
if (!arg.not_use_default) {
|
||||
std::ostringstream format;
|
||||
format << arg_stringify{arg.depf(false, true, arg.default_value)} << ", "
|
||||
<< arg_stringify{arg.depf(true, true, arg.default_value)} << " if '"
|
||||
<< arg.ref.name << "'";
|
||||
semantic->default_value(arg.depf(arg.ref.default_value, true, arg.default_value), format.str());
|
||||
auto default_display = "{}, {} if '{}'"_format(
|
||||
arg_stringify(arg.depf(false, true, arg.default_value)),
|
||||
arg_stringify(arg.depf(true, true, arg.default_value)),
|
||||
arg.ref.name);
|
||||
semantic->default_value(arg.depf(arg.ref.default_value, true, arg.default_value), default_display);
|
||||
}
|
||||
return semantic;
|
||||
}
|
||||
|
@ -182,17 +170,19 @@ namespace command_line
|
|||
if (!arg.not_use_default) {
|
||||
std::array<bool, NUM_DEPS> depval;
|
||||
depval.fill(false);
|
||||
std::ostringstream format;
|
||||
format << arg_stringify{arg.depf(depval, true, arg.default_value)};
|
||||
auto default_display = arg_stringify(arg.depf(depval, true, arg.default_value));
|
||||
for (size_t i = 0; i < depval.size(); ++i)
|
||||
{
|
||||
depval.fill(false);
|
||||
depval[i] = true;
|
||||
format << ", " << arg_stringify{arg.depf(depval, true, arg.default_value)} << " if '" << arg.ref[i]->name << "'";
|
||||
fmt::format_to(std::back_inserter(default_display),
|
||||
", {} if '{}'",
|
||||
arg_stringify(arg.depf(depval, true, arg.default_value)),
|
||||
arg.ref[i]->name);
|
||||
}
|
||||
for (size_t i = 0; i < depval.size(); ++i)
|
||||
depval[i] = arg.ref[i]->default_value;
|
||||
semantic->default_value(arg.depf(depval, true, arg.default_value), format.str());
|
||||
semantic->default_value(arg.depf(depval, true, arg.default_value), default_display);
|
||||
}
|
||||
return semantic;
|
||||
}
|
||||
|
@ -266,26 +256,7 @@ namespace command_line
|
|||
return parser.run();
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
bool handle_error_helper(const boost::program_options::options_description& desc, F parser)
|
||||
{
|
||||
try
|
||||
{
|
||||
return parser();
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
std::cerr << "Failed to parse arguments: " << e.what() << std::endl;
|
||||
std::cerr << desc << std::endl;
|
||||
return false;
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
std::cerr << "Failed to parse arguments: unknown exception" << std::endl;
|
||||
std::cerr << desc << std::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
bool handle_error_helper(const boost::program_options::options_description& desc, std::function<bool()> parser);
|
||||
|
||||
template<typename T, bool required, bool dependent, int NUM_DEPS>
|
||||
std::enable_if_t<!std::is_same_v<T, bool>, bool> has_arg(const boost::program_options::variables_map& vm, const arg_descriptor<T, required, dependent, NUM_DEPS>& arg)
|
||||
|
|
Loading…
Reference in a new issue