You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

76 lines
1.9 KiB

#include <getopt.h>
#include <iostream>
#include <fmt/format.h>
#include "cmd_line_args.h"
static struct option options[] =
{
{"help", no_argument, NULL, '?'},
{"version", no_argument, NULL, 'v'},
{"config-file", optional_argument, NULL, 'c'},
{"initial-pin-state", optional_argument, NULL, 's'},
{NULL, 0, NULL, 0 }
};
static constexpr auto optc = "?v:c:";
static constexpr auto help_format =
"{0} v{1}\n\n"
"Usage:\n"
"Availabe options are:\n"
"\t--help Show help\n"
"\t--config-file Config file path\n"
"\t--initial-pin-state Initial state of control pin\n"
"\t--version Version of the {0}\n"
"\n"
"Usage example:\n"
"{0} --config-file=scheduler.conf\n"
"\n\n";
static constexpr auto version_format =
"{0} v{1}\n\n"
"Build with gcc-{2}";
void usage() {
std::cout << fmt::format(help_format, APP_NAME, APP_VERSION) << std::endl;
exit(0);
}
void version() {
std::cout << fmt::format(version_format, APP_NAME, APP_VERSION, APP_GCC) << std::endl;
exit(0);
}
app_options parse_opts(int argc, char** argv) {
if(argc < 1){
exit(1);
}
std::string config_file {""};
int initial_pin_state = 0;
for(;;) {
int opt_idx, c;
if((c = getopt_long(argc, argv, optc, options, &opt_idx)) == -1) break;
switch(c) {
case '?':
usage();
break;
case 'v':
version();
break;
case 'c':
config_file = optarg;
break;
case 's':
initial_pin_state = std::stoi(optarg);
break;
default:
exit(1);
}
}
return {config_file, initial_pin_state};
}