/* Camicro - Microscope camera viewer. * * Copyright (C) 2019 Nicolas Schodet * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * Contact : * Web: http://ni.fr.eu.org/ * Email: */ #define _GNU_SOURCE #include #include #include #include #include #include #include "options.h" /* Report command line usage, optionally prefixed with an error message. */ static void usage(int status, const char *msg) { if (msg) fprintf(stderr, "%s: %s", program_invocation_short_name, msg); fprintf(status == EXIT_SUCCESS ? stdout : stderr, "usage: %s [options] [FILE]\n" "\n" "Microscope camera viewer.\n" "\n" "positional arguments:\n" " FILE output file pattern (default:" " out%%02d.png)\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" " -n, --count N number of image to take" " (default: live video)\n" , program_invocation_name); exit(status); } void options_parse(int argc, char **argv, struct options *options) { 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' }, { "count", required_argument, 0, 'n' }, { NULL }, }; int option_index = 0; int c = getopt_long(argc, argv, "hw:e:g: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 '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(); } } if (optind < argc) options->out = argv[optind++]; 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"); }