diff --git a/examples/cli/main.cpp b/examples/cli/main.cpp index dc5013b3..decee0a9 100644 --- a/examples/cli/main.cpp +++ b/examples/cli/main.cpp @@ -169,8 +169,9 @@ struct SDCliParams { return 1; }; - auto on_help_arg = [&](int argc, const char** argv, int index) { + auto on_help_arg = [&](int argc, const char** argv, int index, bool& valid) { normal_exit = true; + valid = true; return -1; }; diff --git a/examples/common/common.cpp b/examples/common/common.cpp index 0ecc72dc..6f397aa3 100644 --- a/examples/common/common.cpp +++ b/examples/common/common.cpp @@ -245,6 +245,7 @@ bool parse_options(int argc, const char** argv, const std::vector& o return false; }; + bool valid = false; for (int i = 1; i < argc; i++) { arg = argv[i]; bool found_arg = false; @@ -287,7 +288,7 @@ bool parse_options(int argc, const char** argv, const std::vector& o break; if (match_and_apply(options.manual_options, [&](auto& option) { - int ret = option.cb(argc, argv, i); + int ret = option.cb(argc, argv, i, valid); if (ret < 0) { invalid_arg = true; return; @@ -299,7 +300,9 @@ bool parse_options(int argc, const char** argv, const std::vector& o } if (invalid_arg) { - LOG_ERROR("error: invalid parameter for argument: %s", arg.c_str()); + if (!valid) { + LOG_ERROR("error: invalid parameter for argument: %s", arg.c_str()); + } return false; } if (!found_arg) { diff --git a/examples/common/common.h b/examples/common/common.h index cd02d212..3a6653bb 100644 --- a/examples/common/common.h +++ b/examples/common/common.h @@ -56,11 +56,42 @@ struct BoolOption { bool* target; }; +struct ManualFunction { + std::function _func; + + ManualFunction() = default; + + ManualFunction(std::function func) + : _func(std::move(func)) { + } + + template + ManualFunction(F func) + : _func(make_function(func)) { + } + + int operator()(int argc, const char** argv, int index, bool& valid) const { + return _func(argc, argv, index, valid); + } + +private: + template + static std::function make_function(F func) { + if constexpr (std::is_invocable_v) { + return func; + } else { + return [func](int argc, const char** argv, int index, bool&) { + return func(argc, argv, index); + }; + } + } +}; + struct ManualOption { std::string short_name; std::string long_name; std::string desc; - std::function cb; + ManualFunction cb; }; struct ArgOptions { diff --git a/examples/server/runtime.cpp b/examples/server/runtime.cpp index 1127ab73..79fa757e 100644 --- a/examples/server/runtime.cpp +++ b/examples/server/runtime.cpp @@ -203,8 +203,9 @@ ArgOptions SDSvrParams::get_options() { {"", "--color", "colors the logging tags according to level", true, &color}, }; - auto on_help_arg = [&](int, const char**, int) { + auto on_help_arg = [&](int, const char**, int, bool& valid) { normal_exit = true; + valid = true; return -1; };