Skip to content

Commit

Permalink
test: use a common function for printing usage.
Browse files Browse the repository at this point in the history
Shuffle the common usage() function in the common library,
and remove it from all the individual applications.

Signed-off-by: Robin Getz <[email protected]>
  • Loading branch information
rgetz committed Apr 19, 2020
1 parent 503fa9b commit 5c66cc2
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 121 deletions.
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ add_executable(iio_readdev iio_readdev.c ${GETOPT_C_FILE} ${LIBIIO_RC})
add_executable(iio_reg iio_reg.c ${GETOPT_C_FILE} ${LIBIIO_RC})
add_executable(iio_writedev iio_writedev.c ${GETOPT_C_FILE} ${LIBIIO_RC})

target_link_libraries(iio_genxml iio)
target_link_libraries(iio_genxml iio iio_tests_helper)
target_link_libraries(iio_info iio iio_tests_helper)
target_link_libraries(iio_attr iio iio_tests_helper)
target_link_libraries(iio_readdev iio iio_tests_helper)
Expand Down
16 changes: 3 additions & 13 deletions tests/iio_adi_xflow_check.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,14 @@ static const struct option options[] = {
};

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

static void usage(char *argv[])
{
unsigned int i;

printf("Usage:\n\t%s [-n <hostname>] [-u <uri>] [ -a ][-s <size>] <iio_device>\n\nOptions:\n", argv[0]);
for (i = 0; options[i].name; i++)
printf("\t-%c, --%s\n\t\t\t%s\n",
options[i].val, options[i].name,
options_descriptions[i]);
}

static bool app_running = true;
static bool device_is_tx;

Expand Down Expand Up @@ -188,7 +178,7 @@ int main(int argc, char **argv)
options, &option_index)) != -1) {
switch (c) {
case 'h':
usage(argv);
usage(MY_NAME, options, options_descriptions);
return EXIT_SUCCESS;
case 's':
ret = sscanf(optarg, "%u%c", &buffer_size, &unit);
Expand Down Expand Up @@ -217,7 +207,7 @@ int main(int argc, char **argv)

if (optind + 1 != argc) {
fprintf(stderr, "Incorrect number of arguments.\n\n");
usage(argv);
usage(MY_NAME, options, options_descriptions);
return EXIT_FAILURE;
}

Expand Down
37 changes: 7 additions & 30 deletions tests/iio_attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,11 @@ static const struct option options[] = {
};

static const char *options_descriptions[] = {
"-d [device] [attr] [value]\n"
"\t\t\t\t-c [device] [channel] [attr] [value]\n"
"\t\t\t\t-B [device] [attr] [value]\n"
"\t\t\t\t-D [device] [attr] [value]\n"
"\t\t\t\t-C [attr]",
/* help */
"Show this help and quit.",
"Ignore case distinctions.",
Expand All @@ -312,34 +317,6 @@ static const char *options_descriptions[] = {
"Read/Write debug attributes.",
};

static void usage(void)
{
unsigned int i, j = 0, k;

printf("Usage:\n\t" MY_NAME " [OPTION]...\t-d [device] [attr] [value]\n"
"\t\t\t\t-c [device] [channel] [attr] [value]\n"
"\t\t\t\t-B [device] [attr] [value]\n"
"\t\t\t\t-D [device] [attr] [value]\n"
"\t\t\t\t-C [attr]\nOptions:\n");
for (i = 0; options[i].name; i++) {
k = strlen(options[i].name);
if (k > j)
j = k;
}
j++;
for (i = 0; options[i].name; i++) {
printf("\t-%c, --%s%*c: %s\n",
options[i].val, options[i].name,
j - (int)strlen(options[i].name), ' ',
options_descriptions[i]);
/* when printing out the help, add some subtitles, to help visually */
if (i == 3)
printf("Optional qualifiers:\n");
if (i == 8)
printf("Attribute types:\n");
}
}

int main(int argc, char **argv)
{
struct iio_context *ctx;
Expand All @@ -359,7 +336,7 @@ int main(int argc, char **argv)
switch (c) {
/* help */
case 'h':
usage();
usage(MY_NAME, options, options_descriptions);
return EXIT_SUCCESS;
/* context connection */
case 'a':
Expand Down Expand Up @@ -429,7 +406,7 @@ int main(int argc, char **argv)

if (!(search_device + search_channel + search_context + search_debug + search_buffer)) {
if (argc == 1) {
usage();
usage(MY_NAME, options, options_descriptions);
return EXIT_SUCCESS;
}
fprintf(stderr, "must specify one of -d, -c, -C, -B or -D.\n");
Expand Down
17 changes: 17 additions & 0 deletions tests/iio_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <iio.h>
#include <stdio.h>
#include <inttypes.h>
#include <getopt.h>

#include "iio_common.h"
#include "gen_code.h"
Expand Down Expand Up @@ -128,3 +129,19 @@ unsigned long int sanitize_clamp(const char *name, const char *argv,
return val;
}


void usage(char *name, const struct option *options,
const char *options_descriptions[])
{
unsigned int i;

printf("Usage:\n");
printf("\t%s [OPTION]...\t%s\n", name, options_descriptions[0]);
printf("Options:\n");
for (i = 0; options[i].name; i++) {
printf("\t-%c, --%s\n\t\t\t%s\n",
options[i].val, options[i].name,
options_descriptions[i + 1]);
}
}

3 changes: 3 additions & 0 deletions tests/iio_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#ifndef IIO_TESTS_COMMON_H
#define IIO_TESTS_COMMON_H

#include <getopt.h>

/*
* internal buffers need to be big enough for attributes
* coming back from the kernel. Because of virtual memory,
Expand All @@ -42,5 +44,6 @@ void * xmalloc(size_t n, const char *name);
struct iio_context * autodetect_context(bool rtn, bool gen_code, const char *name);
unsigned long int sanitize_clamp(const char *name, const char *argv,
uint64_t min, uint64_t max);
void usage(char *name, const struct option *options, const char *options_descriptions[]);

#endif /* IIO_TESTS_COMMON_H */
20 changes: 5 additions & 15 deletions tests/iio_genxml.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,15 @@ static const struct option options[] = {
};

static const char *options_descriptions[] = {
"\t[-x <xml_file>]\n"
"\t\t\t\t[-u <uri>]\n"
"\t\t\t\t[-n <hostname>]",
"Show this help and quit.",
"Use the XML backend with the provided XML file.",
"Use the network backend with the provided hostname.",
"Use the context with the provided URI.",
};

static void usage(void)
{
unsigned int i;

printf("Usage:\n\t" MY_NAME " [-x <xml_file>]\n\t"
MY_NAME " [-u <uri>]\n\t"
MY_NAME " [-n <hostname>]\n\nOptions:\n");
for (i = 0; options[i].name; i++)
printf("\t-%c, --%s\n\t\t\t%s\n",
options[i].val, options[i].name,
options_descriptions[i]);
}

int main(int argc, char **argv)
{
char *xml;
Expand All @@ -74,7 +64,7 @@ int main(int argc, char **argv)
options, &option_index)) != -1) {
switch (c) {
case 'h':
usage();
usage(MY_NAME, options, options_descriptions);
return EXIT_SUCCESS;
case 'n':
if (backend != IIO_LOCAL) {
Expand Down Expand Up @@ -103,7 +93,7 @@ int main(int argc, char **argv)

if (optind != argc) {
fprintf(stderr, "Incorrect number of arguments.\n\n");
usage();
usage(MY_NAME, options, options_descriptions);
return EXIT_FAILURE;
}

Expand Down
20 changes: 5 additions & 15 deletions tests/iio_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ static const struct option options[] = {
};

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

static void usage(void)
{
unsigned int i;

printf("Usage:\n\t" MY_NAME " [-x <xml_file>]\n\t"
MY_NAME " [-n <hostname>]\n\t"
MY_NAME " [-u <uri>]\n\nOptions:\n");
for (i = 0; options[i].name; i++)
printf("\t-%c, --%s\n\t\t\t%s\n",
options[i].val, options[i].name,
options_descriptions[i]);
}

static int dev_is_buffer_capable(const struct iio_device *dev)
{
unsigned int i;
Expand Down Expand Up @@ -97,7 +87,7 @@ int main(int argc, char **argv)
options, &option_index)) != -1) {
switch (c) {
case 'h':
usage();
usage(MY_NAME, options, options_descriptions);
return EXIT_SUCCESS;
case 'n':
if (backend != IIO_LOCAL) {
Expand Down Expand Up @@ -136,7 +126,7 @@ int main(int argc, char **argv)

if (optind != argc) {
fprintf(stderr, "Incorrect number of arguments.\n\n");
usage();
usage(MY_NAME, options, options_descriptions);
return EXIT_FAILURE;
}

Expand Down
19 changes: 4 additions & 15 deletions tests/iio_readdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ static const struct option options[] = {
};

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

static void usage(void)
{
unsigned int i;

printf("Usage:\n\t" MY_NAME " [-n <hostname>] [-t <trigger>] "
"[-T <timeout-ms>] [-b <buffer-size>] [-s <samples>] "
"<iio_device> [<channel> ...]\n\nOptions:\n");
for (i = 0; options[i].name; i++)
printf("\t-%c, --%s\n\t\t\t%s\n",
options[i].val, options[i].name,
options_descriptions[i]);
}

static struct iio_context *ctx;
static struct iio_buffer *buffer;
static const char *trigger_name = NULL;
Expand Down Expand Up @@ -223,7 +212,7 @@ int main(int argc, char **argv)
options, &option_index)) != -1) {
switch (c) {
case 'h':
usage();
usage(MY_NAME, options, options_descriptions);
return EXIT_SUCCESS;
case 'n':
arg_ip = optarg;
Expand Down Expand Up @@ -253,7 +242,7 @@ int main(int argc, char **argv)

if (argc == optind) {
fprintf(stderr, "Incorrect number of arguments.\n\n");
usage();
usage(MY_NAME, options, options_descriptions);
return EXIT_FAILURE;
}

Expand Down
23 changes: 6 additions & 17 deletions tests/iio_stresstest.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ static const struct option options[] = {
};

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

static void usage(void)
{
unsigned int i;

printf("Usage:\n\t" MY_NAME " [-n <hostname>] [-u <vid>:<pid>] "
"[-t <trigger>] [-b <buffer-size>] [-s <samples>] "
"<iio_device> [<channel> ...]\n\nOptions:\n");
for (i = 0; options[i].name; i++)
printf("\t-%c, --%s\n\t\t\t%s\n",
options[i].val, options[i].name,
options_descriptions[i]);
}

static bool app_running = true;
static bool threads_running = true;
static int exit_code = EXIT_SUCCESS;
Expand Down Expand Up @@ -422,7 +411,7 @@ int main(int argc, char **argv)
options, &option_index)) != -1) {
switch (c) {
case 'h':
usage();
usage(MY_NAME, options, options_descriptions);
return EXIT_SUCCESS;
case 'u':
info.arg_index += 2;
Expand Down Expand Up @@ -490,15 +479,15 @@ int main(int argc, char **argv)
}
}
fprintf(stderr, "\n");
usage();
usage(MY_NAME, options, options_descriptions);
return EXIT_FAILURE;
}

if (info.uri_index) {
struct iio_context *ctx = iio_create_context_from_uri(info.argv[info.uri_index]);
if (!ctx) {
fprintf(stderr, "need valid uri\n");
usage();
usage(MY_NAME, options, options_descriptions);
return EXIT_FAILURE;
}
iio_context_destroy(ctx);
Expand All @@ -511,7 +500,7 @@ int main(int argc, char **argv)

} else {
fprintf(stderr, "need valid uri\n");
usage();
usage(MY_NAME, options, options_descriptions);
return EXIT_FAILURE;
}

Expand Down
Loading

0 comments on commit 5c66cc2

Please sign in to comment.