From 4dee95d352a3767d75fc1def0b25c1ae8d72bd33 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Mon, 28 Dec 2015 17:36:32 +0100 Subject: [PATCH] Add the version command. --- examples/Makefile.am | 1 + examples/dctool.c | 1 + examples/dctool.h | 1 + examples/dctool_version.c | 94 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 97 insertions(+) create mode 100644 examples/dctool_version.c diff --git a/examples/Makefile.am b/examples/Makefile.am index 65a4024..186ec97 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -10,5 +10,6 @@ dctool_SOURCES = \ dctool.h \ dctool.c \ dctool_help.c \ + dctool_version.c \ utils.h \ utils.c diff --git a/examples/dctool.c b/examples/dctool.c index 28d94c1..da36120 100644 --- a/examples/dctool.c +++ b/examples/dctool.c @@ -49,6 +49,7 @@ static const dctool_command_t *g_commands[] = { &dctool_help, + &dctool_version, NULL }; diff --git a/examples/dctool.h b/examples/dctool.h index 8edbc6a..3b422bd 100644 --- a/examples/dctool.h +++ b/examples/dctool.h @@ -43,6 +43,7 @@ typedef struct dctool_command_t { } dctool_command_t; extern const dctool_command_t dctool_help; +extern const dctool_command_t dctool_version; const dctool_command_t * dctool_command_find (const char *name); diff --git a/examples/dctool_version.c b/examples/dctool_version.c new file mode 100644 index 0000000..deb0702 --- /dev/null +++ b/examples/dctool_version.c @@ -0,0 +1,94 @@ +/* + * libdivecomputer + * + * Copyright (C) 2015 Jef Driesen + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include +#ifdef HAVE_GETOPT_H +#include +#endif + +#include +#include +#include + +#include "dctool.h" + +static int +dctool_version_run (int argc, char *argv[], dc_context_t *context, dc_descriptor_t *descriptor) +{ + // Default option values. + unsigned int help = 0; + + // Parse the command-line options. + int opt = 0; + const char *optstring = "h"; +#ifdef HAVE_GETOPT_LONG + struct option options[] = { + {"help", no_argument, 0, 'h'}, + {0, 0, 0, 0 } + }; + while ((opt = getopt_long (argc, argv, optstring, options, NULL)) != -1) { +#else + while ((opt = getopt (argc, argv, optstring)) != -1) { +#endif + switch (opt) { + case 'h': + help = 1; + break; + default: + return EXIT_FAILURE; + } + } + + argc -= optind; + argv += optind; + + // Show help message. + if (help) { + dctool_command_showhelp (&dctool_version); + return EXIT_SUCCESS; + } + + printf ("libdivecomputer version %s\n", dc_version (NULL)); + + return EXIT_SUCCESS; +} + +const dctool_command_t dctool_version = { + dctool_version_run, + DCTOOL_CONFIG_NONE, + "version", + "Show version information", + "Usage:\n" + " dctool version [options]\n" + "\n" + "Options:\n" +#ifdef HAVE_GETOPT_LONG + " -h, --help Show help message\n" +#else + " -h Show help message\n" +#endif +};