]> sigrok.org Git - sigrok-cli.git/blob - options.c
b26ca4bee9a29f0270f7f013be0155bc300d7680
[sigrok-cli.git] / options.c
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
23 gboolean opt_version = FALSE;
24 gint opt_loglevel = SR_LOG_WARN; /* Show errors+warnings by default. */
25 gboolean opt_scan_devs = FALSE;
26 gboolean opt_wait_trigger = FALSE;
27 gchar *opt_input_file = NULL;
28 gchar *opt_output_file = NULL;
29 gchar *opt_drv = NULL;
30 gchar *opt_config = NULL;
31 gchar *opt_channels = NULL;
32 gchar *opt_channel_group = NULL;
33 gchar *opt_triggers = NULL;
34 gchar *opt_pds = NULL;
35 #ifdef HAVE_SRD
36 gchar *opt_pd_stack = NULL;
37 gchar *opt_pd_annotations = NULL;
38 gchar *opt_pd_meta = NULL;
39 gchar *opt_pd_binary = NULL;
40 #endif
41 gchar *opt_input_format = NULL;
42 gchar *opt_output_format = NULL;
43 gchar *opt_show = NULL;
44 gchar *opt_time = NULL;
45 gchar *opt_samples = NULL;
46 gchar *opt_frames = NULL;
47 gchar *opt_continuous = NULL;
48 gchar *opt_set = NULL;
49
50 static const GOptionEntry optargs[] = {
51         {"version", 'V', 0, G_OPTION_ARG_NONE, &opt_version,
52                         "Show version and support list", NULL},
53         {"loglevel", 'l', 0, G_OPTION_ARG_INT, &opt_loglevel,
54                         "Set loglevel (5 is most verbose)", NULL},
55         {"driver", 'd', 0, G_OPTION_ARG_STRING, &opt_drv,
56                         "The driver to use", NULL},
57         {"config", 'c', 0, G_OPTION_ARG_STRING, &opt_config,
58                         "Specify device configuration options", NULL},
59         {"input-file", 'i', 0, G_OPTION_ARG_FILENAME, &opt_input_file,
60                         "Load input from file", NULL},
61         {"input-format", 'I', 0, G_OPTION_ARG_STRING, &opt_input_format,
62                         "Input format", NULL},
63         {"output-file", 'o', 0, G_OPTION_ARG_FILENAME, &opt_output_file,
64                         "Save output to file", NULL},
65         {"output-format", 'O', 0, G_OPTION_ARG_STRING, &opt_output_format,
66                         "Output format", NULL},
67         {"channels", 'C', 0, G_OPTION_ARG_STRING, &opt_channels,
68                         "Channels to use", NULL},
69         {"channel-group", 'g', 0, G_OPTION_ARG_STRING, &opt_channel_group,
70                         "Channel groups", NULL},
71         {"triggers", 't', 0, G_OPTION_ARG_STRING, &opt_triggers,
72                         "Trigger configuration", NULL},
73         {"wait-trigger", 'w', 0, G_OPTION_ARG_NONE, &opt_wait_trigger,
74                         "Wait for trigger", NULL},
75 #ifdef HAVE_SRD
76         {"protocol-decoders", 'P', 0, G_OPTION_ARG_STRING, &opt_pds,
77                         "Protocol decoders to run", NULL},
78         {"protocol-decoder-stack", 'S', 0, G_OPTION_ARG_STRING, &opt_pd_stack,
79                         "Protocol decoder stack", NULL},
80         {"protocol-decoder-annotations", 'A', 0, G_OPTION_ARG_STRING, &opt_pd_annotations,
81                         "Protocol decoder annotation(s) to show", NULL},
82         {"protocol-decoder-meta", 'M', 0, G_OPTION_ARG_STRING, &opt_pd_meta,
83                         "Protocol decoder meta output to show", NULL},
84         {"protocol-decoder-binary", 'B', 0, G_OPTION_ARG_STRING, &opt_pd_binary,
85                         "Protocol decoder binary output to show", NULL},
86 #endif
87         {"scan", 0, 0, G_OPTION_ARG_NONE, &opt_scan_devs,
88                         "Scan for devices", NULL},
89         {"show", 0, 0, G_OPTION_ARG_NONE, &opt_show,
90                         "Show device detail", NULL},
91         {"time", 0, 0, G_OPTION_ARG_STRING, &opt_time,
92                         "How long to sample (ms)", NULL},
93         {"samples", 0, 0, G_OPTION_ARG_STRING, &opt_samples,
94                         "Number of samples to acquire", NULL},
95         {"frames", 0, 0, G_OPTION_ARG_STRING, &opt_frames,
96                         "Number of frames to acquire", NULL},
97         {"continuous", 0, 0, G_OPTION_ARG_NONE, &opt_continuous,
98                         "Sample continuously", NULL},
99         {"set", 0, 0, G_OPTION_ARG_NONE, &opt_set, "Set device options only", NULL},
100         {NULL, 0, 0, 0, NULL, NULL, NULL}
101 };
102
103 /* Parses the command line and sets all the 'opt_...' variables.
104    Returns zero on success, non-zero otherwise. */
105 int parse_options(int argc, char **argv)
106 {
107         GError *error = NULL;
108         GOptionContext *context = g_option_context_new(NULL);
109         int ret = 1;
110
111         g_option_context_add_main_entries(context, optargs, NULL);
112
113         if (!g_option_context_parse(context, &argc, &argv, &error)) {
114                 g_critical("%s", error->message);
115                 goto done;
116         }
117
118         ret = 0;
119
120 done:
121         g_option_context_free(context);
122
123         return ret;
124 }
125
126 void show_help(void)
127 {
128         GOptionContext *context = g_option_context_new(NULL);
129         g_option_context_add_main_entries(context, optargs, NULL);
130
131         char *help = g_option_context_get_help(context, TRUE, NULL);
132         printf("%s", help);
133         g_free(help);
134
135         g_option_context_free(context);
136 }