]> sigrok.org Git - sigrok-cli.git/blame - options.c
options: String options are only allowed once.
[sigrok-cli.git] / options.c
CommitLineData
cd62e027
JS
1/*
2 * This file is part of the sigrok-cli project.
3 *
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "sigrok-cli.h"
21#include <glib.h>
22
23gboolean opt_version = FALSE;
24gint opt_loglevel = SR_LOG_WARN; /* Show errors+warnings by default. */
25gboolean opt_scan_devs = FALSE;
26gboolean opt_wait_trigger = FALSE;
27gchar *opt_input_file = NULL;
28gchar *opt_output_file = NULL;
29gchar *opt_drv = NULL;
30gchar *opt_config = NULL;
31gchar *opt_channels = NULL;
32gchar *opt_channel_group = NULL;
33gchar *opt_triggers = NULL;
34gchar *opt_pds = NULL;
35#ifdef HAVE_SRD
36gchar *opt_pd_stack = NULL;
37gchar *opt_pd_annotations = NULL;
38gchar *opt_pd_meta = NULL;
39gchar *opt_pd_binary = NULL;
40#endif
41gchar *opt_input_format = NULL;
42gchar *opt_output_format = NULL;
43gchar *opt_show = NULL;
44gchar *opt_time = NULL;
45gchar *opt_samples = NULL;
46gchar *opt_frames = NULL;
47gchar *opt_continuous = NULL;
48gchar *opt_set = NULL;
49
0894b972
JS
50/* defines a callback function that generates
51 an error if an option occurs twice */
52#define CHECK_ONCE(option) \
53static gboolean check_ ## option \
54 (const gchar *option_name, const gchar *value, \
55 gpointer data, GError **error) \
56{ \
57 (void)data; \
58 \
59 static gboolean seen = FALSE; \
60 if (seen) { \
61 g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED, \
62 "superfluous option \"%s\"", option_name); \
63 return FALSE; \
64 } \
65 \
66 option = g_strdup(value); \
67 seen = TRUE; \
68 return TRUE; \
69}
70
71CHECK_ONCE(opt_drv)
72CHECK_ONCE(opt_config)
73CHECK_ONCE(opt_input_format)
74CHECK_ONCE(opt_output_format)
75CHECK_ONCE(opt_channels)
76CHECK_ONCE(opt_channel_group)
77CHECK_ONCE(opt_triggers)
78#ifdef HAVE_SRD
79CHECK_ONCE(opt_pds)
80CHECK_ONCE(opt_pd_stack)
81CHECK_ONCE(opt_pd_annotations)
82CHECK_ONCE(opt_pd_meta)
83CHECK_ONCE(opt_pd_binary)
84#endif
85CHECK_ONCE(opt_time)
86CHECK_ONCE(opt_samples)
87CHECK_ONCE(opt_frames)
88
89#undef CHECK_STR_ONCE
90
cd62e027
JS
91static const GOptionEntry optargs[] = {
92 {"version", 'V', 0, G_OPTION_ARG_NONE, &opt_version,
93 "Show version and support list", NULL},
94 {"loglevel", 'l', 0, G_OPTION_ARG_INT, &opt_loglevel,
95 "Set loglevel (5 is most verbose)", NULL},
0894b972 96 {"driver", 'd', 0, G_OPTION_ARG_CALLBACK, &check_opt_drv,
cd62e027 97 "The driver to use", NULL},
0894b972 98 {"config", 'c', 0, G_OPTION_ARG_CALLBACK, &check_opt_config,
cd62e027
JS
99 "Specify device configuration options", NULL},
100 {"input-file", 'i', 0, G_OPTION_ARG_FILENAME, &opt_input_file,
101 "Load input from file", NULL},
0894b972 102 {"input-format", 'I', 0, G_OPTION_ARG_CALLBACK, &check_opt_input_format,
cd62e027
JS
103 "Input format", NULL},
104 {"output-file", 'o', 0, G_OPTION_ARG_FILENAME, &opt_output_file,
105 "Save output to file", NULL},
0894b972 106 {"output-format", 'O', 0, G_OPTION_ARG_CALLBACK, &check_opt_output_format,
cd62e027 107 "Output format", NULL},
0894b972 108 {"channels", 'C', 0, G_OPTION_ARG_CALLBACK, &check_opt_channels,
cd62e027 109 "Channels to use", NULL},
0894b972 110 {"channel-group", 'g', 0, G_OPTION_ARG_CALLBACK, &check_opt_channel_group,
cd62e027 111 "Channel groups", NULL},
0894b972 112 {"triggers", 't', 0, G_OPTION_ARG_CALLBACK, &check_opt_triggers,
cd62e027
JS
113 "Trigger configuration", NULL},
114 {"wait-trigger", 'w', 0, G_OPTION_ARG_NONE, &opt_wait_trigger,
115 "Wait for trigger", NULL},
116#ifdef HAVE_SRD
0894b972 117 {"protocol-decoders", 'P', 0, G_OPTION_ARG_CALLBACK, &check_opt_pds,
cd62e027 118 "Protocol decoders to run", NULL},
0894b972 119 {"protocol-decoder-stack", 'S', 0, G_OPTION_ARG_CALLBACK, &check_opt_pd_stack,
cd62e027 120 "Protocol decoder stack", NULL},
0894b972 121 {"protocol-decoder-annotations", 'A', 0, G_OPTION_ARG_CALLBACK, &check_opt_pd_annotations,
cd62e027 122 "Protocol decoder annotation(s) to show", NULL},
0894b972 123 {"protocol-decoder-meta", 'M', 0, G_OPTION_ARG_CALLBACK, &check_opt_pd_meta,
cd62e027 124 "Protocol decoder meta output to show", NULL},
0894b972 125 {"protocol-decoder-binary", 'B', 0, G_OPTION_ARG_CALLBACK, &check_opt_pd_binary,
cd62e027
JS
126 "Protocol decoder binary output to show", NULL},
127#endif
128 {"scan", 0, 0, G_OPTION_ARG_NONE, &opt_scan_devs,
129 "Scan for devices", NULL},
130 {"show", 0, 0, G_OPTION_ARG_NONE, &opt_show,
131 "Show device detail", NULL},
0894b972 132 {"time", 0, 0, G_OPTION_ARG_CALLBACK, &check_opt_time,
cd62e027 133 "How long to sample (ms)", NULL},
0894b972 134 {"samples", 0, 0, G_OPTION_ARG_CALLBACK, &check_opt_samples,
cd62e027 135 "Number of samples to acquire", NULL},
0894b972 136 {"frames", 0, 0, G_OPTION_ARG_CALLBACK, &check_opt_frames,
cd62e027
JS
137 "Number of frames to acquire", NULL},
138 {"continuous", 0, 0, G_OPTION_ARG_NONE, &opt_continuous,
139 "Sample continuously", NULL},
140 {"set", 0, 0, G_OPTION_ARG_NONE, &opt_set, "Set device options only", NULL},
141 {NULL, 0, 0, 0, NULL, NULL, NULL}
142};
143
144/* Parses the command line and sets all the 'opt_...' variables.
145 Returns zero on success, non-zero otherwise. */
146int parse_options(int argc, char **argv)
147{
148 GError *error = NULL;
149 GOptionContext *context = g_option_context_new(NULL);
150 int ret = 1;
151
152 g_option_context_add_main_entries(context, optargs, NULL);
153
154 if (!g_option_context_parse(context, &argc, &argv, &error)) {
155 g_critical("%s", error->message);
156 goto done;
157 }
158
159 ret = 0;
160
161done:
162 g_option_context_free(context);
163
164 return ret;
165}
166
167void show_help(void)
168{
169 GOptionContext *context = g_option_context_new(NULL);
170 g_option_context_add_main_entries(context, optargs, NULL);
171
172 char *help = g_option_context_get_help(context, TRUE, NULL);
173 printf("%s", help);
174 g_free(help);
175
176 g_option_context_free(context);
177}