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(camera, nbufs, 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, low_res_factor, float),
DEFINE_OPTION(http, port, uint),
DEFINE_OPTION(http, maxcons, uint),
DEFINE_OPTION(log, debug, bool),
DEFINE_OPTION(log, verbose, bool),
DEFINE_OPTION_DEFAULT(log, debug, bool, "1"),
DEFINE_OPTION_DEFAULT(log, verbose, bool, "1"),
{}
};

View File

@ -2,4 +2,4 @@
set -xeo pipefail
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;
const char *value = strchr(key, '=');
for (int i = 0; options[i].name; i++) {
if (value) {
if (!strncmp(key, options[i].name, value - key)) {
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) {
}
if (!option || !value) {
return -EINVAL;
}
@ -68,22 +78,23 @@ int parse_opts(option_t *options, int argc, char *argv[])
for (arg = 1; arg < argc; arg += 2) {
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);
return -1;
}
if (arg+1 == argc) {
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]);
int ret = parse_opt(options, key);
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;
option_value_t *value_mapping;
unsigned size;
const char *default_value;
} option_t;
#define lambda(return_type, function_body) \
@ -43,6 +44,15 @@ typedef struct options_s {
.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) \
{ \
.name = #_section "-" #_name, \

View File

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