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