summaryrefslogtreecommitdiff
path: root/options.c
blob: 2bf1489f04f51ffb337f2a07fe3a10ef03c81913 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* 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: <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 "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");
}