summaryrefslogtreecommitdiff
path: root/options.c
diff options
context:
space:
mode:
Diffstat (limited to 'options.c')
-rw-r--r--options.c157
1 files changed, 62 insertions, 95 deletions
diff --git a/options.c b/options.c
index 388402a..bd9f82b 100644
--- a/options.c
+++ b/options.c
@@ -21,108 +21,75 @@
* Email: <nico at ni.fr.eu.org>
*/
#define _GNU_SOURCE
-#include <errno.h>
-#include <getopt.h>
#include <printf.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
+#include "cli.h"
#include "options.h"
+#include "utils.h"
-/* Report command line usage, optionally prefixed with an error message. */
-static void
-usage(int status, const char *msg)
+void
+options_add(GApplication *app)
{
- if (msg)
- fprintf(stderr, "%s: %s\n", program_invocation_short_name, msg);
- fprintf(status == EXIT_SUCCESS ? stdout : stderr,
- "usage: %s [options]\n"
- "\n"
- "Microscope camera viewer.\n"
- "\n"
- "optional arguments:\n"
- " -h, --help show this help message and exit\n"
- " -w, --width VALUE image width\n"
- " -e, --exposure MS exposure value\n"
- " -g, --gain VALUE gain value\n"
- " -o, --output FILE output file pattern (default:"
- " out%%02d.png)\n"
- " -n, --count N number of image to take"
- " (default: gui)\n"
- , program_invocation_name);
- exit(status);
+ g_application_set_option_context_summary(app, "Microscope camera viewer");
+ g_application_set_option_context_description(app,
+ "Without options, run the GUI, else switch to batch mode to dump"
+ " image from the camera.");
+ g_application_add_main_option(app, "width", 'w', 0,
+ G_OPTION_ARG_INT, "image width", "VALUE");
+ g_application_add_main_option(app, "exposure", 'e', 0,
+ G_OPTION_ARG_DOUBLE, "exposure value", "MS");
+ g_application_add_main_option(app, "gain", 'g', 0,
+ G_OPTION_ARG_DOUBLE, "gain value", "VALUE");
+ g_application_add_main_option(app, "output", 'o', 0,
+ G_OPTION_ARG_FILENAME,
+ "output file pattern (default: out%02d.png)", "PATTERN");
+ g_application_add_main_option(app, "count", 'n', 0,
+ G_OPTION_ARG_INT, "number of image to take", "N");
}
-void
-options_parse(int argc, char **argv, struct options *options)
+gint
+options_handle(GApplication *app, GVariantDict *options_dict)
{
- options->width = -1;
- options->height = -1;
- options->exposure_ms = -1.0;
- options->gain = -1.0;
- options->count = 0;
- options->out = NULL;
- char *tail;
- while (1) {
- static struct option long_options[] = {
- { "help", no_argument, 0, 'h' },
- { "width", required_argument, 0, 'w' },
- { "exposure", required_argument, 0, 'e' },
- { "gain", required_argument, 0, 'g' },
- { "output", required_argument, 0, 'o' },
- { "count", required_argument, 0, 'n' },
- { NULL },
- };
- int option_index = 0;
- int c = getopt_long(argc, argv, "hw:e:g:o:n:", long_options,
- &option_index);
- if (c == -1)
- break;
- switch (c) {
- case 'h':
- usage(EXIT_SUCCESS, NULL);
- break;
- case 'w':
- errno = 0;
- options->width = strtol(optarg, &tail, 10);
- if (*tail != '\0' || errno || options->width < 1)
- usage(EXIT_FAILURE, "bad width value");
- break;
- case 'e':
- errno = 0;
- options->exposure_ms = strtod(optarg, &tail);
- if (*tail != '\0' || errno || options->exposure_ms < 1)
- usage(EXIT_FAILURE, "bad exposure value");
- break;
- case 'g':
- errno = 0;
- options->gain = strtod(optarg, &tail);
- if (*tail != '\0' || errno || options->gain <= 0.0)
- usage(EXIT_FAILURE, "bad gain value");
- break;
- case 'o':
- options->out = optarg;
- break;
- case 'n':
- errno = 0;
- options->count = strtoul(optarg, &tail, 10);
- if (*tail != '\0' || errno)
- usage(EXIT_FAILURE, "bad count value");
- break;
- case '?':
- usage(EXIT_FAILURE, NULL);
- break;
- default:
- abort();
- }
+ struct options options;
+ options.width = -1;
+ options.height = -1;
+ options.exposure_ms = -1.0;
+ options.gain = -1.0;
+ options.count = 1;
+ options.out = NULL;
+ bool option_set = false;
+ if (g_variant_dict_lookup(options_dict, "width", "i", &options.width)) {
+ option_set = true;
+ if (options.width <= 0)
+ utils_fatal("invalid width");
+ }
+ if (g_variant_dict_lookup(options_dict, "exposure", "d",
+ &options.exposure_ms)) {
+ option_set = true;
+ if (options.exposure_ms < 1.0)
+ utils_fatal("invalid exposure");
+ }
+ if (g_variant_dict_lookup(options_dict, "gain", "d", &options.gain)) {
+ option_set = true;
+ if (options.gain <= 0.0)
+ utils_fatal("invalid gain");
+ }
+ if (g_variant_dict_lookup(options_dict, "count", "i", &options.count)) {
+ option_set = true;
+ if (options.count <= 0)
+ utils_fatal("invalid count");
+ }
+ if (g_variant_dict_lookup(options_dict, "out", "^&ay", &options.out)) {
+ option_set = true;
+ int argtypes[1];
+ int formats = parse_printf_format(options.out, 1, argtypes);
+ if (formats != 1 || argtypes[0] != PA_INT)
+ utils_fatal("bad file pattern, use one %%d");
+ } else {
+ options.out = "out%02d.png";
}
- if (optind < argc)
- usage(EXIT_FAILURE, "too many arguments");
- if (!options->out)
- options->out = "out%02d.png";
- int argtypes[1];
- int formats = parse_printf_format(options->out, 1, argtypes);
- if (formats != 1 || argtypes[0] != PA_INT)
- usage(EXIT_FAILURE, "bad file pattern, use one %d");
+ if (option_set)
+ return cli_run(&options);
+ else
+ return -1;
}