Skip to content

Commit b219685

Browse files
committed
fix: Suppress spurious error output for --help (#1607)
Signed-off-by: kei-g <km.8k6ce+github@gmail.com>
1 parent 7948df8 commit b219685

4 files changed

Lines changed: 41 additions & 5 deletions

File tree

examples/cli/main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,9 @@ struct SDCliParams {
169169
return 1;
170170
};
171171

172-
auto on_help_arg = [&](int argc, const char** argv, int index) {
172+
auto on_help_arg = [&](int argc, const char** argv, int index, bool& valid) {
173173
normal_exit = true;
174+
valid = true;
174175
return -1;
175176
};
176177

examples/common/common.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ bool parse_options(int argc, const char** argv, const std::vector<ArgOptions>& o
245245
return false;
246246
};
247247

248+
bool valid = false;
248249
for (int i = 1; i < argc; i++) {
249250
arg = argv[i];
250251
bool found_arg = false;
@@ -287,7 +288,7 @@ bool parse_options(int argc, const char** argv, const std::vector<ArgOptions>& o
287288
break;
288289

289290
if (match_and_apply(options.manual_options, [&](auto& option) {
290-
int ret = option.cb(argc, argv, i);
291+
int ret = option.cb(argc, argv, i, valid);
291292
if (ret < 0) {
292293
invalid_arg = true;
293294
return;
@@ -299,7 +300,9 @@ bool parse_options(int argc, const char** argv, const std::vector<ArgOptions>& o
299300
}
300301

301302
if (invalid_arg) {
302-
LOG_ERROR("error: invalid parameter for argument: %s", arg.c_str());
303+
if (!valid) {
304+
LOG_ERROR("error: invalid parameter for argument: %s", arg.c_str());
305+
}
303306
return false;
304307
}
305308
if (!found_arg) {

examples/common/common.h

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,42 @@ struct BoolOption {
5656
bool* target;
5757
};
5858

59+
struct ManualFunction {
60+
std::function<int(int, const char**, int, bool&)> _func;
61+
62+
ManualFunction() = default;
63+
64+
ManualFunction(std::function<int(int argc, const char** argv, int index, bool& valid)> func)
65+
: _func(std::move(func)) {
66+
}
67+
68+
template <typename F>
69+
ManualFunction(F func)
70+
: _func(make_function(func)) {
71+
}
72+
73+
int operator()(int argc, const char** argv, int index, bool& valid) const {
74+
return _func(argc, argv, index, valid);
75+
}
76+
77+
private:
78+
template <typename F>
79+
static std::function<int(int, const char**, int, bool&)> make_function(F func) {
80+
if constexpr (std::is_invocable_v<F, int, const char**, int, bool&>) {
81+
return func;
82+
} else {
83+
return [func](int argc, const char** argv, int index, bool&) {
84+
return func(argc, argv, index);
85+
};
86+
}
87+
}
88+
};
89+
5990
struct ManualOption {
6091
std::string short_name;
6192
std::string long_name;
6293
std::string desc;
63-
std::function<int(int argc, const char** argv, int index)> cb;
94+
ManualFunction cb;
6495
};
6596

6697
struct ArgOptions {

examples/server/runtime.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,9 @@ ArgOptions SDSvrParams::get_options() {
203203
{"", "--color", "colors the logging tags according to level", true, &color},
204204
};
205205

206-
auto on_help_arg = [&](int, const char**, int) {
206+
auto on_help_arg = [&](int, const char**, int, bool& valid) {
207207
normal_exit = true;
208+
valid = true;
208209
return -1;
209210
};
210211

0 commit comments

Comments
 (0)