Add options in format -opt=value

This commit is contained in:
Kamil Trzcinski 2022-04-06 09:22:35 +02:00
parent 4e3c235b36
commit 23178325cf
5 changed files with 43 additions and 22 deletions

View File

@ -59,13 +59,13 @@ option_t all_options[] = {
DEFINE_OPTION_VALUES(camera, format, camera_formats), DEFINE_OPTION_VALUES(camera, format, camera_formats),
DEFINE_OPTION(camera, nbufs, uint), DEFINE_OPTION(camera, nbufs, uint),
DEFINE_OPTION(camera, fps, uint), DEFINE_OPTION(camera, fps, uint),
DEFINE_OPTION(camera, allow_dma, bool), DEFINE_OPTION_DEFAULT(camera, allow_dma, bool, "1"),
DEFINE_OPTION(camera, high_res_factor, float), DEFINE_OPTION(camera, high_res_factor, float),
DEFINE_OPTION(camera, low_res_factor, float), DEFINE_OPTION(camera, low_res_factor, float),
DEFINE_OPTION(http, port, uint), DEFINE_OPTION(http, port, uint),
DEFINE_OPTION(http, maxcons, uint), DEFINE_OPTION(http, maxcons, uint),
DEFINE_OPTION(log, debug, bool), DEFINE_OPTION_DEFAULT(log, debug, bool, "1"),
DEFINE_OPTION(log, verbose, bool), DEFINE_OPTION_DEFAULT(log, verbose, bool, "1"),
{} {}
}; };

View File

@ -2,4 +2,4 @@
set -xeo pipefail set -xeo pipefail
make make
./camera_stream -camera-path /dev/v4l/by-path/*.csi-video-index0 "$@" ./camera_stream -camera-path=$(echo /dev/v4l/by-path/*.csi-video-index0) "$@"

View File

@ -29,18 +29,28 @@ static void print_help(option_t *options)
} }
} }
static int parse_opt(option_t *options, const char *key, const char *value) static int parse_opt(option_t *options, const char *key)
{ {
option_t *option = NULL; option_t *option = NULL;
const char *value = strchr(key, '=');
for (int i = 0; options[i].name; i++) { for (int i = 0; options[i].name; i++) {
if (!strcmp(key, options[i].name)) { if (value) {
option = &options[i]; if (!strncmp(key, options[i].name, value - key)) {
break; option = &options[i];
value++; // ignore '='
break;
}
} else {
// require exact match
if (!strcmp(key, options[i].name)) {
option = &options[i];
value = option->default_value;
break;
}
} }
} }
if (!option || !value) {
if (!option) {
return -EINVAL; return -EINVAL;
} }
@ -68,22 +78,23 @@ int parse_opts(option_t *options, int argc, char *argv[])
for (arg = 1; arg < argc; arg += 2) { for (arg = 1; arg < argc; arg += 2) {
const char *key = argv[arg]; const char *key = argv[arg];
if (!strcmp(key, "-help")) {
if (key[0] == '-') {
key++;
if (key[0] == '-')
key++;
} else {
E_LOG_ERROR(NULL, "The '%s' is not option (should start with - or --).", key);
}
if (!strcmp(key, "help")) {
print_help(options); print_help(options);
return -1; return -1;
} }
if (arg+1 == argc) { int ret = parse_opt(options, key);
E_LOG_ERROR(NULL, "The %s is missing argument.", key);
}
if (key[0] != '-') {
E_LOG_ERROR(NULL, "The '%s' is not option (should start with -).", key);
}
int ret = parse_opt(options, key+1, argv[arg+1]);
if (ret <= 0) { if (ret <= 0) {
E_LOG_ERROR(NULL, "Parsing '%s %s' returned '%d'.", key, argv[arg+1], ret); E_LOG_ERROR(NULL, "Parsing '%s' returned '%d'.", argv[arg], ret);
} }
} }

View File

@ -21,6 +21,7 @@ typedef struct options_s {
const char *help; const char *help;
option_value_t *value_mapping; option_value_t *value_mapping;
unsigned size; unsigned size;
const char *default_value;
} option_t; } option_t;
#define lambda(return_type, function_body) \ #define lambda(return_type, function_body) \
@ -43,6 +44,15 @@ typedef struct options_s {
.size = sizeof(_section##_options._name), \ .size = sizeof(_section##_options._name), \
} }
#define DEFINE_OPTION_DEFAULT(_section, _name, _type, _default_value) \
{ \
.name = #_section "-" #_name, \
.value_##_type = &_section##_options._name, \
.format = OPTION_FORMAT_##_type, \
.size = sizeof(_section##_options._name), \
.default_value = _default_value, \
}
#define DEFINE_OPTION_VALUES(_section, _name, _value_mapping) \ #define DEFINE_OPTION_VALUES(_section, _name, _value_mapping) \
{ \ { \
.name = #_section "-" #_name, \ .name = #_section "-" #_name, \

View File

@ -2,4 +2,4 @@
set -xeo pipefail set -xeo pipefail
make make
./camera_stream -camera-path /dev/v4l/by-id/usb-*-video-index0 "$@" ./camera_stream -camera-path=$(echo /dev/v4l/by-id/usb-*-video-index0 ) "$@"