Skip to content

Commit 5c66cc2

Browse files
committed
test: use a common function for printing usage.
Shuffle the common usage() function in the common library, and remove it from all the individual applications. Signed-off-by: Robin Getz <[email protected]>
1 parent 503fa9b commit 5c66cc2

File tree

10 files changed

+56
-121
lines changed

10 files changed

+56
-121
lines changed

tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ add_executable(iio_readdev iio_readdev.c ${GETOPT_C_FILE} ${LIBIIO_RC})
3636
add_executable(iio_reg iio_reg.c ${GETOPT_C_FILE} ${LIBIIO_RC})
3737
add_executable(iio_writedev iio_writedev.c ${GETOPT_C_FILE} ${LIBIIO_RC})
3838

39-
target_link_libraries(iio_genxml iio)
39+
target_link_libraries(iio_genxml iio iio_tests_helper)
4040
target_link_libraries(iio_info iio iio_tests_helper)
4141
target_link_libraries(iio_attr iio iio_tests_helper)
4242
target_link_libraries(iio_readdev iio iio_tests_helper)

tests/iio_adi_xflow_check.c

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,24 +48,14 @@ static const struct option options[] = {
4848
};
4949

5050
static const char *options_descriptions[] = {
51+
"[-n <hostname>] [-u <uri>] [-a ] [-s <size>] <iio_device>",
5152
"Show this help and quit.",
5253
"Use the network backend with the provided hostname.",
5354
"Use the context with the provided URI.",
5455
"Size of the buffer in sample sets. Default is 1Msample",
5556
"Scan for available contexts and if only one is available use it.",
5657
};
5758

58-
static void usage(char *argv[])
59-
{
60-
unsigned int i;
61-
62-
printf("Usage:\n\t%s [-n <hostname>] [-u <uri>] [ -a ][-s <size>] <iio_device>\n\nOptions:\n", argv[0]);
63-
for (i = 0; options[i].name; i++)
64-
printf("\t-%c, --%s\n\t\t\t%s\n",
65-
options[i].val, options[i].name,
66-
options_descriptions[i]);
67-
}
68-
6959
static bool app_running = true;
7060
static bool device_is_tx;
7161

@@ -188,7 +178,7 @@ int main(int argc, char **argv)
188178
options, &option_index)) != -1) {
189179
switch (c) {
190180
case 'h':
191-
usage(argv);
181+
usage(MY_NAME, options, options_descriptions);
192182
return EXIT_SUCCESS;
193183
case 's':
194184
ret = sscanf(optarg, "%u%c", &buffer_size, &unit);
@@ -217,7 +207,7 @@ int main(int argc, char **argv)
217207

218208
if (optind + 1 != argc) {
219209
fprintf(stderr, "Incorrect number of arguments.\n\n");
220-
usage(argv);
210+
usage(MY_NAME, options, options_descriptions);
221211
return EXIT_FAILURE;
222212
}
223213

tests/iio_attr.c

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,11 @@ static const struct option options[] = {
292292
};
293293

294294
static const char *options_descriptions[] = {
295+
"-d [device] [attr] [value]\n"
296+
"\t\t\t\t-c [device] [channel] [attr] [value]\n"
297+
"\t\t\t\t-B [device] [attr] [value]\n"
298+
"\t\t\t\t-D [device] [attr] [value]\n"
299+
"\t\t\t\t-C [attr]",
295300
/* help */
296301
"Show this help and quit.",
297302
"Ignore case distinctions.",
@@ -312,34 +317,6 @@ static const char *options_descriptions[] = {
312317
"Read/Write debug attributes.",
313318
};
314319

315-
static void usage(void)
316-
{
317-
unsigned int i, j = 0, k;
318-
319-
printf("Usage:\n\t" MY_NAME " [OPTION]...\t-d [device] [attr] [value]\n"
320-
"\t\t\t\t-c [device] [channel] [attr] [value]\n"
321-
"\t\t\t\t-B [device] [attr] [value]\n"
322-
"\t\t\t\t-D [device] [attr] [value]\n"
323-
"\t\t\t\t-C [attr]\nOptions:\n");
324-
for (i = 0; options[i].name; i++) {
325-
k = strlen(options[i].name);
326-
if (k > j)
327-
j = k;
328-
}
329-
j++;
330-
for (i = 0; options[i].name; i++) {
331-
printf("\t-%c, --%s%*c: %s\n",
332-
options[i].val, options[i].name,
333-
j - (int)strlen(options[i].name), ' ',
334-
options_descriptions[i]);
335-
/* when printing out the help, add some subtitles, to help visually */
336-
if (i == 3)
337-
printf("Optional qualifiers:\n");
338-
if (i == 8)
339-
printf("Attribute types:\n");
340-
}
341-
}
342-
343320
int main(int argc, char **argv)
344321
{
345322
struct iio_context *ctx;
@@ -359,7 +336,7 @@ int main(int argc, char **argv)
359336
switch (c) {
360337
/* help */
361338
case 'h':
362-
usage();
339+
usage(MY_NAME, options, options_descriptions);
363340
return EXIT_SUCCESS;
364341
/* context connection */
365342
case 'a':
@@ -429,7 +406,7 @@ int main(int argc, char **argv)
429406

430407
if (!(search_device + search_channel + search_context + search_debug + search_buffer)) {
431408
if (argc == 1) {
432-
usage();
409+
usage(MY_NAME, options, options_descriptions);
433410
return EXIT_SUCCESS;
434411
}
435412
fprintf(stderr, "must specify one of -d, -c, -C, -B or -D.\n");

tests/iio_common.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <iio.h>
2323
#include <stdio.h>
2424
#include <inttypes.h>
25+
#include <getopt.h>
2526

2627
#include "iio_common.h"
2728
#include "gen_code.h"
@@ -128,3 +129,19 @@ unsigned long int sanitize_clamp(const char *name, const char *argv,
128129
return val;
129130
}
130131

132+
133+
void usage(char *name, const struct option *options,
134+
const char *options_descriptions[])
135+
{
136+
unsigned int i;
137+
138+
printf("Usage:\n");
139+
printf("\t%s [OPTION]...\t%s\n", name, options_descriptions[0]);
140+
printf("Options:\n");
141+
for (i = 0; options[i].name; i++) {
142+
printf("\t-%c, --%s\n\t\t\t%s\n",
143+
options[i].val, options[i].name,
144+
options_descriptions[i + 1]);
145+
}
146+
}
147+

tests/iio_common.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#ifndef IIO_TESTS_COMMON_H
2323
#define IIO_TESTS_COMMON_H
2424

25+
#include <getopt.h>
26+
2527
/*
2628
* internal buffers need to be big enough for attributes
2729
* coming back from the kernel. Because of virtual memory,
@@ -42,5 +44,6 @@ void * xmalloc(size_t n, const char *name);
4244
struct iio_context * autodetect_context(bool rtn, bool gen_code, const char *name);
4345
unsigned long int sanitize_clamp(const char *name, const char *argv,
4446
uint64_t min, uint64_t max);
47+
void usage(char *name, const struct option *options, const char *options_descriptions[]);
4548

4649
#endif /* IIO_TESTS_COMMON_H */

tests/iio_genxml.c

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,15 @@ static const struct option options[] = {
4141
};
4242

4343
static const char *options_descriptions[] = {
44+
"\t[-x <xml_file>]\n"
45+
"\t\t\t\t[-u <uri>]\n"
46+
"\t\t\t\t[-n <hostname>]",
4447
"Show this help and quit.",
4548
"Use the XML backend with the provided XML file.",
4649
"Use the network backend with the provided hostname.",
4750
"Use the context with the provided URI.",
4851
};
4952

50-
static void usage(void)
51-
{
52-
unsigned int i;
53-
54-
printf("Usage:\n\t" MY_NAME " [-x <xml_file>]\n\t"
55-
MY_NAME " [-u <uri>]\n\t"
56-
MY_NAME " [-n <hostname>]\n\nOptions:\n");
57-
for (i = 0; options[i].name; i++)
58-
printf("\t-%c, --%s\n\t\t\t%s\n",
59-
options[i].val, options[i].name,
60-
options_descriptions[i]);
61-
}
62-
6353
int main(int argc, char **argv)
6454
{
6555
char *xml;
@@ -74,7 +64,7 @@ int main(int argc, char **argv)
7464
options, &option_index)) != -1) {
7565
switch (c) {
7666
case 'h':
77-
usage();
67+
usage(MY_NAME, options, options_descriptions);
7868
return EXIT_SUCCESS;
7969
case 'n':
8070
if (backend != IIO_LOCAL) {
@@ -103,7 +93,7 @@ int main(int argc, char **argv)
10393

10494
if (optind != argc) {
10595
fprintf(stderr, "Incorrect number of arguments.\n\n");
106-
usage();
96+
usage(MY_NAME, options, options_descriptions);
10797
return EXIT_FAILURE;
10898
}
10999

tests/iio_info.c

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ static const struct option options[] = {
4545
};
4646

4747
static const char *options_descriptions[] = {
48+
"\t[-x <xml_file>]\n"
49+
"\t\t\t\t[-n <hostname>]\n"
50+
"\t\t\t\t[-u <uri>]",
4851
"Show this help and quit.",
4952
"Use the XML backend with the provided XML file.",
5053
"Use the network backend with the provided hostname.",
@@ -53,19 +56,6 @@ static const char *options_descriptions[] = {
5356
"Scan for available contexts and if only one is available use it.",
5457
};
5558

56-
static void usage(void)
57-
{
58-
unsigned int i;
59-
60-
printf("Usage:\n\t" MY_NAME " [-x <xml_file>]\n\t"
61-
MY_NAME " [-n <hostname>]\n\t"
62-
MY_NAME " [-u <uri>]\n\nOptions:\n");
63-
for (i = 0; options[i].name; i++)
64-
printf("\t-%c, --%s\n\t\t\t%s\n",
65-
options[i].val, options[i].name,
66-
options_descriptions[i]);
67-
}
68-
6959
static int dev_is_buffer_capable(const struct iio_device *dev)
7060
{
7161
unsigned int i;
@@ -97,7 +87,7 @@ int main(int argc, char **argv)
9787
options, &option_index)) != -1) {
9888
switch (c) {
9989
case 'h':
100-
usage();
90+
usage(MY_NAME, options, options_descriptions);
10191
return EXIT_SUCCESS;
10292
case 'n':
10393
if (backend != IIO_LOCAL) {
@@ -136,7 +126,7 @@ int main(int argc, char **argv)
136126

137127
if (optind != argc) {
138128
fprintf(stderr, "Incorrect number of arguments.\n\n");
139-
usage();
129+
usage(MY_NAME, options, options_descriptions);
140130
return EXIT_FAILURE;
141131
}
142132

tests/iio_readdev.c

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ static const struct option options[] = {
4747
};
4848

4949
static const char *options_descriptions[] = {
50+
"[-n <hostname>] [-t <trigger>] [-T <timeout-ms>] [-b <buffer-size>]"
51+
"[-s <samples>] <iio_device> [<channel> ...]",
5052
"Show this help and quit.",
5153
"Use the network backend with the provided hostname.",
5254
"Use the context with the provided URI.",
@@ -57,19 +59,6 @@ static const char *options_descriptions[] = {
5759
"Scan for available contexts and if only one is available use it.",
5860
};
5961

60-
static void usage(void)
61-
{
62-
unsigned int i;
63-
64-
printf("Usage:\n\t" MY_NAME " [-n <hostname>] [-t <trigger>] "
65-
"[-T <timeout-ms>] [-b <buffer-size>] [-s <samples>] "
66-
"<iio_device> [<channel> ...]\n\nOptions:\n");
67-
for (i = 0; options[i].name; i++)
68-
printf("\t-%c, --%s\n\t\t\t%s\n",
69-
options[i].val, options[i].name,
70-
options_descriptions[i]);
71-
}
72-
7362
static struct iio_context *ctx;
7463
static struct iio_buffer *buffer;
7564
static const char *trigger_name = NULL;
@@ -223,7 +212,7 @@ int main(int argc, char **argv)
223212
options, &option_index)) != -1) {
224213
switch (c) {
225214
case 'h':
226-
usage();
215+
usage(MY_NAME, options, options_descriptions);
227216
return EXIT_SUCCESS;
228217
case 'n':
229218
arg_ip = optarg;
@@ -253,7 +242,7 @@ int main(int argc, char **argv)
253242

254243
if (argc == optind) {
255244
fprintf(stderr, "Incorrect number of arguments.\n\n");
256-
usage();
245+
usage(MY_NAME, options, options_descriptions);
257246
return EXIT_FAILURE;
258247
}
259248

tests/iio_stresstest.c

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ static const struct option options[] = {
109109
};
110110

111111
static const char *options_descriptions[] = {
112+
"[-n <hostname>] [-u <vid>:<pid>] [-t <trigger>] [-b <buffer-size>] [-s <samples>]"
113+
"<iio_device> [<channel> ...]",
112114
"Show this help and quit.",
113115
"Use the context at the provided URI.",
114116
"Size of the capture buffer. Default is 256.",
@@ -118,19 +120,6 @@ static const char *options_descriptions[] = {
118120
"Increase verbosity (-vv and -vvv for more)",
119121
};
120122

121-
static void usage(void)
122-
{
123-
unsigned int i;
124-
125-
printf("Usage:\n\t" MY_NAME " [-n <hostname>] [-u <vid>:<pid>] "
126-
"[-t <trigger>] [-b <buffer-size>] [-s <samples>] "
127-
"<iio_device> [<channel> ...]\n\nOptions:\n");
128-
for (i = 0; options[i].name; i++)
129-
printf("\t-%c, --%s\n\t\t\t%s\n",
130-
options[i].val, options[i].name,
131-
options_descriptions[i]);
132-
}
133-
134123
static bool app_running = true;
135124
static bool threads_running = true;
136125
static int exit_code = EXIT_SUCCESS;
@@ -422,7 +411,7 @@ int main(int argc, char **argv)
422411
options, &option_index)) != -1) {
423412
switch (c) {
424413
case 'h':
425-
usage();
414+
usage(MY_NAME, options, options_descriptions);
426415
return EXIT_SUCCESS;
427416
case 'u':
428417
info.arg_index += 2;
@@ -490,15 +479,15 @@ int main(int argc, char **argv)
490479
}
491480
}
492481
fprintf(stderr, "\n");
493-
usage();
482+
usage(MY_NAME, options, options_descriptions);
494483
return EXIT_FAILURE;
495484
}
496485

497486
if (info.uri_index) {
498487
struct iio_context *ctx = iio_create_context_from_uri(info.argv[info.uri_index]);
499488
if (!ctx) {
500489
fprintf(stderr, "need valid uri\n");
501-
usage();
490+
usage(MY_NAME, options, options_descriptions);
502491
return EXIT_FAILURE;
503492
}
504493
iio_context_destroy(ctx);
@@ -511,7 +500,7 @@ int main(int argc, char **argv)
511500

512501
} else {
513502
fprintf(stderr, "need valid uri\n");
514-
usage();
503+
usage(MY_NAME, options, options_descriptions);
515504
return EXIT_FAILURE;
516505
}
517506

0 commit comments

Comments
 (0)