]> sigrok.org Git - sigrok-cli.git/blame - options.c
Add --list-supported-wiki option.
[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
d486cbdd 20#include <config.h>
cd62e027 21#include <glib.h>
662a1e27 22#include "sigrok-cli.h"
cd62e027
JS
23
24gboolean opt_version = FALSE;
6293db8a 25gboolean opt_list_supported = FALSE;
c45dd41c 26gboolean opt_list_supported_wiki = FALSE;
cd62e027
JS
27gint opt_loglevel = SR_LOG_WARN; /* Show errors+warnings by default. */
28gboolean opt_scan_devs = FALSE;
29gboolean opt_wait_trigger = FALSE;
30gchar *opt_input_file = NULL;
31gchar *opt_output_file = NULL;
32gchar *opt_drv = NULL;
33gchar *opt_config = NULL;
34gchar *opt_channels = NULL;
35gchar *opt_channel_group = NULL;
36gchar *opt_triggers = NULL;
551f570c 37gchar **opt_pds = NULL;
cd62e027 38#ifdef HAVE_SRD
cd62e027
JS
39gchar *opt_pd_annotations = NULL;
40gchar *opt_pd_meta = NULL;
41gchar *opt_pd_binary = NULL;
08e9378b 42gboolean opt_pd_samplenum = FALSE;
cd62e027
JS
43#endif
44gchar *opt_input_format = NULL;
45gchar *opt_output_format = NULL;
6f7b4c5d 46gchar *opt_transform_module = NULL;
2620358a 47gboolean opt_show = FALSE;
cd62e027
JS
48gchar *opt_time = NULL;
49gchar *opt_samples = NULL;
50gchar *opt_frames = NULL;
2620358a 51gboolean opt_continuous = FALSE;
62a64762 52gchar *opt_get = NULL;
2620358a 53gboolean opt_set = FALSE;
cd62e027 54
f0f54487
UH
55/*
56 * Defines a callback function that generates an error if an
57 * option occurs twice.
58 */
0894b972
JS
59#define CHECK_ONCE(option) \
60static gboolean check_ ## option \
61 (const gchar *option_name, const gchar *value, \
62 gpointer data, GError **error) \
63{ \
64 (void)data; \
65 \
66 static gboolean seen = FALSE; \
67 if (seen) { \
68 g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED, \
69 "superfluous option \"%s\"", option_name); \
70 return FALSE; \
71 } \
72 \
73 option = g_strdup(value); \
74 seen = TRUE; \
75 return TRUE; \
76}
77
78CHECK_ONCE(opt_drv)
79CHECK_ONCE(opt_config)
80CHECK_ONCE(opt_input_format)
81CHECK_ONCE(opt_output_format)
6f7b4c5d 82CHECK_ONCE(opt_transform_module)
0894b972
JS
83CHECK_ONCE(opt_channels)
84CHECK_ONCE(opt_channel_group)
85CHECK_ONCE(opt_triggers)
86#ifdef HAVE_SRD
0894b972
JS
87CHECK_ONCE(opt_pd_annotations)
88CHECK_ONCE(opt_pd_meta)
89CHECK_ONCE(opt_pd_binary)
90#endif
91CHECK_ONCE(opt_time)
92CHECK_ONCE(opt_samples)
93CHECK_ONCE(opt_frames)
62a64762 94CHECK_ONCE(opt_get)
0894b972
JS
95
96#undef CHECK_STR_ONCE
97
44ac7614
JS
98static gchar **input_file_array = NULL;
99static gchar **output_file_array = NULL;
100
cd62e027
JS
101static const GOptionEntry optargs[] = {
102 {"version", 'V', 0, G_OPTION_ARG_NONE, &opt_version,
6293db8a
UH
103 "Show version", NULL},
104 {"list-supported", 'L', 0, G_OPTION_ARG_NONE, &opt_list_supported,
105 "List supported devices/modules/decoders", NULL},
c45dd41c
UH
106 {"list-supported-wiki", 0, 0, G_OPTION_ARG_NONE, &opt_list_supported_wiki,
107 "List supported decoders (MediaWiki)", NULL},
cd62e027
JS
108 {"loglevel", 'l', 0, G_OPTION_ARG_INT, &opt_loglevel,
109 "Set loglevel (5 is most verbose)", NULL},
0894b972 110 {"driver", 'd', 0, G_OPTION_ARG_CALLBACK, &check_opt_drv,
cd62e027 111 "The driver to use", NULL},
0894b972 112 {"config", 'c', 0, G_OPTION_ARG_CALLBACK, &check_opt_config,
cd62e027 113 "Specify device configuration options", NULL},
44ac7614 114 {"input-file", 'i', 0, G_OPTION_ARG_FILENAME_ARRAY, &input_file_array,
cd62e027 115 "Load input from file", NULL},
0894b972 116 {"input-format", 'I', 0, G_OPTION_ARG_CALLBACK, &check_opt_input_format,
cd62e027 117 "Input format", NULL},
44ac7614 118 {"output-file", 'o', 0, G_OPTION_ARG_FILENAME_ARRAY, &output_file_array,
cd62e027 119 "Save output to file", NULL},
0894b972 120 {"output-format", 'O', 0, G_OPTION_ARG_CALLBACK, &check_opt_output_format,
cd62e027 121 "Output format", NULL},
6f7b4c5d
UH
122 {"transform-module", 'T', 0, G_OPTION_ARG_CALLBACK, &check_opt_transform_module,
123 "Transform module", NULL},
0894b972 124 {"channels", 'C', 0, G_OPTION_ARG_CALLBACK, &check_opt_channels,
cd62e027 125 "Channels to use", NULL},
0894b972 126 {"channel-group", 'g', 0, G_OPTION_ARG_CALLBACK, &check_opt_channel_group,
cd62e027 127 "Channel groups", NULL},
0894b972 128 {"triggers", 't', 0, G_OPTION_ARG_CALLBACK, &check_opt_triggers,
cd62e027
JS
129 "Trigger configuration", NULL},
130 {"wait-trigger", 'w', 0, G_OPTION_ARG_NONE, &opt_wait_trigger,
131 "Wait for trigger", NULL},
132#ifdef HAVE_SRD
551f570c 133 {"protocol-decoders", 'P', 0, G_OPTION_ARG_STRING_ARRAY, &opt_pds,
cd62e027 134 "Protocol decoders to run", NULL},
0894b972 135 {"protocol-decoder-annotations", 'A', 0, G_OPTION_ARG_CALLBACK, &check_opt_pd_annotations,
cd62e027 136 "Protocol decoder annotation(s) to show", NULL},
0894b972 137 {"protocol-decoder-meta", 'M', 0, G_OPTION_ARG_CALLBACK, &check_opt_pd_meta,
cd62e027 138 "Protocol decoder meta output to show", NULL},
0894b972 139 {"protocol-decoder-binary", 'B', 0, G_OPTION_ARG_CALLBACK, &check_opt_pd_binary,
cd62e027 140 "Protocol decoder binary output to show", NULL},
08e9378b 141 {"protocol-decoder-samplenum", 0, 0, G_OPTION_ARG_NONE, &opt_pd_samplenum,
bf5d7c77 142 "Show sample numbers in decoder output", NULL},
cd62e027
JS
143#endif
144 {"scan", 0, 0, G_OPTION_ARG_NONE, &opt_scan_devs,
145 "Scan for devices", NULL},
146 {"show", 0, 0, G_OPTION_ARG_NONE, &opt_show,
96d09211 147 "Show device/format/decoder details", NULL},
0894b972 148 {"time", 0, 0, G_OPTION_ARG_CALLBACK, &check_opt_time,
cd62e027 149 "How long to sample (ms)", NULL},
0894b972 150 {"samples", 0, 0, G_OPTION_ARG_CALLBACK, &check_opt_samples,
cd62e027 151 "Number of samples to acquire", NULL},
0894b972 152 {"frames", 0, 0, G_OPTION_ARG_CALLBACK, &check_opt_frames,
cd62e027
JS
153 "Number of frames to acquire", NULL},
154 {"continuous", 0, 0, G_OPTION_ARG_NONE, &opt_continuous,
155 "Sample continuously", NULL},
bf5d7c77 156 {"get", 0, 0, G_OPTION_ARG_CALLBACK, &check_opt_get, "Get device options only", NULL},
cd62e027
JS
157 {"set", 0, 0, G_OPTION_ARG_NONE, &opt_set, "Set device options only", NULL},
158 {NULL, 0, 0, 0, NULL, NULL, NULL}
159};
160
f0f54487
UH
161/*
162 * Parses the command line and sets all the 'opt_...' variables.
163 * Returns zero on success, non-zero otherwise.
164 */
cd62e027
JS
165int parse_options(int argc, char **argv)
166{
167 GError *error = NULL;
168 GOptionContext *context = g_option_context_new(NULL);
169 int ret = 1;
170
171 g_option_context_add_main_entries(context, optargs, NULL);
172
173 if (!g_option_context_parse(context, &argc, &argv, &error)) {
174 g_critical("%s", error->message);
175 goto done;
176 }
177
44ac7614
JS
178 /*
179 * Because of encoding issues with filenames (mentioned in the glib
180 * documentation), we don't check them with a callback function, but
181 * collect them into arrays and then check if the arrays contain at
182 * most one element.
183 */
184 if (NULL != input_file_array) {
185 if (NULL != input_file_array[0] && NULL != input_file_array[1]) {
186 g_critical("option \"--input-file/-i\" only allowed once");
187 goto done;
188 }
189 opt_input_file = g_strdup(input_file_array[0]);
190 }
191
192 if (NULL != output_file_array) {
193 if (NULL != output_file_array[0] && NULL != output_file_array[1]) {
194 g_critical("option \"--output-file/-o\" only allowed once");
195 goto done;
196 }
197 opt_output_file = g_strdup(output_file_array[0]);
198 }
199
41ce2cbb
JS
200 if (1 != argc) {
201 g_critical("superfluous command line argument \"%s\"", argv[1]);
202 goto done;
203 }
204
cd62e027
JS
205 ret = 0;
206
207done:
208 g_option_context_free(context);
44ac7614
JS
209 g_strfreev(input_file_array);
210 g_strfreev(output_file_array);
211 input_file_array = NULL;
212 output_file_array = NULL;
cd62e027
JS
213
214 return ret;
215}
216
217void show_help(void)
218{
219 GOptionContext *context = g_option_context_new(NULL);
220 g_option_context_add_main_entries(context, optargs, NULL);
221
222 char *help = g_option_context_get_help(context, TRUE, NULL);
223 printf("%s", help);
224 g_free(help);
225
226 g_option_context_free(context);
65c1ecf9
GS
227
228#ifdef HAVE_SRD
229#define SHOW_DECODER_TEXT "| -P <decoder> "
230#else
231#define SHOW_DECODER_TEXT ""
232#endif
233 printf("Example use, typical options:\n");
234 printf(" -d <driver> --scan\n");
235 printf(" -d <driver> { --samples N | --frames N | --time T | --continuous }\n");
236 printf(" { -d <driver> | -I <format> | -O <format> %s} --show\n", SHOW_DECODER_TEXT);
237 printf(" See the manpage or the wiki for more details.\n");
238 printf(" Note: --samples/--frames/--time/--continuous is required for acquisition.\n");
cd62e027 239}