Update volk_gnsssdr_option_helpers

This commit is contained in:
Carles Fernandez 2023-12-02 13:31:29 +01:00
parent 15ddddf7c5
commit 3ad02f6dc1
No known key found for this signature in database
GPG Key ID: 4C583C52B0C3877D
3 changed files with 199 additions and 91 deletions

View File

@ -15,70 +15,118 @@
#include <utility> // for pair #include <utility> // for pair
/* option_t::option_t(std::string t_longform,
* Option type std::string t_shortform,
*/ std::string t_msg,
option_t::option_t(std::string longform, std::string shortform, std::string msg, void (*callback)()) void (*t_callback)())
: longform("--" + longform), : longform("--" + t_longform),
shortform("-" + shortform), shortform("-" + t_shortform),
msg(msg), msg(t_msg),
callback(callback) { option_type = VOID_CALLBACK; } callback(t_callback)
{
option_type = VOID_CALLBACK;
}
option_t::option_t(std::string longform, std::string shortform, std::string msg, void (*callback)(int))
: longform("--" + longform),
shortform("-" + shortform),
msg(msg),
callback((void (*)())callback) { option_type = INT_CALLBACK; }
option_t::option_t(std::string longform, std::string shortform, std::string msg, void (*callback)(float)) option_t::option_t(std::string t_longform,
: longform("--" + longform), std::string t_shortform,
shortform("-" + shortform), std::string t_msg,
msg(msg), void (*t_callback)(int))
callback((void (*)())callback) { option_type = FLOAT_CALLBACK; } : longform("--" + t_longform),
shortform("-" + t_shortform),
msg(t_msg),
callback((void (*)())t_callback)
{
option_type = INT_CALLBACK;
}
option_t::option_t(std::string longform, std::string shortform, std::string msg, void (*callback)(bool))
: longform("--" + longform),
shortform("-" + shortform),
msg(msg),
callback((void (*)())callback) { option_type = BOOL_CALLBACK; }
option_t::option_t(std::string longform, std::string shortform, std::string msg, void (*callback)(std::string)) option_t::option_t(std::string t_longform,
: longform("--" + longform), std::string t_shortform,
shortform("-" + shortform), std::string t_msg,
msg(msg), void (*t_callback)(float))
callback((void (*)())callback) { option_type = STRING_CALLBACK; } : longform("--" + t_longform),
shortform("-" + t_shortform),
msg(t_msg),
callback((void (*)())t_callback)
{
option_type = FLOAT_CALLBACK;
}
option_t::option_t(std::string longform, std::string shortform, std::string msg, std::string printval)
: longform("--" + longform), option_t::option_t(std::string t_longform,
shortform("-" + shortform), std::string t_shortform,
msg(msg), std::string t_msg,
printval(printval) { option_type = STRING; } void (*t_callback)(bool))
: longform("--" + t_longform),
shortform("-" + t_shortform),
msg(t_msg),
callback((void (*)())t_callback)
{
option_type = BOOL_CALLBACK;
}
option_t::option_t(std::string t_longform,
std::string t_shortform,
std::string t_msg,
void (*t_callback)(std::string))
: longform("--" + t_longform),
shortform("-" + t_shortform),
msg(t_msg),
callback((void (*)())t_callback)
{
option_type = STRING_CALLBACK;
}
option_t::option_t(std::string t_longform,
std::string t_shortform,
std::string t_msg,
std::string t_printval)
: longform("--" + t_longform),
shortform("-" + t_shortform),
msg(t_msg),
printval(t_printval)
{
option_type = STRING;
}
/* /*
* Option List * Option List
*/ */
option_list::option_list(std::string program_name) : program_name(program_name) option_list::option_list(std::string program_name) : d_program_name(program_name)
{ {
{ d_internal_list = std::vector<option_t>();
internal_list = std::vector<option_t>();
}
} }
void option_list::add(const option_t &opt) { internal_list.push_back(opt); }
void option_list::parse(int argc, char **argv) void option_list::add(const option_t& opt) { d_internal_list.push_back(opt); }
void option_list::parse(int argc, char** argv)
{ {
for (int arg_number = 0; arg_number < argc; ++arg_number) for (int arg_number = 0; arg_number < argc; ++arg_number)
{ {
for (std::vector<option_t>::iterator this_option = internal_list.begin(); for (std::vector<option_t>::iterator this_option = d_internal_list.begin();
this_option != internal_list.end(); this_option != d_internal_list.end();
this_option++) this_option++)
{ {
int int_val = INT_MIN;
if (this_option->longform == std::string(argv[arg_number]) || if (this_option->longform == std::string(argv[arg_number]) ||
this_option->shortform == std::string(argv[arg_number])) this_option->shortform == std::string(argv[arg_number]))
{ {
if (d_present_options.count(this_option->longform) == 0)
{
d_present_options.insert(
std::pair<std::string, int>(this_option->longform, 1));
}
else
{
d_present_options[this_option->longform] += 1;
}
switch (this_option->option_type) switch (this_option->option_type)
{ {
case VOID_CALLBACK: case VOID_CALLBACK:
@ -87,54 +135,94 @@ void option_list::parse(int argc, char **argv)
case INT_CALLBACK: case INT_CALLBACK:
try try
{ {
int int_val = std::stoi(argv[++arg_number]); int_val = atoi(argv[++arg_number]);
((void (*)(int))this_option->callback)(int_val); ((void (*)(int))this_option->callback)(int_val);
} }
catch (std::exception &exc) catch (std::exception& exc)
{ {
std::cout << "An int option can only receive a number\n"; std::cout << "An int option can only receive a number"
<< std::endl;
throw std::exception(); throw std::exception();
}; };
break; break;
case FLOAT_CALLBACK: case FLOAT_CALLBACK:
try try
{ {
int int_val = std::stof(argv[++arg_number]); double double_val = atof(argv[++arg_number]);
((void (*)(float))this_option->callback)(int_val); ((void (*)(float))this_option->callback)(double_val);
} }
catch (std::exception &exc) catch (std::exception& exc)
{ {
std::cout << "A float option can only receive a number\n"; std::cout << "A float option can only receive a number"
<< std::endl;
throw std::exception(); throw std::exception();
}; };
break; break;
case BOOL_CALLBACK: case BOOL_CALLBACK:
try try
{ {
bool int_val = (bool)std::stoi(argv[++arg_number]); if (arg_number == (argc - 1))
{ // this is the last arg
int_val = 1;
}
else
{ // sneak a look at the next arg since it's present
char* next_arg = argv[arg_number + 1];
if ((strncmp(next_arg, "-", 1) == 0) ||
(strncmp(next_arg, "--", 2) == 0))
{
// the next arg is actually an arg, the bool is just
// present, set to true
int_val = 1;
}
else if (strncmp(next_arg, "true", 4) == 0)
{
int_val = 1;
}
else if (strncmp(next_arg, "false", 5) == 0)
{
int_val = 0;
}
else
{
// we got a number or a string.
// convert it to a number and depend on the catch to
// report an error condition
int_val = (bool)atoi(argv[++arg_number]);
}
}
}
catch (std::exception& e)
{
int_val = INT_MIN;
};
if (int_val == INT_MIN)
{
std::cout
<< "option: '" << argv[arg_number - 1]
<< "' -> received an unknown value. Boolean "
"options should receive one of '0', '1', 'true', 'false'."
<< std::endl;
throw std::exception();
}
else if (int_val)
{
((void (*)(bool))this_option->callback)(int_val); ((void (*)(bool))this_option->callback)(int_val);
} }
catch (std::exception &exc)
{
std::cout << "A bool option can only receive 0 or 1\n";
throw std::exception();
};
break; break;
case STRING_CALLBACK: case STRING_CALLBACK:
try try
{ {
((void (*)(std::string))this_option->callback)(argv[++arg_number]); ((void (*)(std::string))this_option->callback)(
argv[++arg_number]);
} }
catch (std::exception &exc) catch (std::exception& exc)
{ {
throw std::exception(); throw std::exception();
}; };
break; break;
case STRING: case STRING:
std::cout << this_option->printval << '\n'; std::cout << this_option->printval << std::endl;
break;
default:
this_option->callback();
break; break;
} }
} }
@ -142,17 +230,31 @@ void option_list::parse(int argc, char **argv)
if (std::string("--help") == std::string(argv[arg_number]) || if (std::string("--help") == std::string(argv[arg_number]) ||
std::string("-h") == std::string(argv[arg_number])) std::string("-h") == std::string(argv[arg_number]))
{ {
d_present_options.insert(std::pair<std::string, int>("--help", 1));
help(); help();
} }
} }
} }
bool option_list::present(std::string option_name)
{
if (d_present_options.count("--" + option_name))
{
return true;
}
else
{
return false;
}
}
void option_list::help() void option_list::help()
{ {
std::cout << program_name << '\n'; std::cout << d_program_name << std::endl;
std::cout << " -h [ --help ] \t\tDisplay this help message\n"; std::cout << " -h [ --help ] \t\tdisplay this help message" << std::endl;
for (std::vector<option_t>::iterator this_option = internal_list.begin(); for (std::vector<option_t>::iterator this_option = d_internal_list.begin();
this_option != internal_list.end(); this_option != d_internal_list.end();
this_option++) this_option++)
{ {
std::string help_line(" "); std::string help_line(" ");
@ -165,24 +267,11 @@ void option_list::help()
help_line += this_option->shortform + " [ " + this_option->longform + " ]"; help_line += this_option->shortform + " [ " + this_option->longform + " ]";
} }
switch (help_line.size() / 8) while (help_line.size() < 32)
{ {
case 0: help_line += " ";
help_line += "\t\t\t\t";
break;
case 1:
help_line += "\t\t\t";
break;
case 2:
help_line += "\t\t";
break;
case 3:
help_line += "\t";
break;
default:
break;
} }
help_line += this_option->msg; help_line += this_option->msg;
std::cout << help_line << '\n'; std::cout << help_line << std::endl;
} }
} }

View File

@ -29,12 +29,30 @@ typedef enum
class option_t class option_t
{ {
public: public:
option_t(std::string longform, std::string shortform, std::string msg, void (*callback)()); option_t(std::string t_longform,
option_t(std::string longform, std::string shortform, std::string msg, void (*callback)(int)); std::string t_shortform,
option_t(std::string longform, std::string shortform, std::string msg, void (*callback)(float)); std::string t_msg,
option_t(std::string longform, std::string shortform, std::string msg, void (*callback)(bool)); void (*t_callback)());
option_t(std::string longform, std::string shortform, std::string msg, void (*callback)(std::string)); option_t(std::string t_longform,
option_t(std::string longform, std::string shortform, std::string msg, std::string printval); std::string t_shortform,
std::string t_msg,
void (*t_callback)(int));
option_t(std::string t_longform,
std::string t_shortform,
std::string t_msg,
void (*t_callback)(float));
option_t(std::string t_longform,
std::string t_shortform,
std::string t_msg,
void (*t_callback)(bool));
option_t(std::string t_longform,
std::string t_shortform,
std::string t_msg,
void (*t_callback)(std::string));
option_t(std::string t_longform,
std::string t_shortform,
std::string t_msg,
std::string t_printval);
std::string longform; std::string longform;
std::string shortform; std::string shortform;
@ -48,6 +66,7 @@ class option_list
{ {
public: public:
explicit option_list(std::string program_name); explicit option_list(std::string program_name);
bool present(std::string option_name);
void add(const option_t &opt); void add(const option_t &opt);
@ -56,8 +75,9 @@ public:
void help(); void help();
private: private:
std::string program_name; std::string d_program_name;
std::vector<option_t> internal_list; std::vector<option_t> d_internal_list;
std::map<std::string, int> d_present_options;
}; };

View File

@ -81,8 +81,7 @@ int main(int argc, char *argv[])
for (int arg_number = 0; arg_number < argc; ++arg_number) for (int arg_number = 0; arg_number < argc; ++arg_number)
{ {
if (std::string("--help") == std::string(argv[arg_number]) || if (profile_options.present("help"))
std::string("-h") == std::string(argv[arg_number]))
{ {
return 0; return 0;
} }